How to initialize Graphics object without using paint method??
807606Feb 15 2007 — edited Feb 15 2007This is a code to make sierpinski triangle using IFS.
I am using a button called "Iterate" which calls funtion paint() .
paint1() gets called from paint on pressing the button.
I want to convert a square into Sierpinski triangle by iterations.
I want to make a square first .If I put that code in paint() then it gets called everytime.
so problem lines are: Graphics g;
g.drawLine(0,0,99,0);
g.drawLine(0,0,0,99);
g.drawLine(0,99,99,99);
g.drawLine(99,0,99,99);
it gives error as object not initialised.
rest of the working code is as follows.
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class mys31 extends Applet implements ActionListener{
String msg ="";
Button iterate;
int i,j;
double [][]t;
double [][]s;
double []a;
double []b;
double []c;
double []d;
double []e;
double []f;
public void init() {
t=new double[100][100];
s=new double[100][100];
a=new double[]{0.5,0.5,0.5};
b=new double[]{0.0,0.0,0.0};
c=new double[]{0.0,0.0,0.0};
d=new double[]{0.5,0.5,0.5};
e=new double[]{1.0,1.0,50.0};
f=new double[]{1.0,50.0,50.0};
for(i=0;i<100;i++)
{
t[0]=1.0;
t[0][i]=1.0;
t[99][i]=1.0;
t[i][99]=1.0;
}
for(i=0;i<100;i++)
for(j=0;j<100;j++)
s[i][j]=0.0;
iterate = new Button("Iterate");
add(iterate);
iterate.addActionListener(this);
} // end init
public void actionPerformed(ActionEvent ae){
String str = ae.getActionCommand();
if(str.equals("Iterate"))
msg=" working";
repaint();
} //end of action Performed
public void paint (Graphics g) {
g.drawString(msg,200,200);
paint1(g);
} // end paint
public void paint1(Graphics g)
{
int j=0,i;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
if(t[i][j]==1)
{
s[(int)(a[0]*i b[0]*j e[0])][(int)(c[0]*i + d[0]*j + f [0])]=1;
s[(int)(a[1]*i b[1]*j e[1])][(int)(c[1]*i + d[1]*j + f [1])]=1;
s[(int)(a[2]*i b[2]*j e[2])][(int)(c[2]*i + d[2]*j + f [2])]=1;
}
g.setColor(Color.red);
for(i=0;i<100;i++)
for(j=0;j<100;j++)
{
t[i][j]=s[i][j];
s[i][j]=0.0;
if(t[i][j]==1.0)
g.drawLine(i,j,i,j);
}
}//end paint1
} // end class