⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logobounce.java

📁 非常好的java事例以及带源码事例的java2教程
💻 JAVA
字号:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.net.*;

public class LogoBounce extends JApplet
                        implements Runnable
{
  // This method is called when the applet is loaded
  public void init()
  {
    tracker = new MediaTracker(this);
    Image image = null;
    try
    {
      // Image from a file specified by a URL
      image = getImage(new URL(getCodeBase(),"Images/wrox_logo.gif")); 
    }
    catch(MalformedURLException e)
    {
      System.out.println("Failed to create URL:\n" + e);
    }

    tracker.addImage(image,0);

    try
    {
      tracker.waitForAll();                       // Wait for image to load
      if(tracker.isErrorAny())                    // If there is an error
        return;                                   // give up
         
      imageWidth = image.getWidth(this);          // Get image width
      imageHeight = image.getHeight(this);        // and its height
      resize(imageWidth,imageHeight);             // set applet size to fit
      imageY = -imageHeight;                      // for image
      imagePanel = new ImagePanel(image);         // Create panel showing the image
      getContentPane().add(imagePanel);           // Add the panel to the content pane
    }
    catch(InterruptedException e)
    {
      System.out.println(e);
    }
  }

  // This method is called when the browser starts the applet
  public void start()
  {
    if(tracker.isErrorAny())                   // If any image errors
      return;                                  // don't create the thread
    bouncer = new Thread(this);                       // Create animation thread
    bouncer.start();                                  // and start it
  }

  // This method is called when the browser want to stop the applet
  // when is it not visible for example
  public void stop()
  {
    bouncer = null;                                  // Discard the thread
  }

  // This method is called when the animation thread is started
  public void run()
  {
    long time = System.currentTimeMillis(); // Starting time
    long interval = 20;                     // Time interval msec
    float t = interval/1000.0f;             // and in seconds
    final float g = 32;                     // Acceleration due to gravity
    float a = g;                            // Initial acceleration
    float v = 0.0f;                         // Initial velocity

    // Move image while the bouncer thread is running
    while(Thread.currentThread() == bouncer)
    {
      imagePanel.repaint();                 // Repaint the image

      // Wait until the end of the next interval
      try
      {
        time += interval;
        Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
      }
      catch (InterruptedException e)
      {
        break;
      }

      imageY += (long)(t*(v+a*t/2));        // New image position
      v += a*t;                             // New velocity

      // Calculate distance moved in interval
      if(imageY>0)                          // Image compressed?
        a = g - 10.000f*g*imageY/imageHeight;  // acceleration in opposite direction 

      if(imageY<=0)                         // Image not compressed?
        a = g;                              // -then falling under gravity
    }
  }

  class ImagePanel extends JPanel
  {
    public ImagePanel(Image image)
    {
      this.image = image;
    }

    public void paint(Graphics g)
    {
      Graphics2D g2D = (Graphics2D)g;
      g2D.setPaint(Color.lightGray);                          // Set a background color
      g2D.fillRect(0, 0, imageWidth, imageHeight);            // paint background
      if(imageY<=0)
        g2D.drawImage(image, imageX, imageY, this);           // Draw normally
      else                                                    // or scaled...
        g2D.drawImage(image,                                  // The image
                      imageX, imageY, imageWidth, imageHeight,// Destination
                      0, 0, imageWidth, imageHeight,          // Image area
                      this);                                  // Image observer
   }

    Image image;                                       // The image
  }

  Thread bouncer;                                     // The animation thread
  ImagePanel imagePanel;                              // Panel for the image
  int imageWidth, imageHeight;                        // Image dimensions
  int imageX, imageY;                                 // Current image position
  MediaTracker tracker;                               // Tracks image loading
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -