您的位置:首页 > 其它

ImageIcon & Image - 在组件上打印图片

2015-06-11 20:04 232 查看
ImageIcon - javax.swing.*;

Image - java.awt.*;//Image 是表示图形图像的所有类的超类

在组件上画图,使用的是Graphics中的drawImage方法

1.需要有一个封装图像数据的Image对象:

Image img=new ImageIcon(String filename).getImage();

2.使用drawImage  Graphics:

drawImage(Image img, int x, int y, ImageObserver observer) //绘制指定图像中当前可用的图像。最后一个参数可以用null

3.复制某块区域的图形到新位置

copyArea(int x, int y, int width, int height, int dx, int dy) //将组件的区域复制到由 dx 和 dy 指定的距离处。

public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g;
Image img=new ImageIcon("F:\\图片\\MHP3\\logo\\mhp3rd_dl_l01\\mhp3rd_dl_l01.png").getImage();

g2d.drawImage(img,0,0,null);
g2d.copyArea(0, 0, img.getWidth(this), img.getHeight(this), 0, img.getHeight(null));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: