[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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。