问题
I have to draw line as the mouse moves, after the mouse click, the line should be continued to draw until double clicked. Say point A, point B and point C. when mouse clicked at point A, the drawing of line should be started. when mouse is clicked on point B, the prev line should be there along with that new line should be started drawing from point B unless double clicked. Should be the same for point C and so on until the mouse is double clicked. After some research, I have written below code, it works perfectly fine, but it works for dragging NOT MOUSE MOVE event, I tried to transfer code to mouseMoved event but the prev lines are disappearing. Can someone help me fix this?
public class WorkingLines {
public WorkingLines() {
JFrame jf = new JFrame("Free Hand Drawing Example");
Board draw = new Board();
jf.add(draw);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(600, 500);
jf.setVisible(true);
}
public static void main(String a[]) {
new WorkingLines();
}
}
class Board extends JPanel implements MouseListener, MouseMotionListener {
ArrayList<pts> list = new ArrayList<pts>();
Point start, end;
public Board() {
start = null; /* Initializing */
end = null;
// this.setBackground(Color.BLACK);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
super.paint(g2);
// g.setColor(Color.BLACK);
for (pts p : list) {
System.out.println("Drawing Lines!!!=>");
g.drawLine((int) p.getStart().getX(), (int) p.getStart().getY(), (int) p.getEnd().getX(),
(int) p.getEnd().getY());
}
if (start != null && end != null) {
System.out.println("Drawing happening!!!=>");
g.drawLine(start.x, start.y, end.x, end.y);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent me) {
start = me.getPoint();
System.out.println("Start initialized!!");
for (pts pt : list) {
System.out.println("Point x coordinate =>" + pt.start.getX());
}
repaint();
}
@Override
public void mouseReleased(MouseEvent me) {
end = me.getPoint();
pts pt = new pts(start, end);
list.add(pt);
repaint();
if (me.getClickCount() == 2) {
start = null;
end = null;
}
}
@Override
public void mouseDragged(MouseEvent me) {
System.out.println("Mouse Moved=>" + start);
end = me.getPoint();
repaint();
}
@Override
public void mouseMoved(MouseEvent me) {
}
}
class pts {
Point start = null;
Point end = null;
public pts(Point start, Point end) {
this.start = start;
this.end = end;
}
public Point getStart() {
return this.start;
}
public Point getEnd() {
return this.end;
}
}
回答1:
Note the flag added:
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WorkingLines {
public WorkingLines() {
JFrame jf = new JFrame("Free Hand Drawing Example");
JPanel draw = new DrawBoard();
jf.add(new Label("Click....move mouse, click again"), BorderLayout.NORTH);
jf.add(draw);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(600, 500);
jf.setVisible(true);
}
public static void main(String a[]) {
new WorkingLines();
}
}
class DrawBoard extends JPanel implements MouseListener, MouseMotionListener {
ArrayList<pts> list = new ArrayList<>();
Point start, end;
private boolean isDrawing = false; //a flag
public DrawBoard() {
start = null; end = null; /* Initializing */
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
super.paintComponent(g2);
for (pts p : list) {
g.drawLine((int) p.getStart().getX(), (int) p.getStart().getY(), (int) p.getEnd().getX(),
(int) p.getEnd().getY());
}
if ((start != null) && (end != null)) {
g.drawLine(start.x, start.y, end.x, end.y);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent me) {
if(! isDrawing) {
start = me.getPoint();
isDrawing = ! isDrawing;
}else {
pts pt = new pts(start, end);
list.add(pt);
start = end;
}
}
@Override
public void mouseReleased(MouseEvent me) {
if (me.getClickCount() == 2) {
start = null;
end = null;
isDrawing = false;
}
}
@Override
public void mouseDragged(MouseEvent me) {}
@Override
public void mouseMoved(MouseEvent me) {
end = me.getPoint();
repaint();
}
}
class pts {
Point start = null, end = null;
public pts(Point start, Point end) {
this.start = start;
this.end = end;
}
public Point getStart() { return start; }
public Point getEnd() { return end; }
}
来源:https://stackoverflow.com/questions/46576948/draw-lines-for-every-mouse-click-and-stop-drawing-when-double-clicked