天天看點

java7 透明

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Window;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.MemoryImageSource;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

import demo.ImageJPanel;

public class TranslucentWindow extends JFrame {
	Image img = null;
	Image img2 = null;
	Image im = null;
	Image im2 = null;
	Image im3 = null;

	ImageFilter imgf = null;

	FilteredImageSource fis = null;
	public TranslucentWindow() {
		super("透明窗體");
		this.setLayout(new FlowLayout());
		this.add(new JButton("按鈕"));
		this.add(new JCheckBox("複選按鈕"));
		this.add(new JRadioButton("單選按鈕"));
		this.add(new JProgressBar(20, 100));
		this.setSize(new Dimension(400, 300));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		img = new ImageIcon("e:/345.jpg").getImage();

		ImageJPanel imageJP = new ImageJPanel(img);

		imgf = new myImage(100, 100, 100);// 調用自定義類進行對象構造
		fis = new FilteredImageSource(img.getSource(), imgf);// 對圖象的源(圖象生産者)進行過濾處理,構造出FilteredImageSource對象執行個體
		img = this.createImage(fis);// 通過FilteredImageSource執行個體生成Image
//		this.getGraphics().drawImage(img, 0	, 0, this );
		this.add(imageJP);
		this.setSize(imageJP.getWidth(), imageJP.getHeight() + 35);// 這裡+35
																	// 是因為JFrame上會有個标題欄
																	// 他會占35像素
//		this.setVisible(true);
	}

	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Window w = new TranslucentWindow();
				ImageFilter imf = new ImageFilter();

				w.setVisible(true);
				com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.56f);

			}
		});
	}

}