推箱子问题/新手
推箱子,大神们求助,自己跟着别人的代码写的。运行出现开始界面和第一关的界面,别人正常的的game(1)是在startgame()里运行的,我的感觉初始化创建对象就出来了。
程序如下package com.fp;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class GameFram extends JFrame {
int level=1;
private Game game=new Game(level);
JPanel jp;
public GameFram(){
this.setTitle("推箱子"); //标题
this.setBounds(200,100,800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
jp=new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
ImageIcon img=new ImageIcon(GameFram.class.getClassLoader().getResource("imgs/backgroundImg.png"));
g.drawImage(img.getImage(), 0, 0, null);
}
};
this.add(jp);
jp.setLayout(null);
JButton jb=new JButton("开始游戏"); //开始按键
jp.add(jb);
jb.setBounds(630,400,100,30);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
gamestart();
requestFocus();
}
});
JButton jb1=new JButton("操作说明"); //操作按键
jp.add(jb1);
jb1.setBounds(630,450,100,30);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"上下左右控制方向"+"\r\n空格键退后一步","操作说明", JOptionPane.INFORMATION_MESSAGE);
requestFocus();
}
});
JButton jb3=new JButton("退出游戏"); //退出
jp.add(jb3);
jb3.setBounds(630,500,100,30);
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
requestFocus();
}
});
this.requestFocus();
this.addKeyListener(new MyKeyListener());
}
protected void gamestart() { //开始
// TODO Auto-generated method stub
repaint();
this.remove(jp);
this.setLayout(null);
this.add(game);
JPanel jp1=new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
ImageIcon img=new ImageIcon(GameFram.class.getClassLoader().getResource("imgs/toolImg.png"));
g.drawImage(img.getImage(), 0, 0, null);
}
};
this.add(jp1);
jp1.setBounds(600, 0, 200, 600);
jp1.setLayout(null);
JButton jb1=new JButton("后退一步");
jp1.add(jb1);
jb1.setBounds(50,250,100,30);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/////////////
requestFocus();
}
});
JButton jb2=new JButton("上一关");
jp1.add(jb2);
jb2.setBounds(50,300,100,30);
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/////////////
requestFocus();
}
});
JButton jb3=new JButton("选关");
jp1.add(jb3);
jb3.setBounds(50,350,100,30);
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/////////////
requestFocus();
}
});
JButton jb4=new JButton("下一关");
jp1.add(jb4);
jb4.setBounds(50,400,100,30);
jb4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/////////////
requestFocus();
}
});
JButton jb5=new JButton("重新开始");
jp1.add(jb5);
jb5.setBounds(50,450,100,30);
jb5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/////////////
requestFocus();
}
});
JButton jb6=new JButton("退出游戏");
jp1.add(jb6);
jb6.setBounds(50,500,100,30);
jb6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
setVisible(false);
requestFocus();
}
});
}
public static void main(String[] args) {
new GameFram();
}
}
//////////////////////////////////////////////////
package com.fp;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import com.
public class Game extends JFrame {
private LoadMap lm;
private int level;
private int mapx,mapy;
private int [][]map;
private static Image []imgs=null;
private static Toolkit tk=Toolkit.getDefaultToolkit();
static {
imgs=new Image[]{
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/0.gif")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/1.gif")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/2.GIF")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/3.GIF")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/4.gif")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/5.GIF")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/6.GIF")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/7.GIF")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/8.GIF")),
tk.getImage(GameFram.class.getClassLoader().getResource("imgs/9.GIF")),
};
}
public Game(int level){
this.setBounds(0,0,600,600);
this.setVisible(true);
lm = new LoadMap(level);
map = lm.getmap();
this.mapx = lm.getmapx();
this.mapy = lm.getmapy();
this.level = level;
}
public void paint(Graphics g) {
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
g.drawImage(imgs[map[i][j]], 30*i, 30*j, this);
}
}
g.setColor(Color.BLUE);
g.setFont(new Font("仿宋", Font.BOLD, 18));
g.drawString("第"+level+"关", 50, 50);
}
public void back() {
// TODO Auto-generated method stub
}
public void goback() {
// TODO Auto-generated method stub
}
public void gonext() {
// TODO Auto-generated method stub
}
public void restart() {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}