Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Random GFX Loop

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 -
     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 taskbar2uhxsia.jpg


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random GFX Loop

    Thanks... Close Thread

Similar Threads

  1. Looking for someone good at 3D GFX
    By Earthly Minds in forum Paid Java Projects
    Replies: 3
    Last Post: June 11th, 2013, 12:40 PM
  2. Java Math.random() and loop
    By maple1100 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: December 28th, 2012, 12:23 PM
  3. Problems with Math.Random() in a for loop
    By csharp100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 06:18 PM
  4. Help with a random loop
    By A1one1nDarkness in forum Loops & Control Statements
    Replies: 1
    Last Post: October 20th, 2011, 07:54 AM
  5. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 10:06 PM