Public paste
Undefined
By: Guest | Date: Jul 1 2010 11:34 | Format: None | Expires: never | Size: 4.01 KB | Hits: 850

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.util.Random;
  4.  
  5. public class BallApplet extends Applet implements Runnable {
  6.    
  7.         Random rd = new Random();
  8.     int x1_pos = 250;
  9.     int trefferanzahl = 5;
  10.     int y1_pos=125;
  11.     int radius = 20;
  12.     int peter =1;
  13.     int peter2 = -1;
  14.     int balken_y = 0;
  15.     int xclick = 0;
  16.     int yclick = 0;
  17.     int appletsize_x = 500;
  18.     int appletsize_y = 250;
  19.     int ball_x_speed = 0;
  20.     int ball_y_speed = 0;
  21.     Image background;
  22.     private Image dbImage;
  23.     private Graphics dbg;
  24.     Button start;
  25.     Button Pause;
  26.  
  27.  
  28.     public void init() {
  29.         this.setLayout(null);
  30.         background = getImage(getCodeBase(), "bg.jpg");
  31.         start = new Button("Spiel starten");
  32.         Pause = new Button("Pause");
  33.         start.setBounds(250,125,80,50);
  34.         add(start);
  35.         add(Pause);
  36.        
  37.  
  38.     }
  39.     public boolean action(Event e, Object args){
  40.         if(e.target == start ) {
  41.                 ball_x_speed = 1+rd.nextInt(trefferanzahl);
  42.                 ball_y_speed = 1+rd.nextInt(trefferanzahl);
  43.                 start.setVisible(false);
  44.         }
  45.        
  46.                 return false;
  47.        
  48.     }
  49.  
  50.     public void start() {
  51.        
  52.         Thread th = new Thread(this);
  53.      
  54.         th.start();
  55.     }
  56.  
  57.     public void stop() {
  58.     }
  59.  
  60.     public void destroy() {
  61.     }
  62.     public boolean mouseMove (Event e, int x, int y)
  63.         {
  64.                
  65.                
  66.                 //System.out.println(""+x+" "+y);
  67.                 xclick = x;
  68.                 yclick = y;
  69.                 return true;
  70.         }
  71.    
  72.  
  73.     public void run() {
  74.        
  75.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  76.  
  77.        
  78.         while (true) {
  79.                
  80.    
  81.                
  82.                
  83.        
  84.                
  85.                    if(yclick+30 >= y1_pos && yclick-30 <= y1_pos && x1_pos >= 30 - radius && x1_pos <= 30 + radius){
  86.                 System.out.println("Ball getroffen!");
  87.                 trefferanzahl++;
  88.                 if (trefferanzahl >=10){ trefferanzahl = 5; }
  89.                
  90.                 ball_y_speed =1+rd.nextInt(trefferanzahl);
  91.                 ball_x_speed = 1+rd.nextInt(trefferanzahl);
  92.                    }
  93.                    
  94.                    if (x1_pos > appletsize_x - radius) {
  95.                    ball_x_speed = ball_x_speed* -1;
  96.                  
  97.                    }
  98.                    if (y1_pos > appletsize_y - radius) {
  99.                    ball_y_speed = ball_y_speed* -1;
  100.                  
  101.                    }
  102.                    if (x1_pos < radius){
  103.                            x1_pos = 250;
  104.                            y1_pos = 125;
  105.                            
  106.                    }
  107.                    
  108.                    
  109.                    if (y1_pos <radius){
  110.                            ball_y_speed = ball_y_speed* -1; }
  111.                    
  112.                         x1_pos = x1_pos + ball_x_speed;
  113.                         y1_pos = y1_pos + ball_y_speed;
  114.                
  115.        
  116.            
  117.             if (x1_pos <= radius){
  118.                 x1_pos = radius +2;
  119.                 ball_x_speed = ball_x_speed * -1;
  120.             }
  121.             if (y1_pos <= radius){
  122.                 y1_pos = radius +2;
  123.                 ball_y_speed = ball_y_speed * -1;
  124.             }
  125.                
  126.                        
  127.              
  128.            
  129.      
  130.             repaint();
  131.  
  132.             try {
  133.              
  134.                 Thread.sleep(20);
  135.             } catch (InterruptedException ex) {
  136.                
  137.             }
  138.  
  139.          
  140.             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  141.         }
  142.     }
  143.  
  144.     public void paint (Graphics g)
  145.     {
  146.         g.drawImage(background,0,0,this);
  147.  
  148.  
  149.     g.setColor (Color.black);
  150.     g.fillOval(x1_pos - radius, y1_pos - radius, 2 * radius, 2 * radius);
  151.    
  152.     g.setColor(Color.black);
  153.     g.fillRect(30,yclick,20,60);
  154.     }
  155.  
  156.     public void update(Graphics g) {
  157.         if (dbImage == null) {
  158.             dbImage = createImage(this.getSize().width, this.getSize().height);
  159.             dbg = dbImage.getGraphics();
  160.         }
  161.         dbg.setColor(getBackground());
  162.         dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
  163.  
  164.         dbg.setColor(getForeground());
  165.         paint(dbg);
  166.  
  167.         g.drawImage(dbImage, 0, 0, this);
  168.     }
  169.  
  170.  
  171. }