codice:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import java.awt.Color;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class PixelCanvas extends Canvas {
public static Color colore(int c) {
Color f;
int i = c % 16;
if (i == 0) {
return f = new Color(66, 30, 15);
}
if (i == 1) {
return f = new Color(25, 7, 26);
}
if (i == 2) {
return f = new Color(9, 1, 47);
}
if (i == 3) {
return f = new Color(4, 4, 73);
}
if (i == 4) {
return f = new Color(0, 7, 100);
}
if (i == 5) {
return f = new Color(12, 44, 138);
}
if (i == 6) {
return f = new Color(24, 82, 177);
}
if (i == 7) {
return f = new Color(57, 125, 209);
}
if (i == 8) {
return f = new Color(134, 181, 229);
}
if (i == 9) {
return f = new Color(211, 236, 248);
}
if (i == 10) {
return f = new Color(241, 233, 191);
}
if (i == 11) {
return f = new Color(248, 201, 95);
}
if (i == 12) {
return f = new Color(255, 170, 0);
}
if (i == 13) {
return f = new Color(204, 128, 0);
}
if (i == 14) {
return f = new Color(153, 87, 0);
}
if (i == 15) {
return f = new Color(106, 52, 3);
}
return f = new Color(106, 52, 3);
}
public static Color colore2(int c) {
Color f;
if (c < 64) {
return f = new Color(c * 2, 0, 0);
} else if (c < 128) {
return f = new Color((((c - 64) * 128) / 126) + 128, 0, 0);
/* 0x0080 to 0x00C0 */
} else if (c < 256) {
return f = new Color((((c - 128) * 62) / 127) + 193, 0, 0);
/* 0x00C1 to 0x00FF */
} else if (c < 512) {
return f = new Color(255, (((c - 256) * 62) / 255) + 1, 0);
/* 0x01FF to 0x3FFF */
} else if (c < 1024) {
return f = new Color(255, (((c - 512) * 63) / 511) + 64, 0);
/* 0x40FF to 0x7FFF */
} else if (c < 2048) {
return f = new Color(255, (((c - 1024) * 63) / 1023) + 128, 0);
/* 0x80FF to 0xBFFF */
} else if (c < 4096) {
return f = new Color(255, (((c - 2048) * 63) / 2047) + 192, 0);
/* 0xC0FF to 0xFFFF */
}
return f = new Color(255, 255, 0);
}
public static double[] quad(double a, double b) {
double z = Math.pow(a, 2) - Math.pow(b, 2);
double z1 = 2 * a * b;
double z2[] = {z, z1};
return z2;
}
public int funz(double a, double b) {
double re = a, co = b;
a = 0;
b = 0;
int c = 0;
while (Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)) < 2 && c < ite) {
double v[] = quad(a, b);
double z1 = (v[0] + re);
double z2 = (v[1] + co);
a = z1;
b = z2;
c++;
}
return c;
}
Scanner in = new Scanner(System.in);
public final int ite = Integer.parseInt(in.next());
public static final int WIDTH = 2000;
public static final int HEIGHT = 2000;
int w = WIDTH / 2;
int h = HEIGHT / 2;
public void paint(Graphics g) {
super.paint(g);
Color [] colore=new Color[16];
colore[0]=new Color(66, 30, 15);
colore[1]=new Color(25, 7, 26);
colore[2]=new Color(9, 1, 47);
colore[3]=new Color(4, 4, 73);
colore[4]=new Color(0, 7, 100);
colore[5]=new Color(12, 44, 138);
colore[6]=new Color(24, 82, 177);
colore[7]=new Color(57, 125, 209);
colore[8]=new Color(134, 181, 229);
colore[9]=new Color(211, 236, 248);
colore[10]=new Color(241, 233, 191);
colore[11]=new Color(248, 201, 95);
colore[12]=new Color(255, 170, 0);
colore[13]=new Color(204, 128, 0);
colore[14]=new Color(153, 87, 0);
colore[15]=new Color(106, 52, 3);
Color bo;
for (int i = -h; i < h; i++) {
for (int y = -w; y < w; y++) {
double a = (double) i / (h / 2);
double b = (double) y / (w / 2);
int c = funz(a, b);
if (c == ite) {
g.drawLine(i + h, y + w, i + h, y + w);
g.setColor(Color.black);
} else {
g.setColor(colore[c%16]);
g.drawLine(i + h, y + w, i + h, y + w);
}
}
}
}
public PixelCanvas()
{
setSize(WIDTH,HEIGHT);
}
public static void main(String[] args) {
System.out.println("Inserire numero di iterzioni");
JPanel jp = new JPanel();
JScrollPane pane = new JScrollPane(new PixelCanvas(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JFrame frame = new JFrame();
frame.setSize(WIDTH, HEIGHT);
frame.setContentPane(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}