1 package test;
2 import java.awt.Image;
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.net.URL;
6 import javax.imageio.ImageIO;
7 /**
8 * 游戏开发中常用的工具类
9 * @author Nicholas
10 */
11 public class GameUtil {
12 public static Image getImage(String path){
13 URL u=GameUtil.class.getClassLoader().getResource(path);
14 BufferedImage imag=null;
15 try {
16 imag=ImageIO.read(u);
17 } catch (IOException e) {
18 e.printStackTrace();
19 }
20 return imag;
21 }
22 }
1 package test;
2 import java.awt.Color;
3 import java.awt.Font;
4 import java.awt.Frame;
5 import java.awt.Graphics;
6 import java.awt.Image;
7 import java.awt.event.WindowAdapter;
8 import java.awt.event.WindowEvent;
9 /**
10 * 游戏窗口类
11 * @author Nicholas
12 * 窗口以左上角为坐标
13 */
14 public class GamePicture extends Frame {//GUI编程AWT,SWING
15 Image img = GameUtil.getImage("picture/test.jpeg");//加载图片
16 //加载窗口
17 public void launchFrame(){
18 setSize(800,600);//设置大小
19 setLocation(100,100);//设置初始位置
20 setVisible(true);//设置图形可见
21 addWindowListener(new WindowAdapter(){//实现窗口关闭
22 public void windowClosing(WindowEvent e) {
23 System.exit(0);
24 }
25 });
26 }
27 public void paint(Graphics g) {
28 g.drawImage(img, 50, 100, null);//设置显示图片
29 }
30 public static void main(String[] args) {
31 GamePicture gp=new GamePicture();
32 gp.launchFrame();
33 }
34
35 }