home *** CD-ROM | disk | FTP | other *** search
/ PC Active 2001 July/August / PCA1307.ISO / EXTRA / cursus / Monkey.java < prev    next >
Encoding:
Java Source  |  2001-05-30  |  3.0 KB  |  123 lines

  1. package nl.pcactive.gfx;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.applet.*;
  6. import java.net.URL;
  7.  
  8. /**
  9.  * Title:        GFX
  10.  * Description:
  11.  * Copyright:    Copyright (c) 2001
  12.  * Company:      Pc-Active
  13.  * @author Benny Lootens
  14.  * @version 1.0
  15.  */
  16.  
  17. public class Monkey extends Applet {
  18.   private boolean isStandalone = false;
  19.   private URL bgURL = null;
  20.   private Image bgIMG = null;
  21.   private Image[] sprite = new Image[4];
  22.   private MediaTracker mt = new MediaTracker(this);
  23.  
  24.   /**Get a parameter value*/
  25.   public String getParameter(String key, String def) {
  26.     return isStandalone ? System.getProperty(key, def) :
  27.       (getParameter(key) != null ? getParameter(key) : def);
  28.   }
  29.  
  30.   /**Initialize the applet*/
  31.   public void init() {
  32.     try {
  33.       jbInit();
  34.     }
  35.     catch(Exception e) {
  36.       e.printStackTrace();
  37.     }
  38.   }
  39.   /**Component initialization*/
  40.   private void jbInit() throws Exception {
  41.     bgURL = new URL(getCodeBase()+"\\shots\\bg.gif");
  42.     bgIMG = getImage(bgURL);
  43.  
  44.     for (int i=0; i<4; i++) {
  45.       URL url = new URL(getCodeBase()+ "\\shots\\"+(i+1)+".gif");
  46.       sprite[i] = getImage(url);
  47.  
  48.       System.out.println(getCodeBase());
  49.       mt.addImage(sprite[i],i);
  50.     }
  51.     mt.addImage(bgIMG,4);
  52.     this.setBackground(Color.blue);
  53.   }
  54.   /**Start the applet*/
  55.   public void start() {
  56.   }
  57.   /**Stop the applet*/
  58.   public void stop() {
  59.   }
  60.   /**Destroy the applet*/
  61.   public void destroy() {
  62.   }
  63.   /**Get Applet information*/
  64.   public String getAppletInfo() {
  65.     return "Aap";
  66.   }
  67.   /**Get parameter info*/
  68.   public String[][] getParameterInfo() {
  69.     return null;
  70.   }
  71.  
  72.   public void paint(Graphics g) {
  73.     try {
  74.       for (int i=0; i<5; i++)
  75.         mt.waitForID(i);
  76.     }
  77.     catch (InterruptedException e) {
  78.       return;
  79.     }
  80.  
  81.     g.drawImage(bgIMG,0,0,getBounds().width,getBounds().height,Color.white,this);
  82.  
  83.     for (int x=0; x<400; x++) {
  84.       double angle=((double)x) / 20;
  85.       int y = (int) (Math.abs(Math.sin(angle))*80);
  86.       g.drawImage(sprite[0],x,80-y,this);
  87.       delay(25);
  88.     }
  89.     this.setBackground(Color.yellow);
  90.     System.exit(0);
  91.   }
  92.  
  93.    private void delay(int millis) {
  94.     try { Thread.sleep(millis); } catch(Exception ignored) {}
  95.   }
  96.  
  97.   /**Main method*/
  98.   public static void main(String[] args) {
  99.     Monkey applet = new Monkey();
  100.     applet.isStandalone = true;
  101.     Frame frame;
  102.     frame = new Frame() {
  103.       protected void processWindowEvent(WindowEvent e) {
  104.         super.processWindowEvent(e);
  105.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  106.           System.exit(0);
  107.         }
  108.       }
  109.       public synchronized void setTitle(String title) {
  110.         super.setTitle(title);
  111.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  112.       }
  113.     };
  114.     frame.setTitle("Aap");
  115.     frame.add(applet, BorderLayout.CENTER);
  116.     applet.init();
  117.     applet.start();
  118.     frame.setBounds(0,0,177,160);
  119.     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  120.     //frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
  121.     frame.setVisible(true);
  122.   }
  123. }