OSDN Git Service

Imported GNU Classpath 0.90
[pf3gnuchains/gcc-fork.git] / libjava / classpath / examples / gnu / classpath / examples / awt / AnimationApplet.java
1 /* AnimationApplet.java -- An example of an old-style AWT applet
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
20
21 package gnu.classpath.examples.awt;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.applet.*;
26
27
28 /**
29  * AnimationApplet demonstrates the need for Xflush calls in
30  * GdkGraphics.c.  To see how this demo can fail in their absence,
31  * remove the contents of schedule_flush in GdkGraphics.c.  The
32  * animation will be so choppy that it is effectively stopped.
33  */
34 public class AnimationApplet
35   extends Applet
36   implements Runnable
37 {
38   boolean going = false;
39   Thread animThread = null;
40   int SPEED = 5;
41   int circleX = 0;
42   int circleY = 0;
43   int circleXold = 0;
44   int circleYold = 0;
45   int circleXdelta = 0;
46   int circleYdelta = 0;
47   int circleDiameter = 0;
48   int autoCircleX = 0;
49   int autoCircleY = 0;
50   int autoCircleXold = 0;
51   int autoCircleYold = 0;
52   int autoCircleXdelta = (int) (0.66 * SPEED);
53   int autoCircleYdelta = (int) (1.33 * SPEED);
54   int boardWidth = 0;
55   int boardHeight = 0;
56   int CIRCLE_SIZE = 5;
57
58   private Graphics appletGraphics;
59
60   // Update the circles' location values.
61   private void moveCircles()
62   {
63     circleX += circleXdelta;
64     if (circleX < 0)
65       circleX = 0;
66     if (circleX > boardWidth - circleDiameter)
67       circleX = boardWidth - circleDiameter;
68
69     circleY += circleYdelta;
70     if (circleY < 0)
71       circleY = 0;
72     if (circleY > boardHeight - circleDiameter)
73       circleY = boardHeight - circleDiameter;
74
75     autoCircleX += autoCircleXdelta;
76     if (autoCircleX < 0)
77       {
78         autoCircleX = 0;
79         autoCircleXdelta = -autoCircleXdelta;
80       }
81     if (autoCircleX > boardWidth - circleDiameter)
82       {
83         autoCircleX = boardWidth - circleDiameter;
84         autoCircleXdelta = -autoCircleXdelta;
85       }
86
87     autoCircleY += autoCircleYdelta;
88     if (autoCircleY < 0)
89       {
90         autoCircleY = 0;
91         autoCircleYdelta = -autoCircleYdelta;
92       }
93     if (autoCircleY > boardHeight - circleDiameter)
94       {
95         autoCircleY = boardHeight - circleDiameter;
96         autoCircleYdelta = -autoCircleYdelta;
97       }
98   }
99
100   // Clear the circle in the old location and paint a new circle
101   // in the new location.
102   private void paintCircles()
103   {
104     appletGraphics.setColor(Color.BLUE);
105     appletGraphics.fillOval(circleXold, circleYold, circleDiameter,
106                             circleDiameter);
107     appletGraphics.setColor(Color.YELLOW);
108     appletGraphics.fillOval(circleX, circleY, circleDiameter,
109                             circleDiameter);
110
111     appletGraphics.setColor(Color.BLUE);
112     appletGraphics.fillOval(autoCircleXold, autoCircleYold, circleDiameter,
113                             circleDiameter);
114     appletGraphics.setColor(Color.WHITE);
115     appletGraphics.fillOval(autoCircleX, autoCircleY, circleDiameter,
116                             circleDiameter);
117   }
118
119   // Override Applet.run.
120   public void run()
121   {
122     while (animThread != null)
123       {
124         circleXold = circleX;
125         circleYold = circleY;
126         autoCircleXold = autoCircleX;
127         autoCircleYold = autoCircleY;
128
129         moveCircles();
130         paintCircles();
131
132         if (animThread != null)
133           {
134             try
135               {
136                 Thread.sleep(20);
137               }
138             catch (InterruptedException e)
139               {
140               }
141           }
142       }
143   }
144
145   // Override Applet.paint.
146   public void paint(Graphics g)
147   {
148     boardWidth = this.getSize().width;
149     boardHeight = this.getSize().height;
150     g.setColor(Color.BLUE);
151     g.fillRect(0, 0, boardWidth, boardHeight);
152     if (!going)
153       {
154         FontMetrics fm = appletGraphics.getFontMetrics();
155         appletGraphics.setColor(Color.WHITE);
156         String msg = "Click to Start";
157         appletGraphics.drawString(msg,
158                                   (boardWidth >> 1) - (fm.stringWidth(msg) >> 1),
159                                   (boardHeight >> 1) - (fm.getHeight() >> 1));
160       }
161   }
162
163   // Override Applet.destroy.
164   public void destroy()
165   {
166     // animThread.stop();
167     animThread = null;
168   }
169
170   // Override Applet.init.
171   public void init()
172   {
173     boardWidth = this.getSize().width;
174     boardHeight = this.getSize().height;
175     going = false;
176     appletGraphics = getGraphics();
177     appletGraphics.setFont(new Font(appletGraphics.getFont().getName(),
178                                     Font.BOLD, 15));
179   }
180
181   // Override Component.preferredSize for when we're run standalone.
182   public Dimension preferredSize ()
183   {
184     return new Dimension (400, 400);
185   }
186
187   // Override Applet.handleEvent, the old-style AWT-event handler.
188   public boolean handleEvent(Event event)
189   {
190     switch (event.id)
191       {
192       case Event.MOUSE_DOWN:
193         if (!going)
194           {
195             going = true;
196             circleDiameter = boardWidth / CIRCLE_SIZE;
197             circleX = (boardWidth - circleDiameter) >> 1;
198             circleY = (boardHeight - circleDiameter) >> 1;
199             circleXdelta = 0;
200             circleYdelta = 0;
201             repaint();
202             animThread = new Thread(this);
203             animThread.start();
204           }
205         break;
206       case Event.KEY_ACTION:
207       case Event.KEY_PRESS:
208         if (event.key == Event.LEFT)
209           circleXdelta = -SPEED;
210         else if (event.key == Event.RIGHT)
211           circleXdelta = SPEED;
212         else if (event.key == Event.UP)
213           circleYdelta = -SPEED;
214         else if (event.key == Event.DOWN)
215           circleYdelta = SPEED;
216         break;
217       case Event.KEY_ACTION_RELEASE:
218       case Event.KEY_RELEASE:
219         if (event.key == Event.LEFT && circleXdelta < 0)
220           circleXdelta = 0;
221         else if (event.key == Event.RIGHT && circleXdelta > 0)
222           circleXdelta = 0;
223         else if (event.key == Event.UP && circleYdelta < 0)
224           circleYdelta = 0;
225         else if (event.key == Event.DOWN && circleYdelta > 0)
226           circleYdelta = 0;
227         break;
228       default:
229         break;
230       }
231     return false;
232   }
233 }