Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 3.39 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5.  
  6. public class Main extends Applet implements Runnable {
  7.  
  8.         private static final long serialVersionUID = 5138339314543152159L;
  9.        
  10.         private ArrayList<Ball> ballList = new ArrayList<Ball>();
  11.        
  12.         int appletsize_x = 380;
  13.         int appletsize_y = 380;
  14.  
  15.         private Image dbImage;
  16.         private Graphics dbg;
  17.  
  18.         private Image backgroundImage = null;
  19.         private AudioClip bounceSound;
  20.        
  21.         private void addBalls(int i) {
  22.                 if(i>0) {
  23.                         for(int i2=0;i2<i;i2++) {
  24.                                 ballList.add(new Ball(appletsize_x, appletsize_y, bounceSound));
  25.                         }
  26.                 } else {
  27.                         System.out.println("Bad argument: " + i + "\n\"i\" must be a positive int");
  28.                 }
  29.  
  30.         }
  31.        
  32.         public void init() {
  33.                 this.addKeyListener(new KeyListener() {
  34.                        
  35.                         @Override
  36.                         public void keyPressed(KeyEvent e) {
  37.                                
  38.                         }
  39.  
  40.                         @Override
  41.                         public void keyReleased(KeyEvent e) {
  42.                                
  43.                         }
  44.  
  45.                         @Override
  46.                         public void keyTyped(KeyEvent e) {
  47.                                
  48.                         }
  49.                 });
  50.                
  51.                 this.addMouseListener(new MouseListener() {
  52.  
  53.                         @Override
  54.                         public void mouseClicked(MouseEvent arg0) {
  55.                                 // TODO Auto-generated method stub
  56.                                
  57.                         }
  58.  
  59.                         @Override
  60.                         public void mouseEntered(MouseEvent arg0) {
  61.                                 // TODO Auto-generated method stub
  62.                                
  63.                         }
  64.  
  65.                         @Override
  66.                         public void mouseExited(MouseEvent arg0) {
  67.                                 // TODO Auto-generated method stub
  68.                                
  69.                         }
  70.  
  71.                         @Override
  72.                         public void mousePressed(MouseEvent arg0) {
  73.                                 // TODO Auto-generated method stub
  74.                                
  75.                         }
  76.  
  77.                         @Override
  78.                         public void mouseReleased(MouseEvent arg0) {
  79.                                 // TODO Auto-generated method stub
  80.                                
  81.                         }
  82.                        
  83.                 });
  84.                
  85.                 this.addMouseMotionListener(new MouseMotionListener() {
  86.  
  87.                         @Override
  88.                         public void mouseDragged(MouseEvent arg0) {
  89.                                 // TODO Auto-generated method stub
  90.                                
  91.                         }
  92.  
  93.                         @Override
  94.                         public void mouseMoved(MouseEvent arg0) {
  95.                                 // TODO Auto-generated method stub
  96.                                
  97.                         }
  98.                        
  99.                 });
  100.                
  101.                
  102.         backgroundImage = getImage(getCodeBase(), "image/land.gif");
  103.         bounceSound = getAudioClip(getCodeBase(), "sound/bounce.au");
  104.        
  105.         addBalls(3);
  106.         }
  107.  
  108.         public void start () {
  109.                 Thread th = new Thread(this);
  110.                 th.start ();
  111.                 Thread gravity = new Thread() {
  112.                         public void run() {
  113.                                 while(true) {
  114.                                         try {
  115.                                                 Thread.sleep(20);
  116.                                         } catch(InterruptedException ex) {}
  117.                                 }
  118.                         }
  119.                 };
  120.                 gravity.start();
  121.         }
  122.  
  123.         public void stop() {
  124.  
  125.         }
  126.  
  127.         public void destroy() {
  128.  
  129.         }
  130.  
  131.         public void run ()
  132.         {
  133.                 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  134.  
  135.                 while (true) {
  136.                         for(int i=0;i<(ballList.size());i++) {
  137.                                 ballList.get(i).move();
  138.                         }
  139.                        
  140.                         repaint();
  141.  
  142.                         try {
  143.                                 Thread.sleep (20);
  144.                         } catch (InterruptedException ex) {}
  145.                        
  146.                         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  147.                 }
  148.         }
  149.  
  150.         public void update (Graphics g) {
  151.                 if (dbImage == null) {
  152.                         dbImage = createImage (this.getSize().width, this.getSize().height);
  153.                         dbg = dbImage.getGraphics ();
  154.                 }
  155.  
  156.                 dbg.setColor (getBackground ());
  157.                 dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
  158.  
  159.                 dbg.setColor (getForeground());
  160.                 paint (dbg);
  161.  
  162.                 g.drawImage (dbImage, 0, 0, this);
  163.         }
  164.  
  165.         public void paint (Graphics g)
  166.         {
  167.                 System.out.println(backgroundImage);
  168.                 g.drawImage (backgroundImage, 0, 0, this);
  169.                 System.out.println(backgroundImage);
  170.                
  171.                 for(int i=0;i<(ballList.size());i++) {
  172.                         ballList.get(i).draw(g);
  173.                 }
  174.         }
  175. }