Public paste
Undefined
By: Guest | Date: Jul 1 2010 07:35 | Format: None | Expires: never | Size: 1.32 KB | Hits: 841

  1. import java.applet.*;
  2. import java.awt.*;
  3.  
  4. public class BallApplet extends Applet implements Runnable {
  5.        
  6.         int x_pos = 10;
  7.         int y_pos = 100;
  8.         int radius = 20;
  9.         int peter =1;
  10.        
  11.         public void init() {
  12.         }
  13.  
  14.         public void start() {
  15.                 // Schaffen eines neuen Threads, in dem das Spiel läuft
  16.                 Thread th = new Thread(this);
  17.                 // Starten des Threads
  18.                 th.start();
  19.         }
  20.  
  21.         public void stop() {
  22.         }
  23.  
  24.         public void destroy() {
  25.         }
  26.  
  27.         public void run() {
  28.                 // Erniedrigen der ThreadPriority
  29.                 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  30.  
  31.                 // Solange true ist läuft der Thread weiter
  32.                 while (true) {
  33.                         // Verändern der X - Koordinate des Balles
  34.                         x_pos +=peter;
  35.  
  36.                         if(x_pos == 400) {
  37.                                 peter= -1;
  38.                         }
  39.                         // Neuzeichnen des Applets
  40.                         repaint();
  41.  
  42.                         try {
  43.                                 // Stoppen des Threads für in Klammern angegebene Millisekunden
  44.                                 Thread.sleep(20);
  45.                         } catch (InterruptedException ex) {
  46.                                 // do nothing
  47.                         }
  48.  
  49.                         // Zurücksetzen der ThreadPriority auf Maximalwert
  50.                         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  51.                 }
  52.         }
  53.  
  54.         public void paint (Graphics g)
  55.         {
  56.         // Setzten der Zeichenfarbe auf Rot
  57.         g.setColor (Color.red);
  58.  
  59.         // Zeichen eines gefüllten Kreises
  60.         g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
  61.         }
  62.        
  63.         public void update(Graphics g)
  64.         {
  65.         paint(g);
  66.         }
  67.  
  68. }