Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

frame3

By: ace on Feb 24th, 2010  |  syntax: None  |  size: 3.56 KB  |  hits: 9  |  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.awt.*;
  2. import java.awt.geom.*;
  3. import java.lang.Number;
  4.  
  5.  
  6. /**
  7.  * Class BallDemo - provides two short demonstrations showing how to use the
  8.  * Canvas class.
  9.  *
  10.  * @author Michael Kolling and David J. Barnes
  11.  * @version 2006.03.30
  12.  */
  13.  
  14. public class BallDemo  
  15. {
  16.     private Canvas myCanvas;
  17.  
  18.     /**
  19.      * Create a BallDemo object. Creates a fresh canvas and makes it visible.
  20.      */
  21.     public BallDemo()
  22.     {
  23.         myCanvas = new Canvas("Ball Demo", 600, 500);
  24.         myCanvas.setVisible(true);
  25.     }
  26.  
  27.     /**
  28.      * Demonstrate some of the drawing operations that are
  29.      * available on a Canvas object.
  30.      */
  31.     public void drawDemo()
  32.     {
  33.         myCanvas.setFont(new Font("helvetica", Font.BOLD, 14));
  34.         myCanvas.setForegroundColor(Color.red);
  35.  
  36.         myCanvas.drawString("We can draw text, ...", 20, 30);
  37.         myCanvas.wait(1000);
  38.  
  39.         myCanvas.setForegroundColor(Color.black);
  40.         myCanvas.drawString("...draw lines...", 60, 60);
  41.         myCanvas.wait(500);
  42.         myCanvas.setForegroundColor(Color.gray);
  43.         myCanvas.drawLine(200, 20, 300, 50);
  44.         myCanvas.wait(500);
  45.         myCanvas.setForegroundColor(Color.blue);
  46.         myCanvas.drawLine(220, 100, 370, 40);
  47.         myCanvas.wait(500);
  48.         myCanvas.setForegroundColor(Color.green);
  49.         myCanvas.drawLine(290, 10, 320, 120);
  50.         myCanvas.wait(1000);
  51.  
  52.         myCanvas.setForegroundColor(Color.gray);
  53.         myCanvas.drawString("...and shapes!", 110, 90);
  54.  
  55.         myCanvas.setForegroundColor(Color.red);
  56.  
  57.         // the shape to draw and move
  58.         int xPos = 10;
  59.         Rectangle rect = new Rectangle(xPos, 150, 30, 20);
  60.  
  61.         // move the rectangle across the screen
  62.         for(int i = 0; i < 200; i ++) {
  63.             myCanvas.fill(rect);
  64.             myCanvas.wait(10);
  65.             myCanvas.erase(rect);
  66.             xPos++;
  67.             rect.setLocation(xPos, 150);
  68.         }
  69.         // at the end of the move, draw once more so that it remains visible
  70.         myCanvas.fill(rect);
  71.     }
  72.  
  73.     /**
  74.      * Simulate two bouncing balls
  75.      */
  76.     public void bounce()
  77.     {
  78.         int ground = 400;   // position of the ground line
  79.  
  80.         myCanvas.setVisible(true);
  81.  
  82.         // draw the ground
  83.         myCanvas.drawLine(50, ground, 550, ground);
  84.  
  85.         // crate and show the balls
  86.         BouncingBall ball = new BouncingBall(50, 50, 16, Color.blue, ground, myCanvas);
  87.         ball.draw();
  88.         BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
  89.         ball2.draw();
  90.  
  91.         // make them bounce
  92.         boolean finished =  false;
  93.         while(!finished) {
  94.             myCanvas.wait(50);           // small delay
  95.             ball.move();
  96.             ball2.move();
  97.             // stop once ball has travelled a certain distance on x axis
  98.             if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
  99.                 finished = true;
  100.             }
  101.         }
  102.         ball.erase();
  103.         ball2.erase();
  104.     }
  105.    
  106.     public void drawFrame()
  107.     {
  108.        Double(myCanvas.getHeight()).intValue();
  109.        Double(myCanvas.getWidth).intValue();
  110.         int cHeight = myCanvas.getHeight().intValue();
  111.        int cWidth = myCanvas.getWidth().intValue();
  112.        int fHeight = cHeight -20;
  113.        int fWidth = fWidth -20;
  114.        Rectangle frame = new Rectangle(0, 0, cHeight, cHeight);
  115.          myCanvas.fill(frame);
  116.          myCanvas.wait(200);
  117.          myCanvas.eraseRectangle (20, 20, fHeight, fWidth);
  118.          
  119.         }
  120.        
  121.    
  122.    
  123. }