|
The example draws several moving wadges on a circle |
Views: 2118
|
Thread Tools | Rate Thread |
#1
|
|||
|
|||
The example draws several moving wadges on a circle
The example draws several moving wadges on a circle
/* The example draws several moving wadges on a circle. These wedges move with different speeds */ import java.applet.*; import java.awt.*; import java.util.*; public class DrawMultWedges extends Applet { Wedges t [] = new Wedges [6]; Graphics g; public void init () { g = getGraphics (); for (int i = 0; i < t.length; i++) { if ( i < t.length / 2) t [i] = new Wedges (g, 10, 10 + i * 100, 100, 100, 100 * (i + 1)); else t [i] = new Wedges (g, 110, 10 + (i - 3) * 100, 100, 100, 100 * (i + 1)); t [i].start (); } } public void paint (Graphics g) { } } class Wedges implements Runnable { int x, y, width, height, interval; Graphics g; volatile boolean isSet = true; Thread t; public Wedges (Graphics g, int x, int y, int width, int height, int interval) { this.g = g; this.x = x; this.y = y; this.width = width; this.height = height; this.interval = interval; t = new Thread (this); } public void start () { t.start (); } public void run () { int i = 0; if (g == null) { System.out.println ("The reference to Graphics is " + g); return; } g.drawArc (x, y, width, height, 0, 360); // draw a circle while (isSet) { g.setColor (Color.white); g.fillArc (x, y, width, height, i - 45, 45); // erase a wedge g.setColor (Color.black); g.drawArc (x, y, width, height, i - 45, 45); // draw a circle sector g.fillArc (x, y, width, height, i, 45); // draw a wedge if (i != 315) i += 45; else i = 0; System.out.println (i); try { Thread.sleep (interval); } catch (InterruptedException ie) { System.out.println (ie.getMessage ()); } } g.dispose (); } } |