import java.awt.*; import java.applet.*; public class funkyCircApplet extends Applet{ private funkyThread fc; private MyCanvas can; private Scrollbar psb; private Label l, s; private Button up, down; private boolean run; public void init() { GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbl); int width, height, pieces, speed; width = Integer.parseInt(getParameter("width")); height = Integer.parseInt(getParameter("height")); if(getParameter("pieces") != null) pieces = Integer.parseInt(getParameter("pieces")); else pieces = 5; if(getParameter("speed") != null) speed = Integer.parseInt(getParameter("speed")); else speed = 0; l = new Label(pieces + ""); gbc.anchor = GridBagConstraints.WEST; gbl.setConstraints(l, gbc); add(l); psb = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 1, 32); psb.setValue(33 - pieces); gbc.gridy = 0; gbc.weightx = 1; gbc.fill = GridBagConstraints.VERTICAL; gbl.setConstraints(psb, gbc); add(psb); can = new MyCanvas(width - 100, height - 100, pieces, speed); can.resize(width - 100, height - 100); gbl.setConstraints(can, gbc); fc = new funkyThread(can); fc.start(); add(can); up = new Button("+"); down = new Button("-"); s = new Label(speed + ""); gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; gbl.setConstraints(up, gbc); add(up); gbl.setConstraints(s, gbc); add(s); gbl.setConstraints(down, gbc); add(down); } public void start() { fc.resume(); } public void stop() { fc.suspend(); } public boolean handleEvent(Event e) { if (e.id == Event.SCROLL_ABSOLUTE || e.id == Event.SCROLL_LINE_DOWN || e.id == Event.SCROLL_LINE_UP || e.id == Event.SCROLL_PAGE_DOWN || e.id == Event.SCROLL_PAGE_UP) { can.setPieces(33 - psb.getValue()); l.setText(33 - psb.getValue() + ""); can.repaint(); return true; } return super.handleEvent(e); } public boolean action(Event e, Object o) { if (o.equals("+")) { can.speedUp(); s.setText(can.getSpeed() + ""); } else if (o.equals("-")) { can.speedDown(); s.setText(can.getSpeed() + ""); } return super.action(e,o); } } class MyCanvas extends Canvas { private int width, height, pieces, speed; private int startAngle; private Graphics offgc; private Image offscreen; MyCanvas(int width, int height, int pieces, int speed) { this.width = width; this.height = height; this.pieces = pieces; this.speed = speed; this.startAngle = 0; } public void rotate() { this.startAngle += Math.abs(this.speed) / this.speed * 2; if (this.startAngle > (360 / pieces)) this.startAngle -= 360 / pieces; else if (this.startAngle < -(360 / pieces)) this.startAngle += 360 / pieces; } public void setPieces(int pieces) { this.pieces = pieces; } public int getSpeed() { return this.speed; } public void speedUp() { if (speed < 10) { this.speed++; } } public void speedDown() { if (speed > -10) { this.speed--; } } public void update(Graphics g) { if (offgc == null) { offscreen = createImage(width, height); offgc = offscreen.getGraphics(); } offgc.setColor(getBackground()); offgc.fillOval(0, 0, width, height); offgc.setColor(getForeground()); offgc.setColor(Color.red); offgc.fillOval(0, 0, width, height); offgc.setColor(Color.black); for(int a = 0; a < pieces; a++) offgc.fillArc(0, 0, width, height, startAngle + (int)(a * 360.0 / pieces), (int)(180.0 / pieces)); if (speed != 0) rotate(); g.drawImage(offscreen, 0, 0, null); } } class funkyThread extends Thread { private MyCanvas c; funkyThread(MyCanvas canvas) { this.c = canvas; } public void run(){ while(true){ c.repaint(); try { sleep(105 - Math.abs(c.getSpeed()) * 10); } catch(InterruptedException ie) {} } } }