swing - Issues Creating a Pen Tool with Java's Path2D -


"itemprop =" text ">

I try to create a pen tool for my Java drawing program using the Path2D class in conjunction with mouse listeners Is doing, but I have done to provoke results. The tool will work for several seconds, but then the whole application will be freeze and close. (There is no exception here, the program still freezes). Here is an SSCE that reflects this 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 PendomapNalJPNL (Private Point Start; Private Point Stop; Private Size Size; Public Pendemo Panel) {setBackground (Color.white); SetPreferredSize (New Dimension (600, 600)) Path Listener Listener = New Path Listener () ; AddMouseListener;;;;;;;; Public Zero Paint Component (Graphics GC); SuperpactConant (GC); Graphics 2DG2 = (Graphics 2D); GC; if (start! = Null & amp; stop! = Faucet) {Basic Stroke Stroke = New Bass Stok (1); Size = Stroke. Cratestrad you (size); g2.draw (size); g2.fill (size);}} Private class pathlistener provides mouseAdapter {public zero mouse mouse pressed (mouse Event event) {start = Event.getPoint (); path2d path = new path2d. double (); size = path;} public zero mouse drag ged (mouse event event) {stop = event.getPoint (); path2d path = (path 2D) shape; Path.moveTo (start.x, start.y); Path.lineTo (stop.x, stop.y); Size = Path; Start = stop; Repaint ();} Public Zero Mouse Release Done (mouseover T event) {path 2D path = (Path 2D) shape; Path.closePath (); Size = path; Repaint (); }} Public static zero main (string [] args) {pendemone shapes = new pandemapenel (); Jeffre Frame = New Gefram ("Pendemo"); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); . Add frame.getContentPane () (size); Frame.pack (); Frame.setVisible (true); }}   

I wrote the code for my own class, which worked perfectly here, but I use some of the extra functionality in Path2D class wanted to do.

Am I doing something wrong here or am trying to do what I am not capable of in Path2D?

Any help would be greatly appreciated.

After the text "itemprop =" text ">

The problem seems to come from letting the stroked shapes shape back. If you avoid doing this, then the app remains liable. 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 Pendmopanel Jepianel (starting private Point; private Point stop, personal size, public Pendemo panel () {setBackground (Color.white); SetPreferredSize (new Dimension (600, 600)); path Listnr listener = new path Listnr () ; AddMouseListener;;;;;;;; Public Zero Paint Component (Graphics GC); SuperpactConant (GC); Graphics 2DG2 = (Graphics 2D); GC; if (start! = Null & amp; stop! = Faucet) {Basic Stroke Stroke = New Bass Stoke (1); shape Strokadhar = Strokkkretestrodap (size); g2.draw (strokedShape); g2.fill (strokedShape);}} offers private classes PathListener MouseAdapter {public void Mouse pressed (Mausivent event) { Start = EventkgetPoint (); path 2D path = new path 2D. Double (); size = path;} public void mouse drag ged (Mausivent event) {stop = event.getPoint (); path 2D path = ( path 2D) shape; Path.moveTo (start.x, start.y); Path.lineTo (stop.x, stop.y); size = path: Start = Stop; repaint ();} public void mouse Released (Mouse Event Event) {Path 2D Path = (Path 2D) shape; Try {path.closePath (); } Hold (exception eleven) {} size = path; Repaint (); }} Public static void (String [] args) {SwingUtilities.invokeLater (new Runnable () {public void run () {PenDemoPanel size = new PenDemoPanel (); JFrame frame = new JFrame ( "PenDemo"); frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE); frame.getContentPane () add (size); frame.pack (); frame.setVisible (true);}}); }}

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -