3

I've been attempting to create a pen tool for my Java drawing program using the Path2D class in conjunction with mouse listeners, but I've had baffling results. The tool will work for several seconds, but then the entire application will freeze and will have to be closed. (No exceptions occur here; the program just freezes). Here's an SSCCE that demonstrates the issue:

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

I had written my own Path class, which worked perfectly here, but I wanted to use some of the additional functionality in the Path2D class.

Am I doing something wrong here or is Path2D not capable of what I'm trying to do?

Any help would be greatly appreciated.

InsaneOstrich
  • 119
  • 5
  • 13
  • 4
    Please provide an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Sep 02 '11 at 17:03
  • @Andrew Thompson really terrible is this popup window with mouse hoverover (-: for users that completed a questionnaire about themselves :-) – mKorbel Sep 02 '11 at 17:13
  • @mKorbel Do you mean like discussed [here](http://meta.stackexchange.com/questions/104824/style-of-gravatar-icon-display-changed-on-stackoverflow/)? – Andrew Thompson Sep 02 '11 at 17:31
  • Sorry I didn't include the sscce earlier; it's there now, along with a more detailed explanation of what my question is. – InsaneOstrich Sep 02 '11 at 19:16
  • @Andrew Thompson my comment isn't about your personal***, very *** without feel free for discu***, why that forum still exist, because for me the red color still have got more than a thousand shades, which I can personally see, other forums would have to fail, if their would have closing button, same feel about down-voting, in short God is not in any way as fair to everyone from us, :-) – mKorbel Sep 03 '11 at 07:24

1 Answers1

6

The problem seems to come from assigning the stroked shape back to the shape. If you avoid doing that, the app. remains responsive. Vis.

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);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433