我必须在鼠标移动时绘制线路,在鼠标单击后,应继续绘制线直到双击。说点A点A,点B和点C。当鼠标在A点单击点时,应启动行的绘制。当鼠标点击B点B点时,除非双击,否则应与点绘制新线路一起使用。对于点C,应该相同,依此类推,直到双击鼠标为止。经过一些研究,我已经写下了下面的代码,它的工作原理很好,但是它可以用于拖动鼠标移动事件,我试图将代码传输到Mousemped事件中,但是上一条线正在消失。有人可以帮我解决这个问题吗?
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;
}
}
注意添加的标志:
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; }
}