import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Transparency;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test_JFrame2 extends JButton
{
Image img;
MediaTracker mt;
int w;
int h;
public Test_JFrame2(String name,String imagePath)
{
try
{
img=new ImageIcon(imagePath).getImage(); //读取本地图片
mt=new MediaTracker(this);//为此按钮添加媒体跟踪器
mt.addImage(img,0);//在跟踪器添加图片,下标为 0
mt.waitForAll(); //等待加载
w=img.getWidth(this);//读取图片长度
h=img.getHeight(this);//读取图片宽度
GraphicsConfiguration gc = new JFrame().getGraphicsConfiguration(); // 本地图形设备
Image image = gc.createCompatibleImage(w,h,Transparency.TRANSLUCENT);//建立透
明画布
Graphics2D g=(Graphics2D)image.getGraphics(); //在画布上创建画笔
Composite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .9f); //指
定透明度为半透明 90%
g.setComposite(alpha);
g.drawImage(img,0,0,this); //注意是,将 image 画到 g 画笔所在的画布上
g.setColor(Color.black);//设置颜色为黑色
g.drawString(name, 25, 20);//写字
g.dispose(); //释放内存
评论0
最新资源