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

Java - Drawing Circles with Mouse Click

tt95588n
tt95588n 


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 - Drawing Circles with Mouse Click - 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
in Lewis and Loftus //***************************************************************** import javax.swing.JFrame; public class Circles { //---------------------------------------------------------------- // Creates and displays the application frame.

Preview
circle on the graphics object given //--------------------------------------------------------- public void draw(Graphics page) { page.setColor(color); page.fillOval(centerX-radius,centerY-radius,radius*2,radius*2); } //--------------------------------------------------------- // Moves circle to specified center point //--------------------------------------------------------- public void move(Point p){ //update center x centerX = p.x; //update center y centerY = p.y; } //---

Preview
{ //set background of panel to white when entering setBackground(Color.WHITE); } public void mouseExited (MouseEvent event) { //set background of panel to blue when leaving setBackground(Color.BLUE); } @Override public void mouseDragged(MouseEvent e) { //make sure we have a circle if(circle != null){ //move circle to follow mouse circle.move(e.getPoint());

Circles_screenshot1.png (16 K) Download Preview

Circles_screenshot2.png (27 K) Download Preview

Screenshots
Screenshot
Screenshot
Purchase Answer

Solution

mariobeka
mariobeka 



Below you will find the classes with the implemented and commented code as asked. Attachments
Screen Shot 2014-04-29 at 12.10.22 AM.png (20 K) Download Preview

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

Circles.java (1 K) Download Preview
static void main (String[] args) { JFrame circlesFrame = new JFrame ("Circles"); circlesFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); circlesFrame.getContentPane().add

CirclePanel.java (2 K) Download Preview
public void mouseExited(MouseEvent event) { setBackground(Color.BLUE); } // Drag the circle in new Position public void mouseDragged(MouseEvent e) { // Check if circle exists, else do nothing if (circle != null) circle.move(e.getPoint()); repaint(); } // ----------------------------------------------------------------- // Provide empty definitions for unused event methods. // ----------------------------------------------------------------- public void mouseClicked(MouseEvent eve

Circle.java (1 K) Download Preview
static Random generator = new Random(); //--------------------------------------------------------- // Creates a circle with center at point given, random radius and color // -- 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())%

Screenshots
Screenshot
Screenshot
Purchase Answer