change
change

Reputation: 3558

exception on drawing polygon using java swing

hey i am writing a simple java swing program. but when i draw the frame with polygons drawn from a for loop an exception occurs.

the for loop:

for (int i=0;i<k;i++)
  { x= 100; y= 100;
  for(int j=0;j<k;j++)
  { if (cell[i][j]==0) break;
   if(i % 2 !=0)
    { p = new Polygon();
     for (int h = 0; h < 6; i++)
            p.addPoint((int) (x + 50 * Math.cos(h * 2 * Math.PI / 6)),(int) (y + 50 * Math.sin(h * 2 * Math.PI / 6)));

         g.drawPolygon(p);
    }    
   x+=75;
  }
  y+=45;
  }

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2834)
    at java.awt.Polygon.addPoint(Polygon.java:279)
    at freqnew.paintComponent(freqnew.java:44)
    at javax.swing.JComponent.paint(JComponent.java:1027)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:277)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1213)
    at javax.swing.JComponent.paint(JComponent.java:1013)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)

    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    at java.awt.Container.paint(Container.java:1762)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:810)

    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)

    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

tried to increase java heap but my method from command promt seems to be wrong. tried : java -Xms128m -Xmx256m

plz help me asap.

Upvotes: 0

Views: 515

Answers (1)

RedGrittyBrick
RedGrittyBrick

Reputation: 4002

Your code is incomplete and scrambled. Are you sure you don't have a loop terminating condition that can't occur? (i.e. an infinite loop). How many polygons are you drawing?

You could put in some logging to verify that the code does what you expect.

You could put in an early termination to the loop and use OS facilities to check memory usage to see if a leak (i.e. pack-ratting) is occurring.

Upvotes: 1

Related Questions