Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - Circles

craigster
craigster 


Drawing Circles with Mouse Click Part1: File Circles.java sets up a panel that creates and draws a circle as defined in Circle.java of random size and color at each mouse click. Each circle replaces the one before it. The code to handle the mouse clicks and do the drawing is in CirclePanel.java. Save these files to your directory, compile them and run them and experiment with the GUI. Then modify these files as described below. 1. This program creates a new circle each time—you can tell because each circle is a different color and size. Write a method void move(Point p) for your Circle class that takes a Point and moves the circle so its center is at that point. Now modify your CirclesListener class (defined inside CirclePanel) so that instead of creating a new circle every time the user clicks, it moves the existing circle to the clickpoint if a circle already exists. If no circle exists, a new one should be created at the clickpoint. So now a circle of the same color and size should move around the screen. 2. Write a method boolean isInside(Point p) for your Circle class that takes a Point and tells whether it is inside the circle. A point is inside the circle if its distance from the center is less than the radius. (Recall that the distance between two points (x1,y1) and (x2,y2) is sqrt((x2-x1)2+(y2-y1)2.) 3. Now modify the mousePressed method of CirclesListener so that the GUI behaves as follows: • If there is no circle (i.e., it is null) and the user clicks anywhere, a new (random) circle should be drawn at the click point. • If there is a circle on the screen and the user clicks inside that circle, the circle should go away. (Hint: To make the circle go away, set it to null and repaint.) • If there is a circle on the screen and the user clicks somewhere else, the circle should move to that point (no change from before). So the logic for mousePressed should look like this: if there is currently no circle create a new circle at the click point else if the click is inside the circle make the circle go away else move the circle to the click point repaint 4. Add bodies for the mouseEntered and mouseExited methods so that when the mouse enters the panel the background turns white, and when it exits the background turns blue. Remember that you can set the background color with the setBackground method. Part2: Modify the code in CirclePanel.java so that a circle is drawn when the user presses a mouse, but the user can drag it around until the mouse button is depressed. If the mouse button is released and then pressed again, a new circle is created, which can then be dragged around. You will need to make the following changes: 1. In the CirclePanel constructor, create a CirclesListener object and make it listen for both mouse events and mouse motion events. 2. Make the CirclesListener class implement the MouseMotionListener interface in addition to the MouseListener interface. This requires two steps: • Note in the header that CirclesListener implements MouseMotionListener. • Add bodies for the two MouseMotionListener methods, mouseDragged and mouseMoved. In mouseDragged, simply move the circle to the point returned by the getPoint method of the MouseEvent and repaint. Provide an empty body for mouseMoved. Attachments Tags Clarifications

Answers

Java - Circles - Solution

dickersonka
dickersonka 



Attached is the solution to the Java - Drawing Circles with Mouse Click assignment including source and comments Attachments
Circles.zip (2 K) Download
Preview
drawing on a panel. // Derived from Dots.java in Lewis and Loftus //***************************************************************** import javax.swing.JFrame; public class Circles { //----------------------------------------------------------------

Preview
// -- color RGB value 0..16777215 (24-bit) //--------------------------------------------------------- public Circle(Point point) { radius = Math.abs(generator.nextInt())%50 + 25; color = new Color(Math.abs(generator.nextInt())% 16777216); centerX = point.x; centerY = point.y; } //--------------------------------------------------------- // Draws circle on the graphics object given //--------------------------------------------------------- public void draw(Graphics page) { page.se

Preview
public void paintComponent (Graphics page) { super.paintComponent(page); if (circle != null) circle.draw(page); } //****************************************************************** // Represents the listener for mouse events. //****************************************************************** private class CirclesListener implements MouseListener, MouseMotionListener { //--------------------------------------------------------------- // Creates a new circle at the current location

Circles_screenshot1.png (16 K) Download Preview

Circles_screenshot2.png (27 K) Download Preview

Screenshots
Screenshot
Screenshot
Purchase Answer

Solution Circles

mariobeka
mariobeka 



Below you will find the classes with the implemented and commented code as asked. Attachments
Circles.java (1 K) Download Preview
application frame. //---------------------------------------------------------------- public static void main (String[] args) { JFrame circlesFrame = new JFrame

CirclePanel.java (2 K) Download Preview
the // circles are drawn. Derived from the Lewis and Loftus DotsPanel class. //************************************************************************ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class CirclePanel extends JPanel { private final int WIDTH = 600, HEIGHT = 400; private Circle circle; private int lastx; // ------------------------------------------------------------------- // Sets up this panel to listen for mouse events. //

Circle.java (1 K) Download Preview
// -- radius 25..74 // -- color RGB value 0..16777215 (24-bit) //--------------------------------------------------------- public Circle(Point point) { radius = Math.abs(generator.nextInt())%50 + 25; color = new Color(Math.abs(generator.nextInt())% 16777216); centerX = point.x; centerY = point.y; } //--------------------------------------------------------- // Draws circle on the graphics object given //--------------------------------------------------------- public void

Screen Shot 2014-04-29 at 12.10.04 AM.png (20 K) Download Preview

Screen Shot 2014-04-29 at 12.10.22 AM.png (20 K) Download Preview

Screenshots
Screenshot
Screenshot
Purchase Answer