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 ();
}
}
Related:
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|