package com.sdu.embeddedLab.MingchaoSun.pac_man.gamewin;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.security.auth.x500.X500Principal;
import javax.swing.*;
import com.sdu.embeddedLab.MingchaoSun.pac_man.mapBuilder.Maze;
import com.sdu.embeddedLab.MingchaoSun.pac_man.value.Value;
/*
* date:2014/7/4
* Writer: Mingchao Sun
* Function: The Main Window of the Game named PAC_MAN
*/
public class MainWin extends JFrame implements ActionListener{
public static Maze map = null;
// define the component of the window
private GamePanel gameboard = null;
private JPanel databoard = null;
private JMenuBar jMenuBar = null;
private JMenu menu, about = null;
private JMenuItem start, suspend, sittings, exit, about1, helps , optimalPaths = null;
private Label step,score,bestStep,nstep,nscore,nbestStep,judge1,judge2 = null;
// constructor
public MainWin() {
// define the imageicon and set the dimension
ImageIcon start1 = new ImageIcon(
MainWin.class.getResource("start1.png"));
start1.setImage(start1.getImage().getScaledInstance(30, 30,
Image.SCALE_DEFAULT));
ImageIcon pause1 = new ImageIcon(
MainWin.class.getResource("pause1.png"));
pause1.setImage(pause1.getImage().getScaledInstance(30, 30,
Image.SCALE_DEFAULT));
ImageIcon gameabout = new ImageIcon(
MainWin.class.getResource("gameabout.png"));
gameabout.setImage(gameabout.getImage().getScaledInstance(30, 30,
Image.SCALE_DEFAULT));
ImageIcon setting = new ImageIcon(
MainWin.class.getResource("setting.png"));
setting.setImage(setting.getImage().getScaledInstance(30, 30,
Image.SCALE_DEFAULT));
ImageIcon close = new ImageIcon(
MainWin.class.getResource("close.png"));
close.setImage(close.getImage().getScaledInstance(30, 30,
Image.SCALE_DEFAULT));
ImageIcon help = new ImageIcon(
MainWin.class.getResource("help.png"));
help.setImage(help.getImage().getScaledInstance(30, 30,
Image.SCALE_DEFAULT));
ImageIcon optimalPath = new ImageIcon(
MainWin.class.getResource("gameabout.png"));
optimalPath.setImage(optimalPath.getImage().getScaledInstance(30, 30,
Image.SCALE_DEFAULT));
// creat the component
// panel
gameboard = new GamePanel();
databoard = new JPanel();
// JMenu
jMenuBar = new JMenuBar();
menu = new JMenu("菜单(M)");
about = new JMenu("关于(A)");
// Label
step = new Label("step");
score = new Label("score");
bestStep = new Label("bestStep");
String stepString = ""+Value.step;
String scoreString = ""+Value.score;
String bestStepString = ""+Value.bestStep;
nstep = new Label(stepString);
nscore = new Label(scoreString);
nbestStep = new Label(bestStepString);
judge1 = new Label("(*^__^*)");
judge2 = new Label("加油哦!");
// JMenuItem
start = new JMenuItem("开始",start1);
suspend = new JMenuItem("暂停",pause1);
optimalPaths = new JMenuItem("最优路径",optimalPath);
sittings = new JMenuItem("设置...",setting);
exit = new JMenuItem("退出",close);
helps = new JMenuItem("帮助",help);
about1 = new JMenuItem("关于",gameabout);
// set Mnemonic
menu.setMnemonic('M');
about.setMnemonic('A');
// set Accelerator
start.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
KeyEvent.CTRL_MASK));
suspend.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
KeyEvent.CTRL_MASK));
sittings.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
KeyEvent.CTRL_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
KeyEvent.CTRL_MASK));
optimalPaths.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
KeyEvent.CTRL_MASK));
// add the ActionListener to JMenuItem
start.addActionListener(this);
start.setActionCommand("start");
suspend.addActionListener(this);
suspend.setActionCommand("suspend");
sittings.addActionListener(this);
sittings.setActionCommand("sittings");
exit.addActionListener(this);
exit.setActionCommand("exit");
about1.addActionListener(this);
about1.setActionCommand("about1");
helps.addActionListener(this);
helps.setActionCommand("helps");
optimalPaths.addActionListener(this);
optimalPaths.setActionCommand("optimalPath");
// add the component
this.setJMenuBar(jMenuBar);
jMenuBar.add(menu);
jMenuBar.add(about);
menu.add(start);
menu.add(suspend);
menu.add(optimalPaths);
menu.add(sittings);
menu.add(exit);
about.add(helps);
about.add(about1);
databoard.setLayout(new GridLayout(4, 2));
databoard.add(step);
databoard.add(nstep);
databoard.add(bestStep);
databoard.add(nbestStep);
databoard.add(score);
databoard.add(nscore);
databoard.add(judge1);
databoard.add(judge2);
// set layout
this.add(gameboard,BorderLayout.CENTER);
this.add(databoard,BorderLayout.LINE_END);
// Key Listener
this.addKeyListener(gameboard);
// JFrame setting
ImageIcon headImageIcon = new ImageIcon(
MainWin.class.getResource("gameabout.png"));
this.setIconImage(headImageIcon.getImage());
this.setTitle("MAZE_MAN");
// Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
// this.setSize(d.width/2,d.height/2);
this.setSize(800,600);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
// Class GamePanel
public class GamePanel extends JPanel implements KeyListener{
public GamePanel(){
MainWin.map = new Maze(25, 25);
MainWin.map.traversal();
MainWin.map.create();
MainWin.map.findPath();
Value.bestStep=getBestStep(MainWin.map);
}
// override paint method
public void paint(Graphics g) {
super.paint(g);
// Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
// g.fillRect(10, 10, d.width/3,d.height/2-75);
g.setColor(Color.black);
g.fillRect(10, 10, 510,510);
g .setColor(Color.yellow);
for(int i = 0 ; i<51;i++){
for(int j = 0 ; j<51;j++){
if(MainWin.map.maze[i][j] == MainWin.map.gridWall){
g.fillRect(10+10*i, 10+10*j, 10,10);
int[] point = new int[2];
point[0] = 10+10*i;
point[1] = 10+10*j;
Value.iswall.add(point);
}
}
}
g.setColor(Color.green);
g.fillRect(20, 20, 10,10);
g.setColor(Color.red);
g.fillRect(500, 500, 10,10);
PaintOptimalPath(g,MainWin.map,Value.isoptimalPaths);
initializationMan(g,Value.isNeedInitialization);
g.setColor(Color.BLUE);
g.fillOval(Value.Man_x, Value.Man_y, 10, 10);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if((e.getKeyCode() == KeyEvent.VK_UP)&&(notWall(Value.Man_x,Value.Man_y-10))){
Value.Man_y=Value.Man_y-10;
System.out.pr