Topic: 淡入淡出效果?

  Print this page

1.淡入淡出效果? Copy to clipboard
Posted by: aqipig
Posted on: 2002-12-23 22:11

如何实现图象淡入淡出效果,即图象的透明度从最大递减至0,再从0递增至最大,如此循环往复。

2.Re:淡入淡出效果? [Re: aqipig] Copy to clipboard
Posted by: taogang
Posted on: 2002-12-24 23:39

have a look of Java2D demo being with JDK bundle

a sample:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FadeIMGFrame
  extends JFrame {

  FadeIMG fimg = null;
  public FadeIMGFrame() {
    fimg = new FadeIMG();
    getContentPane().add(fimg, BorderLayout.CENTER);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    setTitle("Fade Image Demo");
    pack();
    setSize(new Dimension(500, 300));
    setVisible(true);
    fimg.run();
  }

  public static void main(String[] args) {
    new FadeIMGFrame();
  }

  class FadeIMG
    extends JPanel
    implements Runnable {
    int alpha = 0;
    int step = 1;
    Image img = null;
    public FadeIMG() {
      img = this.getToolkit().createImage("image.jpg");
    }

    /**
     * Calculate alpha's value from 1 to 100 then reverse
     */
    public void run() {
      while (true) {
        try {
          Thread.sleep(10);
        }
        catch (Exception e) {
        }

        alpha += step;
        if (alpha > 99 || alpha < 1) {
          step = -step;
        }
        repaint();
      }

    }

    public void paintComponent(Graphics g) {
      Dimension d = getSize();
      Graphics2D g2d = (Graphics2D) g;
      /**
       * Clear the backgroup with white color
       */
      g2d.setBackground(Color.WHITE);
      g2d.clearRect(0, 0, d.width, d.height);
      /**
       * Set Alpha Channel
       */
      try {
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
          (float) alpha / 100));
      }
      catch (Exception e) {
        System.out.println("\n" + alpha);
      }
      /**
       * Draw Image and release resource
       */
      g2d.drawImage(img, 0, 0, this);
      g2d.dispose();
    }
  }
}

3.Re:淡入淡出效果? [Re: aqipig] Copy to clipboard
Posted by: aqipig
Posted on: 2002-12-28 22:21

兄弟谢了

4.Re:淡入淡出效果? [Re: aqipig] Copy to clipboard
Posted by: taogang
Posted on: 2002-12-29 16:46

Welcome

5.Re:淡入淡出效果? [Re: aqipig] Copy to clipboard
Posted by: scottding
Posted on: 2002-12-30 08:32

hoho,猪猪的问题终于解决了。

6.Re:淡入淡出效果? [Re: aqipig] Copy to clipboard
Posted by: darren
Posted on: 2003-01-15 23:42

我们学校出的那本书都有这也例子。。呵呵

7.Re:淡入淡出效果? [Re: aqipig] Copy to clipboard
Posted by: mmx
Posted on: 2003-01-17 14:15

推荐一个例子程序。 jdk目录下/demo/jfc/java2D
我研究一个多星期了。感觉受益匪浅。
可以运行 jdk目录下/demo/jfc/java2D/java2Demo.html看一下效果

8.Re:淡入淡出效果? [Re: aqipig] Copy to clipboard
Posted by: justinnc
Posted on: 2003-01-18 17:52

不错


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923