What I'm trying to do is to draw circles and lines.
When the mouse is first pressed, I draw a small circle. Then, I need to draw a line connecting the original point to the current position of the mouse. When the mouse is released, the line remains, but when I click again, everything disappears and I draw a circle and a line all over again.
This is the code I have so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Canvas4 extends JComponent implements MouseListener, MouseMotionListener {
//constructor
public Canvas4() {
super();
addMouseListener(this);
addMouseMotionListener(this);
}
//some variables I may or may not use
int pressedX;
int pressedY;
int currentX;
int currentY;
int startDragX;
int startDragY;
int endDragX;
int endDragY;
int mouseReleasedX;
int mouseReleasedY;
//mouse events
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mousePressed(MouseEvent event) {
pressedX = event.getX();
pressedY = event.getY();
drawCircle();
startDragX = pressedX;
startDragY = pressedY;
}
public void mouseReleased(MouseEvent event) {
mouseReleasedX = event.getX();
mouseReleasedY = event.getY();
//repaint() here maybe???
}
//mouse motion events
public void mouseDragged(MouseEvent event) {
System.out.println("You dragged the mouse.");
endDragX = event.getX();
endDragY = event.getY();
drawLine();
}
public void mouseMoved(MouseEvent event) { }
//draw circle when mouse pressed
//this method works fine
public void drawCircle() {
Graphics g1 = this.getGraphics();
g1.setColor(Color.CYAN);
g1.fillOval(pressedX, pressedY, 10, 10);
}
//draw line when mouse dragged
//this is where I need help
public void drawLine() {
Graphics g2 = this.getGraphics();
g2.setColor(Color.RED);
g2.drawLine(pressedX, pressedY, mouseReleasedX, mouseReleasedY);
}
}
Then of course, there's a main method that creates the class object and adds it to a frame and whatnot.
My two specific questions are:
- how do I draw a line as it's dragged? The code I have currently only draws a line to the last point of mouse release.
- When do I repaint? If I repaint in the
drawCircle()method, the circle blinks in and out instead of disappearing on the next click.
If you want to draw circles and lines then you need to keep an ArrayList of Shapes to draw. You would add an
Ellipse2D.Doublefor the circle and aLine2D.Doublefor the line.In the
mousePressedevent you add theEllipse2D.Doubleobject to the ArrayList, then you set up a temporaryLine2D.Doubleobject to contain your line information.In the
mouseDraggedevent you updateLine2D.Doubleobject with the new end point and then invoke repaint().In the
mouseReleasedevent you add theLine2D.Doubleobject to the ArrayList and clear the variable referencing theLine2D.Doubleobject.Then in the paintComponent() method you add logic to:
ArrayListto paint eachShape.Line2D.Doubleobject when not nullCheck out the
Draw On Componentexample found in Custom Painting Approaches. This will show you the basic concept of this approach.In the example the ArrayList only contains information on Rectangles so you will need to make it more generic to hold a
Shapeobject. BothEllispse2D.DoubleandLine2D.Doubleimplement theShapeinterface.