# Random GFX Loop

• December 24th, 2013, 10:53 PM
GrabACup
Random GFX Loop
Hello fellow coders, i have an issue with my first assignment with graphics and the topic is fractural recursive trees; I have created the code and it seems up to par but when i run it, the amount of aplications and windows it opens is a continous loop for no reason when i don`t even have a loop code; please check out my code -
Code :

``` public void paintComponent(Graphics g) { new FractalTree().setVisible(true); } public class FractalTree extends JFrame {   public FractalTree() { // super("Fractal Tree"); setBounds(100, 100, 800, 600); // setResizable(false);   }   private void RecurLine(Graphics g, int x1, int y1, double angle, int n) { if (n == 1) return; int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * n * 10.0); int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * n * 10.0); g.drawLine(x1, y1, x2, y2); RecurLine(g, x2, y2, angle - 20, n - 1); RecurLine(g, x2, y2, angle + 20, n - 1); }   @Override public void paint(Graphics g) { g.setColor(Color.BLUE); RecurLine(g, 400, 500, -90, 9);   } }```

I have made a Jframe and a JPanel and am writing code in the JPanel. And that is all the code i have written. Thanks
Heres the problem ~ look at the taskbarAttachment 2566
• December 25th, 2013, 12:41 AM
GregBrannon
Re: Random GFX Loop
Overriding the paint() method is not a good idea unless you really know what you're doing as in you designed and wrote the Java language (almost).

The reason you're in a continuous loop is because you create a new object each time the paintComponent() method is called, which is, as you've demonstrated, quite often. The paintComponent() method was created specifically to be overridden, so you're good there, but due to the way painting works in Java, nearly the first statement in the method should be:

super.paintComponent( g );

That statement clears the graphics object which will clean up the presentation, but I'm guessing you'll still be stuck in a continuous painting loop due to the way painting works in Java. To learn more about that and to avoid the common mistakes you're making here, read the tutorial, Performing Custom Painting.
• December 25th, 2013, 01:08 AM
GrabACup
Re: Random GFX Loop