[quote]I’m working on a maze game from kevglass tutorial… Now, i came working to the Main menu… My problem is when i click my Start button to access the game class it loads the game but the game was distorted and the keyboard doesn’t work… Please my code for MainMenu and Game class…
MainMenu class
import java.awt.;
import javax.swing.;
import java.awt.event.;
import java.lang.;
public class MainMenu extends JFrame implements ActionListener {
private JButton b1, b2, b3;
private JLabel label1;
public MainMenu() {
super("Maze Game");
Container contentPane = getContentPane();
contentPane.setLayout(null);
b1 = new JButton("Start Game");
b1.setActionCommand("start");
contentPane.add(b1);
b2 = new JButton("Help");
b2.setActionCommand("help");
contentPane.add(b2);
b3 = new JButton("Exit");
b3.setActionCommand("exit");
contentPane.add(b3);
ImageIcon icon = new ImageIcon("background.jpg");
label1 = new JLabel(icon);
contentPane.add(label1);
Insets insets = contentPane.getInsets();
label1.setBounds(0, 0, 640, 480);
b1.setBounds(15 + insets.left, 5 + insets.top, 105, 22);
b2.setBounds(15 + insets.left, 35 + insets.top, 105, 22);
b3.setBounds(15 + insets.left, 65 + insets.top, 105, 22);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setSize(640,480);
show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "start")
{
Game game=new Game();
game.gameLoop();
}
if (e.getActionCommand() == "help")
{
//
}
if (e.getActionCommand() == "exit")
{
System.exit(0);
}
}
public static void main(String args[])
{
MainMenu mainmenu=new MainMenu();
mainmenu.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
Game Class
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Font;
import java.util.Random;
import java.applet.AudioClip;
import java.net.URL;
import java.net.MalformedURLException;
public class Game extends Canvas {
private BufferStrategy strategy;
private ArrayList entities = new ArrayList();
private Hero albert;
private AlienEntity zomb1;
private AlienEntity zomb2;
private String message = "";
public boolean waitingForKeyPress = true;
private Keyb board;
private Map map;
private int fontSize = 30;
public Game() {
JFrame container = new JFrame("CSCI15");
JPanel panel = (JPanel) container.getContentPane();
board = new Keyb();
panel.setPreferredSize(new Dimension(800,600));
panel.setLayout(null);
setBounds(0,0,800,600);
panel.add(this);
setIgnoreRepaint(true);
container.pack();
container.setResizable(false);
container.setVisible(true);
container.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(board);
requestFocus();
createBufferStrategy(2);
strategy = getBufferStrategy();
initEntities();
}
public void startGame() {
//startLoadingSounds();
entities.clear();
initEntities();
board.left = false;
board.right = false;
board.up = false;
board.down = false;
}
public void initEntities() {
map = new Map();
albert = new Hero(this,"res/hero_down.gif", map, 5, 5);
entities.add(albert);
zomb1 = new AlienEntity(this,"res/zombie1.gif",map,15,14);
entities.add(zomb1);
zomb2 = new AlienEntity(this,"res/zombie1.gif",map,24,1);
entities.add(zomb2);
//loopClip = soundList.getClip(chosenFile);
}
public void gameLoop() {
boolean gameRunning = true;
while (gameRunning) {
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,800,600);
for (int p=0;p<entities.size();p++)
{
for (int s=p+1;s<entities.size();s++)
{
Entity me = (Entity) entities.get(s);
Entity him = (Entity) entities.get(p);
if (me.collidesWith(him))
{
me.collidedWith(him);
him.collidedWith(me);
}
}
}
map.paint(g);
albert.paint(g);
zomb1.paint(g);
zomb2.paint(g);
g.dispose();
strategy.show();
try { Thread.sleep(100); } catch (Exception e) {}
logicHero();
logicAlien();
}
}
public void logicHero() {
int dx = 0;
int dy = 0;
if (board.left) {
dx -= 1;
}
if (board.right) {
dx += 1;
}
if (board.up) {
dy -= 1;
}
if (board.down) {
dy += 1;
}
if ((dx != 0) || (dy != 0)) {
albert.move(dx,dy);
}
}
public void logicAlien() {
Random randomNumbers = new Random();
int dx;
int dy;
// dx = ( randomNumbers.nextInt( 3 ) - 1);
// dy = ( randomNumbers.nextInt( 3 ) - 1);
// dx = ( randomNumbers.nextInt( 300 / 100 ) - 1);
// dy = ( randomNumbers.nextInt( 300 / 100 ) - 1);
dx = ( randomNumbers.nextInt( 30/10)-1);
dy = ( randomNumbers.nextInt( 30/10 )-1);
zomb1.moveZ1(dx,dy);
zomb2.moveZ1(dx,dy);
}
public void notifyDeath() {
//code here }
public void notifyWon() {
//code here }
}
[/quote]
Please i really nid help!!! Thnx!!!
