Java Fractal
Author |
Message |
HeavenAgain

|
Posted: Fri Jun 15, 2007 10:29 am Post subject: Java Fractal |
|
|
well, i dont see any java graphics being posted on this forum, so i'll share one of "mine"
its not that pretty and all, but i hope it will get people started thinking about recursions, and graphics
code: | import javax.swing.JFrame; //imports for the frame
import java.awt.Color; //imports the colour
import java.awt.Graphics; //imports the graphics
import java.util.Random; // imports the random class for random colour
public class SierpinskiSquare
{
public static void main (String[]args)
{
new SFrame(); // call to the frame, program
}
}
class SFrame extends JFrame
{
Graphics g; // graphics
Color c =Color.WHITE; // set colour to white to creat the blanks
Random r = new Random();
public SFrame()
{
setTitle("Sierpinski Square"); // name of the window/frame
setVisible(true); // true to show the window
setSize(500,500); // creats the frame, starting window visible true
// setResizable(false); //cannot resize the frame/window -- useless, since fratal base is on window size
setLocationRelativeTo(null); //center the frame/window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit window, omit this line to avoid annoying pop up, useless
// setBackground(Color.WHITE); //set background to white
}
public void paint(Graphics g)
{
this.g = g;
repaint(); // incase if the window size is change, then "refresh" page, useless since window size never change
drawSierpinskiSquare( 0, 0, getWidth(), getHeight()); // start recursion
}
private void drawSierpinskiSquare ( int x, int y ,int w, int h)
{
if (w>2 && h>2) //control when to stop
{
int nw=w/3, nh=h/3; // the new square's width and height
g.setColor(c); //random colour (default is white)
g.fillRect ( x+nw,y+nh,nw,nh); //draws and fill the square, bigger one
for (int k=0;k<9;k++) // inside the big square, divide it into 9 smaller one equally
if (k!=4) // if is not the middle square
{
c=new Color( r.nextInt(256), r.nextInt(256), r.nextInt(256)); //makes the new colour
int i=k/3, j=k%3; // dividing the 8 equal squares into another 9 smaller ones
drawSierpinskiSquare ( x+i*nw, y+j*nh, nw, nh ); // draws out the recursion
}
}
}
} |
feel free to put any comments on it
oh, and the logic behind it is here |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Prince Pwn
|
Posted: Sat Jun 16, 2007 2:55 pm Post subject: Re: Java Fractal |
|
|
That's pretty cool. It looks like a processor. |
|
|
|
|
 |
wtd
|
Posted: Sun Jun 17, 2007 3:10 am Post subject: Re: Java Fractal |
|
|
code: | import javax.swing.JFrame
import java.awt.{Color, Graphics}
import java.util.Random
import Color.WHITE
import JFrame.EXIT_ON_CLOSE
object SierpinskiSquare extends Application {
new SFrame
}
class SFrame extends JFrame("Sierpinski Square") {
var g: Graphics = null
var c = WHITE
val r = new Random
this setVisible true
this setSize (500, 500)
this setLocationRelativeTo null
this setDefaultCloseOperation EXIT_ON_CLOSE
override def paint(g: Graphics) {
this g = g
this repaint ()
this drawSierpinskiSquare (0, 0, getWidth, getHeight)
}
def drawSierpinskiSquare(x: Int, y: Int, w: Int, h: Int) {
def rand = r nextInt 256
if (w > 2 && h > 2) {
val nw = w / 3
val nh = h / 3
g setColor c
g fillRect (x + nw, y + nh, nw, nh)
for (val k <- 0 to 9 if k != 4) {
c = new Color(rand, rand, rand)
val i = k / 3
val j = k % 3
this drawSierpinskiSquare (x + i * nw, y + j * nh, nw, nh)
}
}
}
} |
Food for thought. |
|
|
|
|
 |
HeavenAgain

|
Posted: Sun Jun 17, 2007 9:29 am Post subject: RE:Java Fractal |
|
|
ummm wtd, what java language is that? looks neat |
|
|
|
|
 |
Clayton

|
Posted: Sun Jun 17, 2007 9:51 am Post subject: RE:Java Fractal |
|
|
That, unless I'm very much mistaken, is Scala |
|
|
|
|
 |
|
|