/* * WindTree.java * * Copyright (C) 1998 by Hiroshi Yuki. * All rights reserved. * * http://www.hyuki.com/jb/ */ import java.awt.*; import java.applet.Applet; public class WindTree extends Applet { Graphics offgra = null; Image offimg = null; double CENTERX = 200.0; double CENTERY = 380.0; double basex = 0.0; double basey = 0.0; double basedir = -Math.PI/2.0; double distance = 1.0; public void init() { resize(400, 400); } public void start() { if (offgra == null) { offimg = createImage(400, 400); offgra = offimg.getGraphics(); } } public boolean mouseMove(Event e, int x, int y) { if (y - CENTERY < 0) { Graphics g = getGraphics(); basex = x - CENTERX; basey = y - CENTERY; basedir = getDirection(x, y) + Math.PI/2.0; distance = Math.sqrt(basex * basex + basey * basey) / Math.sqrt(200*200 + 200*200); if (distance > 0) { update(g); } } return true; } public void update(Graphics g) { if (offgra != null) { offgra.setColor(new Color(198, 236, 244)); offgra.fillRect(0, 0, 400, 400); offgra.setColor(Color.black); paint(offgra); g.drawImage(offimg, 0, 0, this); } } public void paint(Graphics g) { FontMetrics fm = g.getFontMetrics(); g.setColor(Color.black); g.drawString("This is WindTree by Hiroshi Yuki.", 0, fm.getHeight()); g.drawString("http://www.hyuki.com", 0, fm.getHeight()*2); drawtree(g, 0.0, 0.0, -Math.PI/2, 50.0); } public void drawtree(Graphics g, double x, double y, double dir, double size) { // stem if (size > 4.0) { double nextx = x + (double)Math.cos(dir) * size; double nexty = y + (double)Math.sin(dir) * size; g.setColor(getColorBySize(size)); g.drawLine((int)(CENTERX + x), (int)(CENTERY + y), (int)(CENTERX + nextx), (int)(CENTERY + nexty)); // right drawtree(g, nextx, nexty, dir + 0.2/distance + basedir, size / 1.4); // left drawtree(g, nextx, nexty, dir - 0.2/distance + basedir, size / 1.4); } } public double getDirection(int x, int y) { if (x - CENTERX >= 0) { return Math.atan((double)(y - CENTERY) / (double)(x - CENTERX)); } else { return Math.atan((double)(CENTERX - x) / (double)(y - CENTERY)) - Math.PI/2.0; } } Color getColorBySize(double size) { if (size / 1.4 <= 4.0) { return new Color(245, 129, 208); } else if (size > 20) { return new Color(125, 68, 0); } else if (size < 10) { return new Color(10,239,0); } else { return new Color(98, 108, 0); } } }