黑马程序员技术交流社区
标题:
怎么用java读取电脑中的图片然后在JFrame中显示出来?
[打印本页]
作者:
张奇云
时间:
2011-11-3 07:49
标题:
怎么用java读取电脑中的图片然后在JFrame中显示出来?
怎么用java读取电脑中的图片然后在JFrame中显示出来?
作者:
fso918
时间:
2011-11-3 09:10
给段代码给你:
我自己用的。
class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
JPanel contentPanel;
BorderLayout bl = new BorderLayout();
DrawCanvas canvas = new DrawCanvas();
ImagePanel(){
init();
}
private void init() {
contentPanel = this;
contentPanel.setLayout(bl);
this.setSize(new Dimension(330, 432));
contentPanel.add(this.canvas, BorderLayout.CENTER);
}
class DrawCanvas extends Canvas {
private static final long serialVersionUID = 1L;
Image image;
public DrawCanvas() {
image = this.getToolkit().getImage("images//zhenji.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 1);
try {
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (image.getWidth(this) == -1) {
System.out.println("Could not get the image");
System.exit(-1);
}
this.setBackground(Color.black);
}
@Override
public void paint(Graphics g){
Graphics2D g2D = (Graphics2D)g;
BufferedImage bimage = (BufferedImage)this.createImage(this.getWidth(),this.getHeight());
bimage.createGraphics().drawImage(this.image,0,0,this);
g2D.drawImage(bimage,0,0,this);
}
}
}
复制代码
作者:
张奇云
时间:
2011-11-3 10:41
谢谢,学习了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2