每次鼠标点击都会画线,双击时会停止绘制

时间:2017-10-05 02:25:18

标签: java swing draw custom-painting

我必须在鼠标移动时绘制线条,鼠标单击后,该线条应继续绘制,直到双击。 说点A,点B和点C.当鼠标点击A点时,应该开始绘制线。当在B点上单击鼠标时,prev行应该在那里,除非双击,否则应该从B点开始绘制新行。对于C点应该是相同的,依此类推,直到双击鼠标。经过一些研究,我已经编写了下面的代码,它工作得很好,但是它适用于拖动NOT MOUSE MOVE事件,我试图将代码转移到mouseMoved事件但是prev行消失了。有人可以帮我解决这个问题吗?

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 个答案:

答案 0 :(得分:1)

注意添加的标志:

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