bicycle928130
bicycle928130

Reputation: 15

How to implement a mouse listener that will help drag a circle in java?

I am trying to figure out how to complete the assignment below

  1. 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.
  2. In the CirclePanel constructor, create a CirclesListener object and make it listen for both mouse events and mouse motion events.
  3. 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.

I know what I need to do (for the most part), I just can't figure out how to do it. (New to programming). Thank you!

public class circlePanel extends JPanel {
private final int WIDTH = 600, HEIGHT = 400;
private Circle circle;

// -------------------------------------------------------------------
// Sets up this panel to listen for mouse events.
// -------------------------------------------------------------------
public circlePanel() {
    addMouseListener(new CirclesListener());
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
}

// -------------------------------------------------------------------
// Draws the current circle, if any.
// -------------------------------------------------------------------
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 whenever the
    // mouse button is pressed and repaints.
    // ---------------------------------------------------------------
    public void mousePressed(MouseEvent event) {
        if (circle == null) {
            circle = new Circle(event.getPoint());
        } else if (circle.isInside(event.getPoint())) {
            circle = null;
        } else {
            circle.move(getMousePosition());
        }
        repaint();
    }

    // -----------------------------------------------------------------
    // Provide empty definitions for unused event methods.
    // -----------------------------------------------------------------
    public void mouseClicked(MouseEvent event) {
    }

    public void mouseReleased(MouseEvent event) {
    }

    public void mouseEntered(MouseEvent event) {
        setBackground(Color.white);
    }

    public void mouseExited(MouseEvent event) {
        setBackground(Color.blue);
    }
}
}

Here is the circles class

public class Circles {
// ----------------------------------------------------------------
// Creates and displays the application frame.
// ----------------------------------------------------------------
public static void main(String[] args) {
    JFrame circlesFrame = new JFrame("Circles");
    circlesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    circlesFrame.getContentPane().add(new circlePanel());
    circlesFrame.pack();
    circlesFrame.setVisible(true);
}
}

aannnddddd...here is the Circle class

public class Circle {
private int centerX, centerY;
private int radius;
private Color color;
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()) % 16777216);
    centerX = point.x;
    centerY = point.y;
}

// ---------------------------------------------------------
// Draws circle on the graphics object given
// ---------------------------------------------------------
public void draw(Graphics page) {
    page.setColor(color);
    page.fillOval(centerX - radius, centerY - radius, radius * 2,     
radius * 2);
}

public void move(Point p) {
    centerX = p.x;
    centerY = p.y;
}

public boolean isInside(Point p) {
    if (Math.sqrt(Math.pow(p.x - this.centerX, 2) + Math.pow(p.y - 
this.centerY, 2)) < this.radius) {
        return true;
    } else {
        return false;
    }
}
}

Upvotes: 0

Views: 996

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

So, basically, based on you code example, you need:

  • Implement the functionality of the MouseMotionListener, this will include the mouseDragged method
  • You need to register the CirclesListener to the circlePanel (JPanel#addMouseMotionListener)
  • When mouseDragged is called, you need to take the Point from the MouseEvent and call Circle#move and repaint the component

If you get stuck, best place to start is How to Write a Mouse Listener

Upvotes: 2

Related Questions