经典小游戏一-三国战纪
经典小游戏一-三国战纪
一、构建窗体和面板
GameFrame类
1.写一个类GameFrame,继承JFrame
2.写一个构造方法,确定窗体的特点
1 package day1; 2 3 import javax.swing.JFrame; 4 5 public class GameFrame extends JFrame { 6 7 public GameFrame() { 8 setTitle("a game"); 9 setSize(800, 600); 10 setLocationRelativeTo(null); 11 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 } 13 14 public static void main(String[] args) { 15 GameFrame frame = new GameFrame(); 16 GamePanel panel = new GamePanel(); 17 frame.add(panel); 18 frame.setVisible(true); 19 } 20 }
GamePanal类
1.写一个类GameFrame,继承JPanal
2.写一个构造方法,确定面板的特点
1 package day1; 2 3 import java.awt.Color; 4 import java.awt.Graphics; 5 import java.awt.image.BufferedImage; 6 import java.io.IOException; 7 import javax.imageio.ImageIO; 8 import javax.swing.JPanel; 9 10 /*游戏的面板模具Panel 11 */ 12 public class GamePanel extends JPanel { 13 14 BufferedImage bg1; 15 16 public GamePanel() { 17 // 设置背景 18 try { 19 bg1 = ImageIO.read(GamePanel.class.getResource("/day1/img/bg1.png")); 20 } catch (IOException e) { 21 e.printStackTrace();// 输出异常 22 } 23 24 // 背景色RGB 25 setBackground(new Color(0, 238, 0)); 26 } 27 28 /** 29 * 画图方法 30 */ 31 32 public void paint(Graphics g) { 33 super.paint(g); 34 g.drawImage(bg1, 0, 0, 800, 600, null); 35 36 } 37 38 }

浙公网安备 33010602011771号