gpt4 book ai didi

java - 每次鼠标单击时绘制线条,双击时停止绘制

转载 作者:行者123 更新时间:2023-11-30 06:28:39 27 4
gpt4 key购买 nike

我必须随着鼠标移动而绘制线条,单击鼠标后,应继续绘制线条,直到双击。比如说A点、B点和C点。当鼠标点击A点时,就开始画线。当鼠标单击 B 点时,上一条线应该在那里,并且新线应该从 B 点开始绘制,除非双击。 C 点应该是相同的,依此类推,直到双击鼠标。经过一番研究,我编写了下面的代码,它工作得很好,但它适用于拖动 NOT MOUSE MOVE 事件,我尝试将代码传输到 mouseMoved 事件,但前面的行正在消失。有人可以帮我解决这个问题吗?

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; }
}

关于java - 每次鼠标单击时绘制线条,双击时停止绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46576948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com