import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Colori extends Applet implements ActionListener,Runnable{
Color c [];
double Left,Right,Top,Bottom;
double c1,c2;
double p1x,p1y,p2x,p2y;
Button tasto,tasto1,tasto2,tasto3;
TextField t1,t2,t3,t4,t5,t6;
Panel p1,p2;
Image mandel;
int nim;
Thread animazione;
boolean anima;
Image o;
Graphics og;
public void start() { if(anima)if (animazione== null) { animazione = new Thread(this); animazione.start(); } }
public void stop() { if (animazione != null) { animazione = null; } }
public void init(){
o = createImage(size().width,size().height);
og = o.getGraphics();
Left=-2.;Right=2.;Top=1.5;Bottom=-1.5;
c1=0.1654104;c2=-0.6369678;
p1x=-1.;p1y=0;p2x=2;p2y=0;
nim = 10;
anima=false;
c = new Color[500];
creaTavolozza();
//for(int i=0;i<500;i++){
// c[i]=Color.getHSBColor((float)(1F*(Math.random()*100*i*i/3)/500),1.0F,1.0F);
// }
tasto = new Button("Nuovo disegno");
tasto1 = new Button("Nuova tavolozza");
tasto2 = new Button("Disegna");
tasto3 = new Button("Animazione");
t1 = new TextField(Double.toString(c1));
t2 = new TextField(Double.toString(c2));
t3 = new TextField(Double.toString(p1x));
t4 = new TextField(Double.toString(p1y));
t5 = new TextField(Double.toString(p2x));
t6 = new TextField(Double.toString(p2y));
p1 = new Panel();p2 = new Panel();
setLayout(new BorderLayout());
add(p1,BorderLayout.SOUTH);
add(p2,BorderLayout.NORTH);
p1.add(tasto);p1.add(tasto1);p1.add(tasto2);
p1.add(t1);p1.add(t2);
p2.add(tasto3);
p2.add(t3);p2.add(t4);p2.add(t5);p2.add(t6);
tasto.addActionListener(this);
tasto1.addActionListener(this);
tasto2.addActionListener(this);
tasto3.addActionListener(this);
mandel = getImage(getCodeBase(),"Mandelbrot.gif");
}
void creaTavolozza(){
double a, b;
for (int xyz=0; xyz<4; xyz++){
a=Math.random();
b=100*Math.random();
for(int i=xyz*125;i<(xyz+1)*125;i++){
c[i]=Color.getHSBColor((float)(1.0F*(b*(Math.log(a*(i-125*xyz))))/500),1.0F,1.0F);
}
}
}
public void run(){
for(int i=0;i < nim;i++){
c1 = p1x +i*(p2x-p1x)/nim;
c2 = p1y + i*(p2y-p1y)/nim;
System.out.println("i="+i);
t1.setText(Double.toString(c1));
t2.setText(Double.toString(c2));
repaint();
try { Thread.sleep(500);}
catch (InterruptedException e){}
}
anima = false;
}
public void actionPerformed(ActionEvent ev){
if(ev.getSource()==tasto){
c1 = Math.random()*4-2.;
c2 = Math.random()*3-1.5;
t1.setText(Double.toString(c1));
t2.setText(Double.toString(c2));
repaint();
}
if(ev.getSource()==tasto1){
creaTavolozza();
repaint();
}
if(ev.getSource()==tasto2)
{
c1 = Double.valueOf(t1.getText()).doubleValue();
c2 = Double.valueOf(t2.getText()).doubleValue();
repaint();
}
if(ev.getSource()==tasto3){
anima=true;
start();
}
}
public void paint(Graphics g){
double x,y,x1,y1;
int ic;
for (int i1=0; i1<500; i1++){
og.setColor(c[i1]);
og.fillRect(i1, 50, 1, 20);
}
for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
x = Left +i*((Right-Left)/200);
y = Bottom + j*((Top-Bottom)/200);
for(ic=0;ic < c.length;ic++){
x1 = x*x - y*y +c1;
y1 = 2*x*y +c2;
if((x1*x1+y1*y1)>4.)break;
x=x1;y=y1;
}
if(ic ==c.length)og.setColor(Color.black);
else og.setColor(c[ic]);
og.fillRect(i,j+100,1,1);
}
}
og.drawImage(mandel,250,150,100,100,this);
g.drawImage(o,0,0,this);
}
}
Venerdi' 29 Giugno 2001 : 14 59 5 -