1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.TextField;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import sun.font.TextLabel;
public class CrossFractal extends JPanel
{
Graphics g;
static int r =180;
static int k;
int recursion;
int xc1=getWidth()/2;
int yc1=getHeight()/2;
public CrossFractal()
{
JFrame f=new JFrame();
String level=JOptionPane.showInputDialog("Enter the level of recursion (between 1 et 10)?");
recursion=Integer.parseInt(level);
while(recursion>10 | recursion<1)
{
String level1=JOptionPane.showInputDialog(null,"Please enter the correct level of recursion...");
recursion=Integer.parseInt(level1);
}
JLabel jl=new JLabel(" Cross Fractal");
jl.setOpaque(true);
jl.setBackground(Color.white);
jl.setForeground(Color.red);
jl.setPreferredSize(new Dimension(100,30));
f.setSize(1200,600);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setAlwaysOnTop(true);;
JPanel panel=new JPanel();
panel.setBackground(new Color(25,250,0));
f.add(panel);
f.setVisible(true);
panel.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
panel.add(jl);
panel.setPreferredSize(new Dimension(1200, 600));
drawCross( g, xc1, yc1, r, k);
}
public void drawCross( Graphics g, int xc1, int yc1, int r,int k)
{
if ( k == 0) return;
int xc2 = (int) xc1;
int yc2 = (int) (int) (yc1-r);
int xc3 = (int) (xc1+r);
int yc3 = yc1;
int xc4= xc1;
int yc4=(int) (yc1+r);
int xc5=(int) (xc1-r);
int yc5=(int)yc1;
if(k==recursion)
g.setColor(Color.yellow);
if(k==recursion-1)
g.setColor(Color.red);
if(k==recursion-2)
g.setColor(Color.green);
if(k==recursion-3)
g.setColor(Color.magenta);
if(k==recursion-4)
g.setColor(Color.BLUE);
if(k==recursion-5)
g.setColor(Color.GRAY);
if(k==recursion-6)
g.setColor(Color.orange);
if(k==recursion-7)
g.setColor(Color.white);
g.drawLine( xc1, yc1, xc2, yc2);
g.drawLine( xc1, yc1, xc3, yc3);
g.drawLine( xc1, yc1, xc4, yc4);
g.drawLine( xc1, yc1, xc5, yc5);
r= (int) (r*0.53);
drawCross(g, xc2, yc2, r,k-1);
drawCross(g, xc3, yc3,r , k - 1);
drawCross(g, xc4, yc4,r , k - 1);
drawCross(g, xc5, yc5,r , k - 1);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawCross(g,getWidth()/2,getHeight()/2,r,recursion);
}
public static void main(String[] args)
{
CrossFractal cf=new CrossFractal();
}
} |
Partager