
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.
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, 400variant, 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.length2)
{
}
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

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.

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.

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.

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

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:
Code:
super.paintComponent(gr);
gr.drawPolygon(fractal);
Also, be sure to add the JPanel to the JApplet's contentPane.

thanks, i'll update the applet