问题描述
我必须在鼠标移动时画线,单击鼠标后,应该继续画线,直到双击为止.说点A,点B和点C.在点A上单击鼠标时,应开始画线.当在B点上单击鼠标时,除非双击,否则上一条线应与新线一起从B点开始绘制.对于C点,应该相同,依此类推,直到双击鼠标为止.经过研究,我编写了下面的代码,它工作得很好,但是它可以用于拖动NOT MOUSE MOVE事件,我试图将代码转移到mouseMoved事件,但是上一行消失了.有人可以帮我解决这个问题吗?
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;
}
}
推荐答案
请注意添加的标志:
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; }
}
这篇关于每次单击鼠标都画一条线,双击则停止绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!