使用Java Path2D创建钢笔工具的问题

时间:2011-09-02 16:56:09

标签: java swing user-interface graphics2d

我一直在尝试使用Path2D类和鼠标监听器为我的Java绘图程序创建一个笔工具,但我有令人困惑的结果。该工具将工作几秒钟,但整个应用程序将冻结,必须关闭。 (这里没有例外;程序只是冻结)。这是一个证明问题的SSCCE:

import java.awt.BasicStroke;
import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PenDemoPanel extends JPanel {

    private Point start;
    private Point stop;
    private Shape shape;

    public PenDemoPanel() {
        setBackground(Color.white);
        setPreferredSize(new Dimension(600, 600));
        PathListener listener = new PathListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }

    public void paintComponent(Graphics gc) {
        super.paintComponent(gc);

        Graphics2D g2 = (Graphics2D) gc;

        if (start != null && stop != null) {
            BasicStroke stroke = new BasicStroke(1);
            shape = stroke.createStrokedShape(shape);
            g2.draw(shape);
            g2.fill(shape);
        }

    }

    private class PathListener
        extends MouseAdapter {

        public void mousePressed(MouseEvent event) {
            start = event.getPoint();
            Path2D path = new Path2D.Double();
            shape = path;
        }

        public void mouseDragged(MouseEvent event) {
            stop = event.getPoint();

            Path2D path = (Path2D) shape;
            path.moveTo(start.x, start.y);
            path.lineTo(stop.x, stop.y);
            shape = path;
            start = stop;

            repaint();

        }

        public void mouseReleased(MouseEvent event) {
            Path2D path = (Path2D) shape;
            path.closePath();
            shape = path;
            repaint();
        }
    }

    public static void main(String[] args) {
        PenDemoPanel shapes = new PenDemoPanel();

        JFrame frame = new JFrame("PenDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(shapes);

        frame.pack();
        frame.setVisible(true);
    }
}

我编写了自己的Path类,它在这里工作得很好,但我想使用Path2D类中的一些附加功能。

我在这里做错了什么,或者Path2D不能胜任我想做的事情?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:6)

问题似乎来自于将描边形状分配回形状。如果你避免这样做,应用程序。保持敏感。可见。

import java.awt.BasicStroke;
import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import javax.swing.*;

public class PenDemoPanel extends JPanel {

    private Point start;
    private Point stop;
    private Shape shape;

    public PenDemoPanel() {
        setBackground(Color.white);
        setPreferredSize(new Dimension(600, 600));
        PathListener listener = new PathListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }

    public void paintComponent(Graphics gc) {
        super.paintComponent(gc);

        Graphics2D g2 = (Graphics2D) gc;

        if (start != null && stop != null) {
            BasicStroke stroke = new BasicStroke(1);
            Shape strokedShape = stroke.createStrokedShape(shape);
            g2.draw(strokedShape);
            g2.fill(strokedShape);
        }
    }

    private class PathListener
        extends MouseAdapter {

        public void mousePressed(MouseEvent event) {
            start = event.getPoint();
            Path2D path = new Path2D.Double();
            shape = path;
        }

        public void mouseDragged(MouseEvent event) {
            stop = event.getPoint();

            Path2D path = (Path2D) shape;
            path.moveTo(start.x, start.y);
            path.lineTo(stop.x, stop.y);
            shape = path;
            start = stop;

            repaint();
        }

        public void mouseReleased(MouseEvent event) {
            Path2D path = (Path2D) shape;
            try {
                path.closePath();
            } catch(Exception ingore) {
            }
            shape = path;
            repaint();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                PenDemoPanel shapes = new PenDemoPanel();

                JFrame frame = new JFrame("PenDemo");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(shapes);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}