OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [examples/] [gnu/] [classpath/] [examples/] [awt/] [AnimationApplet.java] - Blame information for rev 781

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 781 jeremybenn
/* 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.applet.*;
25
 
26
 
27
/**
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.
32
 */
33
public class AnimationApplet
34
  extends Applet
35
  implements Runnable
36
{
37
  boolean going = false;
38
  Thread animThread = null;
39
  int SPEED = 5;
40
  int circleX = 0;
41
  int circleY = 0;
42
  int circleXold = 0;
43
  int circleYold = 0;
44
  int circleXdelta = 0;
45
  int circleYdelta = 0;
46
  int circleDiameter = 0;
47
  int autoCircleX = 0;
48
  int autoCircleY = 0;
49
  int autoCircleXold = 0;
50
  int autoCircleYold = 0;
51
  int autoCircleXdelta = (int) (0.66 * SPEED);
52
  int autoCircleYdelta = (int) (1.33 * SPEED);
53
  int boardWidth = 0;
54
  int boardHeight = 0;
55
  int CIRCLE_SIZE = 5;
56
 
57
  private Graphics appletGraphics;
58
 
59
  // Update the circles' location values.
60
  private void moveCircles()
61
  {
62
    circleX += circleXdelta;
63
    if (circleX < 0)
64
      circleX = 0;
65
    if (circleX > boardWidth - circleDiameter)
66
      circleX = boardWidth - circleDiameter;
67
 
68
    circleY += circleYdelta;
69
    if (circleY < 0)
70
      circleY = 0;
71
    if (circleY > boardHeight - circleDiameter)
72
      circleY = boardHeight - circleDiameter;
73
 
74
    autoCircleX += autoCircleXdelta;
75
    if (autoCircleX < 0)
76
      {
77
        autoCircleX = 0;
78
        autoCircleXdelta = -autoCircleXdelta;
79
      }
80
    if (autoCircleX > boardWidth - circleDiameter)
81
      {
82
        autoCircleX = boardWidth - circleDiameter;
83
        autoCircleXdelta = -autoCircleXdelta;
84
      }
85
 
86
    autoCircleY += autoCircleYdelta;
87
    if (autoCircleY < 0)
88
      {
89
        autoCircleY = 0;
90
        autoCircleYdelta = -autoCircleYdelta;
91
      }
92
    if (autoCircleY > boardHeight - circleDiameter)
93
      {
94
        autoCircleY = boardHeight - circleDiameter;
95
        autoCircleYdelta = -autoCircleYdelta;
96
      }
97
  }
98
 
99
  // Clear the circle in the old location and paint a new circle
100
  // in the new location.
101
  private void paintCircles()
102
  {
103
    appletGraphics.setColor(Color.BLUE);
104
    appletGraphics.fillOval(circleXold, circleYold, circleDiameter,
105
                            circleDiameter);
106
    appletGraphics.setColor(Color.YELLOW);
107
    appletGraphics.fillOval(circleX, circleY, circleDiameter,
108
                            circleDiameter);
109
 
110
    appletGraphics.setColor(Color.BLUE);
111
    appletGraphics.fillOval(autoCircleXold, autoCircleYold, circleDiameter,
112
                            circleDiameter);
113
    appletGraphics.setColor(Color.WHITE);
114
    appletGraphics.fillOval(autoCircleX, autoCircleY, circleDiameter,
115
                            circleDiameter);
116
  }
117
 
118
  // Override Applet.run.
119
  public void run()
120
  {
121
    while (animThread != null)
122
      {
123
        circleXold = circleX;
124
        circleYold = circleY;
125
        autoCircleXold = autoCircleX;
126
        autoCircleYold = autoCircleY;
127
 
128
        moveCircles();
129
        paintCircles();
130
 
131
        if (animThread != null)
132
          {
133
            try
134
              {
135
                Thread.sleep(20);
136
              }
137
            catch (InterruptedException e)
138
              {
139
              }
140
          }
141
      }
142
  }
143
 
144
  // Override Applet.paint.
145
  public void paint(Graphics g)
146
  {
147
    boardWidth = this.getSize().width;
148
    boardHeight = this.getSize().height;
149
    g.setColor(Color.BLUE);
150
    g.fillRect(0, 0, boardWidth, boardHeight);
151
    if (!going)
152
      {
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));
159
      }
160
  }
161
 
162
  // Override Applet.destroy.
163
  public void destroy()
164
  {
165
    // animThread.stop();
166
    animThread = null;
167
  }
168
 
169
  // Override Applet.init.
170
  public void init()
171
  {
172
    boardWidth = this.getSize().width;
173
    boardHeight = this.getSize().height;
174
    going = false;
175
    appletGraphics = getGraphics();
176
    appletGraphics.setFont(new Font(appletGraphics.getFont().getName(),
177
                                    Font.BOLD, 15));
178
  }
179
 
180
  // Override Component.preferredSize for when we're run standalone.
181
  public Dimension preferredSize ()
182
  {
183
    return new Dimension (400, 400);
184
  }
185
 
186
  // Override Applet.handleEvent, the old-style AWT-event handler.
187
  public boolean handleEvent(Event event)
188
  {
189
    switch (event.id)
190
      {
191
      case Event.MOUSE_DOWN:
192
        if (!going)
193
          {
194
            going = true;
195
            circleDiameter = boardWidth / CIRCLE_SIZE;
196
            circleX = (boardWidth - circleDiameter) >> 1;
197
            circleY = (boardHeight - circleDiameter) >> 1;
198
            circleXdelta = 0;
199
            circleYdelta = 0;
200
            repaint();
201
            animThread = new Thread(this);
202
            animThread.start();
203
          }
204
        break;
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;
215
        break;
216
      case Event.KEY_ACTION_RELEASE:
217
      case Event.KEY_RELEASE:
218
        if (event.key == Event.LEFT && circleXdelta < 0)
219
          circleXdelta = 0;
220
        else if (event.key == Event.RIGHT && circleXdelta > 0)
221
          circleXdelta = 0;
222
        else if (event.key == Event.UP && circleYdelta < 0)
223
          circleYdelta = 0;
224
        else if (event.key == Event.DOWN && circleYdelta > 0)
225
          circleYdelta = 0;
226
        break;
227
      default:
228
        break;
229
      }
230
    return false;
231
  }
232
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.