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;
28 * AnimationApplet demonstrates the need for Xflush calls in
29 * GdkGraphics.c. To see how this demo can fail in their absence,
30 * remove the contents of schedule_flush in GdkGraphics.c. The
31 * animation will be so choppy that it is effectively stopped.
33 public class AnimationApplet
37 boolean going = false;
38 Thread animThread = null;
46 int circleDiameter = 0;
49 int autoCircleXold = 0;
50 int autoCircleYold = 0;
51 int autoCircleXdelta = (int) (0.66 * SPEED);
52 int autoCircleYdelta = (int) (1.33 * SPEED);
57 private Graphics appletGraphics;
59 // Update the circles' location values.
60 private void moveCircles()
62 circleX += circleXdelta;
65 if (circleX > boardWidth - circleDiameter)
66 circleX = boardWidth - circleDiameter;
68 circleY += circleYdelta;
71 if (circleY > boardHeight - circleDiameter)
72 circleY = boardHeight - circleDiameter;
74 autoCircleX += autoCircleXdelta;
78 autoCircleXdelta = -autoCircleXdelta;
80 if (autoCircleX > boardWidth - circleDiameter)
82 autoCircleX = boardWidth - circleDiameter;
83 autoCircleXdelta = -autoCircleXdelta;
86 autoCircleY += autoCircleYdelta;
90 autoCircleYdelta = -autoCircleYdelta;
92 if (autoCircleY > boardHeight - circleDiameter)
94 autoCircleY = boardHeight - circleDiameter;
95 autoCircleYdelta = -autoCircleYdelta;
99 // Clear the circle in the old location and paint a new circle
100 // in the new location.
101 private void paintCircles()
103 appletGraphics.setColor(Color.BLUE);
104 appletGraphics.fillOval(circleXold, circleYold, circleDiameter,
106 appletGraphics.setColor(Color.YELLOW);
107 appletGraphics.fillOval(circleX, circleY, circleDiameter,
110 appletGraphics.setColor(Color.BLUE);
111 appletGraphics.fillOval(autoCircleXold, autoCircleYold, circleDiameter,
113 appletGraphics.setColor(Color.WHITE);
114 appletGraphics.fillOval(autoCircleX, autoCircleY, circleDiameter,
118 // Override Applet.run.
121 while (animThread != null)
123 circleXold = circleX;
124 circleYold = circleY;
125 autoCircleXold = autoCircleX;
126 autoCircleYold = autoCircleY;
131 if (animThread != null)
137 catch (InterruptedException e)
144 // Override Applet.paint.
145 public void paint(Graphics g)
147 boardWidth = this.getSize().width;
148 boardHeight = this.getSize().height;
149 g.setColor(Color.BLUE);
150 g.fillRect(0, 0, boardWidth, boardHeight);
153 FontMetrics fm = appletGraphics.getFontMetrics();
154 appletGraphics.setColor(Color.WHITE);
155 String msg = "Click to Start";
156 appletGraphics.drawString(msg,
157 (boardWidth >> 1) - (fm.stringWidth(msg) >> 1),
158 (boardHeight >> 1) - (fm.getHeight() >> 1));
162 // Override Applet.destroy.
163 public void destroy()
165 // animThread.stop();
169 // Override Applet.init.
172 boardWidth = this.getSize().width;
173 boardHeight = this.getSize().height;
175 appletGraphics = getGraphics();
176 appletGraphics.setFont(new Font(appletGraphics.getFont().getName(),
180 // Override Component.preferredSize for when we're run standalone.
181 public Dimension preferredSize ()
183 return new Dimension (400, 400);
186 // Override Applet.handleEvent, the old-style AWT-event handler.
187 public boolean handleEvent(Event event)
191 case Event.MOUSE_DOWN:
195 circleDiameter = boardWidth / CIRCLE_SIZE;
196 circleX = (boardWidth - circleDiameter) >> 1;
197 circleY = (boardHeight - circleDiameter) >> 1;
201 animThread = new Thread(this);
205 case Event.KEY_ACTION:
206 case Event.KEY_PRESS:
207 if (event.key == Event.LEFT)
208 circleXdelta = -SPEED;
209 else if (event.key == Event.RIGHT)
210 circleXdelta = SPEED;
211 else if (event.key == Event.UP)
212 circleYdelta = -SPEED;
213 else if (event.key == Event.DOWN)
214 circleYdelta = SPEED;
216 case Event.KEY_ACTION_RELEASE:
217 case Event.KEY_RELEASE:
218 if (event.key == Event.LEFT && circleXdelta < 0)
220 else if (event.key == Event.RIGHT && circleXdelta > 0)
222 else if (event.key == Event.UP && circleYdelta < 0)
224 else if (event.key == Event.DOWN && circleYdelta > 0)