home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-25 | 3.4 KB | 158 lines |
- import java.applet.*;
- import java.awt.*;
- import java.util.*;
-
- public class EX12D extends Applet
- {
- protected Panel p = new Panel();
- protected Button CircleButton = new Button("Stop Circles");
- protected Button SquareButton = new Button("Stop Squares");
- protected ThreadGroup Circles = new ThreadGroup("Circles");
- protected ThreadGroup Squares = new ThreadGroup("Squares");
-
- public void init()
- {
- setLayout(new BorderLayout());
-
- p.add(CircleButton);
- p.add(SquareButton);
- add("South", p);
- }
-
- public boolean action(Event evt, Object obj)
- {
- boolean result = false; // asume no action
-
- if ("Start Circles".equals(obj)) {
- Circles.resume();
- CircleButton.setLabel("Stop Circles");
-
- result = true;
- }
- else if ("Stop Circles".equals(obj)) {
- Circles.suspend();
- CircleButton.setLabel("Start Circles");
-
- result = true;
- }
- else if ("Start Squares".equals(obj)) {
- Squares.resume();
- SquareButton.setLabel("Stop Squares");
-
- result = true;
- }
- else if ("Stop Squares".equals(obj)) {
- Squares.suspend();
- SquareButton.setLabel("Start Squares");
-
- result = true;
- }
-
- return result;
- }
-
- public void start()
- {
- DrawThread.SetGraphics(getGraphics());
-
- // add two circle threads
- for (int count = 1; count <= 2; count++)
- (new DrawCircleThread(Circles, size().width,
- size().height - p.size().height,
- count)).start();
-
- // add three square threads
- for (int count = 1; count <= 3; count++)
- (new DrawSquareThread(Squares, size().width,
- size().height - p.size().height,
- count)).start();
- }
-
- public void stop()
- {
- Circles.stop();
- Squares.stop();
- }
- }
-
- class DrawThread extends Thread
- {
- protected static Graphics g;
- protected static Random r = new Random();
- protected int totalWidth, totalHeight;
- protected int count;
-
- public static void SetGraphics(Graphics _g)
- {
- g = _g;
- }
-
- public DrawThread(ThreadGroup group, int totalWidth,
- int totalHeight, int count)
- {
- super(group, Integer.toString(count));
-
- this.totalWidth = totalWidth;
- this.totalHeight = totalHeight;
- this.count = count;
- }
-
- public void run()
- {
- while (true) {
- try {
- sleep(GetNextRandom(4, 1) * 1000);
- }
- catch (InterruptedException e) {}
-
- if (g != null)
- Draw(g, GetNextRandom(totalWidth, 0),
- GetNextRandom(totalHeight, 0));
- }
- }
-
- protected synchronized int GetNextRandom(int range, int offset)
- {
- return (int)(r.nextDouble() * (double)range) + offset;
- }
-
- protected void Draw(Graphics g, int x, int y)
- {
- }
- }
-
- class DrawCircleThread extends DrawThread
- {
- protected final static int graphicsSize = 30;
-
- public DrawCircleThread(ThreadGroup group, int totalWidth,
- int totalHeight, int count)
- {
- super(group, totalWidth - graphicsSize,
- totalHeight - graphicsSize, count);
- }
-
- protected synchronized void Draw(Graphics g, int x, int y)
- {
- g.setColor(Color.red);
- g.fillOval(x, y, graphicsSize / count, graphicsSize / count);
- }
- }
-
- class DrawSquareThread extends DrawThread
- {
- protected final static int graphicsSize = 5;
-
- public DrawSquareThread(ThreadGroup group, int totalWidth,
- int totalHeight, int count)
- {
- super(group, totalWidth - graphicsSize,
- totalHeight - graphicsSize, count);
- }
-
- protected synchronized void Draw(Graphics g, int x, int y)
- {
- g.setColor(Color.green);
- g.fillRect(x, y, graphicsSize * count, graphicsSize * count);
- }
- }