[Java]在窗口界面上画出硬盘中图片文件

  利用类javax.swing.JPanel来在窗口界面上画图。图片文件通过javax.imageio.ImageIO类来获取。

 1 import java.awt.Graphics;
 2 import java.awt.image.BufferedImage;
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import javax.imageio.ImageIO;
 7 import javax.swing.JFrame;
 8 import javax.swing.JPanel;
 9 
10 import com.Sunday.TankWar.configure.TankWarConfigure;
11 
12 public class ImageFrame {
13 
14     private BufferedImage image;
15     private String imageName = "home.jpg";
16 
17     public ImageFrame() throws IOException {
18         image = ImageIO.read(new File(TankWarConfigure.imagePath + imageName));
19     }
20 
21     @SuppressWarnings("serial")
22     class Background extends JPanel {
23         public void paint(Graphics g) {
24             g.drawImage(image, 0, 0, null);
25             g.drawImage(image, 100, 100, null);
26         }
27     }
28 
29     public static void main(String[] args) throws Exception {
30         ImageFrame image = new ImageFrame();
31         JPanel jpanel = image.new Background();
32         JFrame frame = new JFrame("图片测试");
33         frame.add(jpanel);
34         frame.setBounds(0, 0, 200, 220);
35         frame.setVisible(true);
36     }
37 
38 }

 

你可能感兴趣的:(java)