Reputation: 15
I am trying to figure out how to complete the assignment below
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
Reputation: 347184
So, basically, based on you code example, you need:
MouseMotionListener
, this will include the mouseDragged
methodCirclesListener
to the circlePanel
(JPanel#addMouseMotionListener
)mouseDragged
is called, you need to take the Point
from the MouseEvent
and call Circle#move
and repaint
the componentIf you get stuck, best place to start is How to Write a Mouse Listener
Upvotes: 2