通过2次鼠标点击绘制一条线

时间:2012-09-04 06:32:24

标签: java awt graphics2d java-2d mouselistener

我试图通过一系列鼠标点击画一条线。

例如,当我从屏幕上的一个点点击到下一个点时,它会连接线条,然后我可以继续点击它继续绘制连续绘图线的下一个点。

示例如下:练习2

下的http://oneslime.net/java/Tutorial_2

我相信我的逻辑中有一些错误(不画线,只是一点),但我似乎无法找到它!

有人可以帮帮我吗?

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class RoadCreator extends JPanel {

    private MouseHandler mouseHandler = new MouseHandler();

    private Point previousPoint = new Point();
    private Point nextPoint = new Point();
    private boolean drawing;

    public RoadCreator() {
        this.setPreferredSize(new Dimension(640, 480));
        this.addMouseListener(mouseHandler);
       this.addMouseMotionListener(mouseHandler);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.blue);
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke(8,
            BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
        g.drawLine(previousPoint.x, previousPoint.y, nextPoint.x, nextPoint.y);
    }

    private class MouseHandler extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            drawing = true;
            nextPoint = e.getPoint();
            repaint();
            previousPoint = e.getPoint();

        }
    }

    public void display() {
        JFrame f = new JFrame("Road Creator");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
    }

    }

非常感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:6)

改为使用GeneralPathPath2D

  

GeneralPath类表示由直线和二次和三次(Bézier)曲线构成的几何路径。 它可以包含多个子路径。

enter image description here

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.GeneralPath;

public class RoadCreator extends JPanel {

    private MouseHandler mouseHandler = new MouseHandler();

    GeneralPath path = null;
    private boolean drawing = false;

    public RoadCreator() {
        this.setPreferredSize(new Dimension(320, 200));
        this.addMouseListener(mouseHandler);
        this.addMouseMotionListener(mouseHandler);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.blue);
        g2d.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke(8,
                BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
        if (path!=null) {
            g2d.draw(path);
        }
    }

    private class MouseHandler extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint(); 
            if (!drawing) {
                path = new GeneralPath();
                path.moveTo(p.x, p.y);
                drawing = true;
            } else {
                path.lineTo(p.x, p.y);
            }

            repaint();
        }
    }

    public void display() {
        JFrame f = new JFrame("Road Creator");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        RoadCreator rc = new RoadCreator();
        rc.display();
    }
}

答案 1 :(得分:1)

这不起作用。

public void mousePressed(MouseEvent e) {
    drawing = true;
    nextPoint = e.getPoint();
    repaint();
    previousPoint = e.getPoint();
}

您假设repaint是内联调用(即在返回之前绘制)。它没有,repaint会将请求排队到将在某个时间更新的重绘管理器。

public void mousePressed(MouseEvent e) {
    drawing = true;
    previousPoint = nextPoint
    nextPoint = e.getPoint();
    repaint();
}

应该工作,请注意,在用户第二次点击之前,previousPoint将为null。

答案 2 :(得分:0)

我找到了一种尽可能少的努力来完成这项任务的方法。然而,这并不等同于创建应用程序的“好”方式。您需要考虑将来的可扩展性,例如绘制其他对象的能力等等。所以,让我们来了解如何使其发挥作用。

1)让我们省略paintComponent方法......我相信这应该是paint()。

    //@Override
    /*protected void paintComponent(Graphics g) {
         //Do Something...
      }*/

2)让我们添加这个方法:

    public void drawLineHelper(Point prev, Point next){
        Graphics g = getGraphics();
        g.setColor(Color.blue);
        g.drawLine(previousPoint.x, previousPoint.y, nextPoint.x, nextPoint.y);
    }

3)最后,我们需要对mouseHandler类进行一些更改:

   private class MouseHandler extends MouseAdapter {
        //twoPoints make sure we have two points.
        boolean twoPoints=false;

        @Override
        public void mousePressed(MouseEvent e) {
            if(twoPoints==false){
                nextPoint = e.getPoint();
                twoPoints = true;
            }
            else{
                //Set previous to next from now on.
                previousPoint = nextPoint;

                //Get a new next point.
                nextPoint = e.getPoint();

                //Helper method will draw the line each time.
                drawLineHelper(previousPoint,nextPoint);
                //repaint() no longer necessary.

            }
        }
    }

以下是对具有相同问题的另一个示例的快速参考。页面上的最后一篇文章解释了同样的方法:http://www.ozzu.com/programming-forum/java-repainting-problem-t49362.html