1 /* AnimationApplet.java -- An example of an old-style AWT applet
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
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)
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.
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
21 package gnu.classpath.examples.awt;
24 import java.awt.event.*;
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.
34 public class AnimationApplet
38 boolean going = false;
39 Thread animThread = null;
47 int circleDiameter = 0;
50 int autoCircleXold = 0;
51 int autoCircleYold = 0;
52 int autoCircleXdelta = (int) (0.66 * SPEED);
53 int autoCircleYdelta = (int) (1.33 * SPEED);
58 private Graphics appletGraphics;
60 // Update the circles' location values.
61 private void moveCircles()
63 circleX += circleXdelta;
66 if (circleX > boardWidth - circleDiameter)
67 circleX = boardWidth - circleDiameter;
69 circleY += circleYdelta;
72 if (circleY > boardHeight - circleDiameter)
73 circleY = boardHeight - circleDiameter;
75 autoCircleX += autoCircleXdelta;
79 autoCircleXdelta = -autoCircleXdelta;
81 if (autoCircleX > boardWidth - circleDiameter)
83 autoCircleX = boardWidth - circleDiameter;
84 autoCircleXdelta = -autoCircleXdelta;
87 autoCircleY += autoCircleYdelta;
91 autoCircleYdelta = -autoCircleYdelta;
93 if (autoCircleY > boardHeight - circleDiameter)
95 autoCircleY = boardHeight - circleDiameter;
96 autoCircleYdelta = -autoCircleYdelta;
100 // Clear the circle in the old location and paint a new circle
101 // in the new location.
102 private void paintCircles()
104 appletGraphics.setColor(Color.BLUE);
105 appletGraphics.fillOval(circleXold, circleYold, circleDiameter,
107 appletGraphics.setColor(Color.YELLOW);
108 appletGraphics.fillOval(circleX, circleY, circleDiameter,
111 appletGraphics.setColor(Color.BLUE);
112 appletGraphics.fillOval(autoCircleXold, autoCircleYold, circleDiameter,
114 appletGraphics.setColor(Color.WHITE);
115 appletGraphics.fillOval(autoCircleX, autoCircleY, circleDiameter,
119 // Override Applet.run.
122 while (animThread != null)
124 circleXold = circleX;
125 circleYold = circleY;
126 autoCircleXold = autoCircleX;
127 autoCircleYold = autoCircleY;
132 if (animThread != null)
138 catch (InterruptedException e)
145 // Override Applet.paint.
146 public void paint(Graphics g)
148 boardWidth = this.getSize().width;
149 boardHeight = this.getSize().height;
150 g.setColor(Color.BLUE);
151 g.fillRect(0, 0, boardWidth, boardHeight);
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));
163 // Override Applet.destroy.
164 public void destroy()
166 // animThread.stop();
170 // Override Applet.init.
173 boardWidth = this.getSize().width;
174 boardHeight = this.getSize().height;
176 appletGraphics = getGraphics();
177 appletGraphics.setFont(new Font(appletGraphics.getFont().getName(),
181 // Override Component.preferredSize for when we're run standalone.
182 public Dimension preferredSize ()
184 return new Dimension (400, 400);
187 // Override Applet.handleEvent, the old-style AWT-event handler.
188 public boolean handleEvent(Event event)
192 case Event.MOUSE_DOWN:
196 circleDiameter = boardWidth / CIRCLE_SIZE;
197 circleX = (boardWidth - circleDiameter) >> 1;
198 circleY = (boardHeight - circleDiameter) >> 1;
202 animThread = new Thread(this);
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;
217 case Event.KEY_ACTION_RELEASE:
218 case Event.KEY_RELEASE:
219 if (event.key == Event.LEFT && circleXdelta < 0)
221 else if (event.key == Event.RIGHT && circleXdelta > 0)
223 else if (event.key == Event.UP && circleYdelta < 0)
225 else if (event.key == Event.DOWN && circleYdelta > 0)