Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6542007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:10:27+00:00 2026-05-25T11:10:27+00:00

I’ve been attempting to create a pen tool for my Java drawing program using

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:10:27+00:00Added an answer on May 25, 2026 at 11:10 am

    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);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.