日期:2014-05-20 浏览次数:20927 次
package com.hello.model; import java.awt.Graphics; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.*; public class NewImage { public static void main(String[] args) throws IOException { JFrame frame = new JFrame(); Image image = new ImageIcon("icon/baidu2.gif").getImage().getScaledInstance(33, -1, Image.SCALE_DEFAULT); MyPanel canvas = new MyPanel(image); frame.add(new JScrollPane(canvas)); frame.pack(); frame.setVisible(true); } } class MyPanel extends JPanel { Image image ; final int ICON_WIDTH = 23; final int ICON_HEIGHT = 22; public MyPanel(Image image) { this.image = image; } public void paint(Graphics g) { g.drawImage( new ImageIcon("icon/baidu2.gif").getImage().getScaledInstance(33, -1, Image.SCALE_DEFAULT),(getWidth() - ICON_WIDTH)/2,(getHeight() - ICON_WIDTH)/2,null);// 1 这样写不会有图片输出 //g.drawImage(image),(getWidth() - ICON_WIDTH)/2,(getHeight() - ICON_WIDTH)/2,null); //2 可以输出图片 } }