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

📄 faderapplet.java

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

public class FaderApplet extends JApplet
                         implements Runnable
{
  public void init()
  {
    // Get parameters - if any
    String fade = getParameter("fadeTime");
    if(fade != null)
      fadeTime = Integer.parseInt(fade);
    String fps = getParameter("frameRate");
    if(fps != null)
      frameRate = Integer.parseInt(fps);

    ImagePanel imagePanel = new ImagePanel();
    getContentPane().add(imagePanel);

    composite = AlphaComposite.SrcOver;
  }

  // Parameter information for anyone that needs it
  public String[][] getParameterInfo()
  {
    String[][] info = {
             {"fadeTime" , "integer", "time to complete fade in seconds"},
             {"frameRate", "integer", "frames per second"               }
                      };
    return info;
  }

  public void start()
  {
    fader = new Thread(this);                 // Create the thread doing the fading
    fader.start();                            // and start it.
  }

  public void stop()
  {  fader = null;  }

  public void run()
  {
    // Remember the starting time
    long tm = System.currentTimeMillis();

    long count = frameRate*fadeTime;                // Steps for a complete fade
    float alphaStep = 1.0f/count;                   // Alpha step per interval
    long countDelta = 1;                            // Counting up to start
    long frameInterval = 1000/frameRate;            // Msec for one interval

    float currentAlphaStep = alphaStep;
    float currentAlpha = 1.0f;

    while (Thread.currentThread() == fader)
    {
      // Display the next frame of animation.
      repaint();
        
      // Sleep until the interval is up
      try
      {
        tm += frameInterval;
        Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
      }
      catch (InterruptedException e)
      {
        break;
      }
      if(count ==frameRate*fadeTime)                 // When we have complete fade
      {
        countDelta = -1;                             // count down
        currentAlphaStep = -alphaStep;
      }
      else if(count == 0)                            // When we are unfaded
      {
        countDelta = 1;                              // count up
        currentAlphaStep = alphaStep;
      }

      currentAlpha += currentAlphaStep;
      count += countDelta;                           // Increment count
      composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, currentAlpha);
    }
  }

  class ImagePanel extends JPanel
  {
    // Panel creates its own image icon
    public ImagePanel()
    {  icon = new ImageIcon("Images/wrox_logo.gif");  }

    public void paint(Graphics g)
    {
      Graphics2D g2D = (Graphics2D)g;
      Image image = icon.getImage();                 // Get the image reference
      Dimension size = getSize();                    // Get panel size
      g2D.setPaint(Color.lightGray);                 // Background color
      g2D.fillRect(0,0,size.width,size.height);      // fill the panel
      g2D.setComposite(composite);                   // Set current alpha

      // Scale and draw image
      g2D.drawImage(image,
                    size.width/10,size.height/10,    // Image position inset
                    4*size.width/5, 4*size.height/5, // Width & height of image space
                    0,0,                             // Image top left
                    icon.getIconWidth(),icon.getIconHeight(), // Image width and height
                    this);
    }

  ImageIcon icon;
  }

  AlphaComposite composite;                     // The alpha compositing object
  Thread fader;                                 // The animation thread
  int frameRate = 10;                           // Fade change frequency frames per sec
  int fadeTime = 5;                             // time to fade completely in seconds 
}

⌨️ 快捷键说明

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