Results 1 to 7 of 7
  1. #1
    sir.logic209 is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Question unwanted return to paint

    Im very new to java, and i'm very confused as to what my Applet is doing. Basically, my Applet appears to be returning from a method before it's finished, and it then does some really weird things.

    Basically, i'm trying to design an Applet to draw the Kosh curve. I've put in the basic structure of my Applet here and omitted statements to make it eisier to pinpoint the issue.

    Java Code:
    import javax.swing.JApplet;
    import java.awt.*;
    
    public class TriFractal extends JApplet
    {
        int variant = (int) Math.round(300/Math.tan(Math.toRadians(60)));
        int[] lines = {400+variant, 400, 400, 100, 400-variant, 400};
        int iterations = 1;
        Polygon fractal = new Polygon();
    
        public void paint(Graphics gr)
        {
            lines = CalcPoints(lines, gr);
    
            for(int index=0; index<lines.length; index+=2)
            {
                fractal.addPoint(lines[index],lines[index+1]);
            }
    
            gr.drawPolygon(fractal);
        }
    
        public int[] CalcPoints(int[] Xarray, Graphics gr)
        {
            int[] tempArray;
            int[] array = Xarray;
    
            for(int itCount = 1; itCount<=iterations; itCount = itCount+1)
            {
                for(int arrayElement=0; arrayElement<array.length; arrayElement +=2)
                {
                    if(arrayElement == array.length-2)
                    {
                    }
                    else
                    {
                    }
    
                }
                array = tempArray;
            }
    
            return array;
        }
    }

    The method Calcpoints() is called and returns to the paint method, but CalcPoints() continues to run or is called again after paint() has finished drawing the polygon fractal.

    Why is this happening?

    Thanks,

    Sir.Logic
    Last edited by Fubarable; 12-06-2010 at 01:36 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Put a println statement before your CalcPoints call, inside the method itself, and after it, to see how many times each prints out. This way you can determine how many times its called, how many times it processes, and how many times it succeeds.

  3. #3
    sir.logic209 is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Yeah i have done this. The calcPoints() method is called twice apparently b/c it continues to do work after paint() finishes.

    i've also had this same problem with the paint() method defore. It almost seems like the paint method is executed twice when it should only execute once.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You cannot directly control when and if paint gets called and should build your program with this in mind. This is one of the reasons why we recommend that no program logic go in any paint or paintComponent method, but rather only painting instructions. You shouldn't be painting directly in the applet anyway, but rather should paint in a JPanel's paintComponent method.

  5. #5
    sir.logic209 is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    ok, I understand the first part about not controling when paint is called; the browser will call it when it sees fit. but i don't know how to get around it. control goes to paint from the browser, correct? Can you tell me how i should structure the applet so that i bypass the problem? Again, im new to java (and programming) and haven't quite gotten a hang of all the features.

    Thank you very much for the info

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You do all your calculations elsewhere in an actionPerformed if they occur on button press or perhaps in the constructor if the calculations occur once on JApplet start up, and you place all the data points into your data structure (the Polygon) at this time. Then call repaint on the JPanel that draws the Polygon. I believe that the JPanel's paintComponent method will only have two lines:

    Java Code:
    super.paintComponent(gr);
    gr.drawPolygon(fractal);
    Also, be sure to add the JPanel to the JApplet's contentPane.

  7. #7
    sir.logic209 is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 08:07 AM
  2. Unwanted Java update
    By Hummingbird in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 11:18 AM
  3. unwanted bug. JMenu
    By ocean in forum New To Java
    Replies: 7
    Last Post: 11-20-2009, 10:29 PM
  4. Repositioning An unwanted JInternalFrame
    By marco.c84 in forum AWT / Swing
    Replies: 8
    Last Post: 03-18-2009, 10:42 PM
  5. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 11:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •