import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.ListIterator; import java.util.Vector; public class artapp2 extends Applet implements MouseListener { Image buffer = null, buffer2 = null, tbuffer; boolean run = false, running = false; Canvas c; Thread t; @Override public void init(){ buffer = createImage(400, 400); buffer2 = createImage(400, 400); resize(400, 400); addMouseListener(this); ShapeColor sc = new ShapeColor(); t = new Thread(c = new Canvas(this)); t.start(); this.repaint(); running = true; } @Override public void stop(){ run = false; } @Override public void start(){ run = true; } @Override public void destroy(){ run = false; try{Thread.sleep(100);} catch (InterruptedException e){} System.exit(0); } @Override public void paint(Graphics g){ if (buffer != null){ g.drawImage(buffer, 0, 0, null); g.setColor(Color.BLACK); g.drawRect(0, 0, 399, 399); } } public void swap(){ buffer.getGraphics().drawImage(buffer2, 0, 0, null); this.repaint(); } public boolean running(){ return run; } public Graphics getG(){ return buffer2.getGraphics(); } public void mouseClicked(MouseEvent e) { run = true; //c.click(e.getX(), e.getY()); if (!running){ t.start(); this.repaint(); running = true; } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } @Override public String getAppletInfo(){ return "Test applit crated by Loren Harris"; } } class Canvas implements Runnable{ artapp2 win; Shape shape = new Shape(null, null); boolean running = false; public Canvas(artapp2 w){ win = w; } public void run(){ Graphics g = win.getG(); long start, sleep; while(true){ start = System.currentTimeMillis(); if (win.running()){ running = true; g.setColor(Color.BLACK); g.fillRect(0, 0, 400, 400); if (shape.tick() == null) break; shape.draw(g); running = false; win.swap(); } sleep = 1000 - (System.currentTimeMillis() - start); if (sleep > 0){ try{Thread.sleep(100);} catch (InterruptedException e){} } } } public void click(int x, int y){ while(running){ try{Thread.sleep(10);} catch (InterruptedException e){} } //shaters.add(new Shater(x, y)); //shape = new Shape(null, null); //this.run(); } } class Shape{ Vector points, children = new Vector(); Shape parent = null; int gen = 1; public Shape(Shape parent, Vector points){ this.parent = parent; if (parent != null){ this.gen = parent.gen+1; this.points = points; } else { this.points = new Vector(); this.points.add(new Point(50+(int)(Math.random()*10), 50+(int)(Math.random()*10))); this.points.add(new Point(350+(int)(Math.random()*10), 50+(int)(Math.random()*10))); this.points.add(new Point(350+(int)(Math.random()*10), 350+(int)(Math.random()*10))); this.points.add(new Point(50+(int)(Math.random()*10), 350+(int)(Math.random()*10))); } } public Shape tick(){ if (gen > 10) return null; if (children.size() != 0){ ListIterator li = children.listIterator(); int count = 0; while(li.hasNext()){ if (((Shape)li.next()).tick() == null) count++; } if (count == children.size()) return null; } else { Point p1 = (Point)points.get(0); Point p2 = (Point)points.get(1); if (Math.abs(p1.x-p2.x) <= 2) return null; Point p3 = (Point)points.get(2); Point p4 = (Point)points.get(3); double mult = Math.random()/5+.4; Point p5 = new Point((int)((p1.x-p2.x)*mult+p2.x), (int)((p1.y-p2.y)*mult+p2.y)); mult = Math.random()/5+.4; Point p6 = new Point((int)((p2.x-p3.x)*mult+p3.x), (int)((p2.y-p3.y)*mult+p3.y)); mult = Math.random()/5+.4; Point p7 = new Point((int)((p3.x-p4.x)*mult+p4.x), (int)((p3.y-p4.y)*mult+p4.y)); mult = Math.random()/5+.4; Point p8 = new Point((int)((p4.x-p1.x)*mult+p1.x), (int)((p4.y-p1.y)*mult+p1.y)); double a1 = (p5.y-p7.y)/(p5.x-p7.x), b1 = -p5.x*a1+p5.y, a2 = (p6.y-p8.y)/(p6.x-p8.x), b2 = -p6.x*a2+p6.y; Point p9 = new Point((int)((b1-b2)/(a2-a1)), (int)(a2*(b1-b2)/(a2-a1)+b2)); if (p9.x > 400 || p9.y > 400 || p9.x <= 0 || p9.y <= 0) return null; Vector v = new Vector(); v.add(p1); v.add(p5); v.add(p9); v.add(p8); children.add(new Shape(this, v)); v = new Vector(); v.add(p5); v.add(p2); v.add(p6); v.add(p9); children.add(new Shape(this, v)); v = new Vector(); v.add(p9); v.add(p6); v.add(p3); v.add(p7); children.add(new Shape(this, v)); v = new Vector(); v.add(p8); v.add(p9); v.add(p7); v.add(p4); children.add(new Shape(this, v)); } return this; } public Shape draw(Graphics g){ if (children.size() != 0){ ListIterator li = children.listIterator(); while(li.hasNext()){ ((Shape)li.next()).draw(g); } } g.setColor(ShapeColor.getColor(gen*25)); ListIterator li = points.listIterator(); Point first = null, prev = null, cur = null; while(li.hasNext()){ cur = (Point)li.next(); if (first == null) first = cur; if (prev != null) prev.draw(cur, g, gen); prev = cur; } first.draw(prev, g, gen); return this; } } class ShapeColor{ static Vector colors = new Vector(); static boolean dir = true; public ShapeColor(){ dir = Math.random() > .5; double m1 = Math.random()/5+.8, m2 = Math.random()/5+.8, m3 = Math.random()/5+.8; for (int i=0;i<255;i++){ double c1 = Math.random()/5+.8, c2 = Math.random()/5+.8, c3 = Math.random()/5+.8; colors.add(new Color((int)(i*c1*m1), (int)(i*c2*m2), (int)(i*c3*m3))); } } public static Color getColor(int i){ if (Math.random() < .3) i++; if (Math.random() < .3) i--; if (i > 254) i = 254; if (i < 0) i = 0; if (dir) i = 254-i; return (Color)colors.get(i); } } class Point{ public double x, y; public Point(Point p){ this.x = p.x; this.y = p.y; } public Point(int x, int y){ this.x = x; this.y = y; } public void draw(Graphics g){ g.drawLine((int)x, (int)y, (int)x, (int)y); } public void draw(Point p, Graphics g, int gen){ if (gen <= 1){ g.drawLine((int)p.x, (int)p.y+2, (int)x, (int)y+2); g.drawLine((int)p.x-2, (int)p.y, (int)x-2, (int)y); } if (gen <= 2){ g.drawLine((int)p.x, (int)p.y+2, (int)x, (int)y+2); g.drawLine((int)p.x-2, (int)p.y, (int)x-2, (int)y); } if (gen <= 3){ g.drawLine((int)p.x, (int)p.y+1, (int)x, (int)y+1); g.drawLine((int)p.x-1, (int)p.y, (int)x-1, (int)y); } if (gen <= 5){ g.drawLine((int)p.x, (int)p.y-1, (int)x, (int)y-1); g.drawLine((int)p.x+1, (int)p.y, (int)x+1, (int)y); } g.drawLine((int)p.x, (int)p.y, (int)x, (int)y); } }