0

我试图从一系列鼠标点击中画一条线。

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

一个例子在这里: http: //oneslime.net/java/Tutorial_2练习 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);
    }

    }

任何帮助将不胜感激,谢谢!

4

3 回答 3

6

使用GeneralPathorPath2D代替。

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

在此处输入图像描述

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();
    }
}
于 2012-09-04T07:00:51.937 回答
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 将为空,直到用户第二次单击。

于 2012-09-04T06:45:11.983 回答
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

于 2012-09-04T16:28:20.083 回答