home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue159 / files / copyjava.exe / ex1 / Example1.java < prev    next >
Encoding:
Java Source  |  1999-10-03  |  2.3 KB  |  85 lines

  1. /*
  2.  * @(#)Example1.java    1999/09/28
  3.  * 
  4.  * Copyright (c) 1999, David Griffiths. All Rights Reserved.
  5.  * 
  6.  * This software is the proprietary information of David Griffiths.
  7.  * This source code may not be published or redistributed without the 
  8.  * express permission of the author. 
  9.  * 
  10.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
  11.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  12.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  13.  * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
  14.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  15.  * THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17.  
  18. /**
  19.  * This first example creates a GIF animation of a coloured square in motion.
  20.  * @version    1.0 1999/09/28
  21.  * @author     David Griffiths 
  22.  */
  23.  
  24. import java.util.Hashtable;
  25. import java.io.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28.  
  29. public class Example1 {
  30.     private Frame frm;
  31.     private Animator animator;
  32.     private Graphics g;
  33.     private final static int WIDTH = 200;
  34.     private final static int HEIGHT = 200;
  35.     private final static int BORDER = 20;
  36.  
  37.     public static void main (String[] args) {
  38.         Example1 g = new Example1();
  39.         g.init();
  40.     }
  41.  
  42.     public void init() {
  43.         frm = new Frame();
  44.         frm.setLayout(new BorderLayout());
  45.         frm.setTitle("Processing...");
  46.         frm.setSize(WIDTH, HEIGHT + BORDER);
  47.         frm.addWindowListener(new WindowAdapter() {
  48.             public void windowClosing(WindowEvent e) {
  49.                 System.exit(0);
  50.             }
  51.             public void windowOpened(WindowEvent e) {
  52.                 createAnim();
  53.             }
  54.         });
  55.         frm.show();
  56.     }
  57.  
  58.     private void createAnim() {
  59.         Image imgMain = frm.createImage(WIDTH, HEIGHT);
  60.         Graphics gMain = imgMain.getGraphics();
  61.  
  62.         animator = Animator.getInstance("GIF", WIDTH, HEIGHT);
  63.         animator.setFPS(40);
  64.         g = frm.getGraphics();
  65.  
  66.         for (int i = 0; i < 100; i+= 3) {
  67.             gMain.setColor(Color.white);
  68.             gMain.fillRect(0, 0, WIDTH, HEIGHT);
  69.             gMain.setColor(new Color(2 * i, 0, 255 - 2 * i));
  70.             gMain.fillRect(i, i, 100, 100);
  71.             animator.addImage(imgMain, frm);
  72.             g.drawImage(imgMain, 0, BORDER, frm);
  73.         }
  74.  
  75.         try {
  76.             animator.getAnimation().serializeTo(new PrintStream(
  77.                 new FileOutputStream("ex1.gif")));
  78.         }
  79.         catch (IOException e) {
  80.         }
  81.  
  82.         System.exit(0);
  83.     }
  84. }
  85.