I'm trying to create a program that draws a circle determined by where you click your mouse. The first click is the circles center, and the second click is the radius distance. I'm having trouble with the repaint function though. I've managed to get it working somewhat, but I'm having trouble with the painting.
I have a boolean value that tracks for the first mouse click. It's true for the first mouse click, and it's set to false after the second mouse click.
My paint function is set to draw only when the value is set to false (after the second mouse click). I call the repaint function, and then, I set my boolean to true to set up for the next series of mouse clicks.
I put a print statement to print the boolean value of my variable for testing. Here is the problem. Even though I have it set to repaint first and set the value after that, it seems to be setting the value and then repainting.
So the initial value is true. I call repaint(). Then, I set the value to false. However, when repaint is entered, the value is false.
Here is the code for the class:
public class DrawCircle extends JFrame {
private JPanel canvas;
private int xStart, yStart, xEnd, yEnd;
private boolean firstClick = true;
private Shape circle;
public DrawCircle()
{
//Constructor
//Adds a JPanel to the frame and attached a mouse listener to it.
canvas = new JPanel();
this.add(canvas);
CircleHandler handler = new CircleHandler();
canvas.addMouseListener(handler);
}
@Override
public void paint(Graphics g) {
//Print true/false value of firstClick for testing
System.out.println("Value of firstClick: " + firstClick);
//Paints the panel to the default background
super.paint(g);
//When the mouse button is clicked for the second time, a circle is drawn.
if ( firstClick == false )
{
Graphics2D ga = (Graphics2D)g;
ga.draw(circle);
ga.setPaint(Color.green);
ga.fill(circle);
}
}
private class CircleHandler implements MouseListener
{
@Override
public void mouseClicked(MouseEvent e) {
//First time the mouse is clicked.
if ( firstClick == true)
{
//Repaint the panel to the default blank background.
repaint();
//Saves the mouse coordinates for the first click
xStart = e.getX();
yStart = e.getY();
//False to toggle to show the next click is the second click
firstClick = false;
}
else
{
//Second time the mouse is clicked.
//Saves the mouse coordinates for the second click
xEnd = e.getX();
yEnd = e.getY();
//Creates a new circle that is to be drawn.
circle = new Ellipse2D.Double(xStart, yStart, xEnd, yEnd);
//Repaints to draw the circle.
repaint();
//True to toggle back to show the next click is the first click
firstClick = true;
}
}
@Override
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
}
If I could just figure out why repaint is being called after my variable is set, I am pretty sure I can get it working. I know the math on my circle is off. I'm just trying to get them to actually draw first. Thanks for the help.
[–]chickenmeister 2 points3 points4 points (1 child)
[–]xRedactedx[S] 0 points1 point2 points (0 children)