problem for games2

i have the menu and i create more java file. i want to click new game form gamemenu and change the screen of Jframe to a playscreen.but it have a problem form the startgame() method.how to fixed this?

Downstair.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

public class Downstair {

public Downstair(){

playscreen playpage;

public void startgame(){

playpage = new playscreen();

repaint();

}

public static void main(String[] args) {

GM app = new GM();

app.createGUI();

}

}

}

GM.java

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

public class GM extends JFrame implements MouseListener {

ImageIcon icon;

//Image image;

ImageIcon icon2;

JList menulist;

Downstair downstair = new Downstair();

public GM() {

//Create background

icon = new ImageIcon("image/bgimg.jpg");

//"images/Bird.gif");

icon2 = new ImageIcon("image/002.jpg");

//"images/Rabbit.gif");

JPanel panel = new JPanel(new GridBagLayout()) {

protected void paintComponent(Graphics g) {

// This next statement tells this (panel) to

// draw its default background. Do it first.

super.paintComponent(g);

// Dispaly image at at full size

g.drawImage(icon.getImage(), 0, 0, null);

g.drawImage(icon2.getImage(), 100, 100, null);

}

};

GridBagConstraints a = new GridBagConstraints();

//create menulist

//Feild

String[] gameMenulist = {"New Game","Abouts","how to play","Options","Exit"};

menulist= new JList(gameMenulist);

ListSelectionModel menulistModel = menulist.getSelectionModel();

//menulist setting

menulist.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

menulist.setLayoutOrientation(JList.HORIZONTAL_WRAP);

menulist.setVisibleRowCount(-1);

menulist.setBackground(Color.BLACK);

menulist.setForeground(Color.WHITE);

menulist.addMouseListener(this);

panel.add(menulist, a);

panel.setOpaque( false );

// This call to setPreferredSize will have no affect when

// you add panel to the center of a BorderLayout. To see why

// comment-out the line above and un-comment this next line.

//panel.setBackground(Color.pink);

panel.setPreferredSize( new Dimension(400, 400) );

getContentPane().add(panel);

}

public void mouseClicked(MouseEvent e) {

switch(menulist.getSelectedIndex()) {

case 0:

downstair.startgame();

break;

}

// You can change the size but to tell the JFrame to

// make the change we must use this Container method.

validate();

}

// These method declarations are required.

// Why? Because the enclosing class implements the

// MouseListener interface. You must provide an

// implementation of every method defined in an

// inteface when you implement it (the interface).

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void createGUI() {

// The enclosing class is a JFrame by virtue of extension.

// So you can call its methods without an instance variable.

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(600, 600);

setLocationRelativeTo( null );

setVisible(true);

}

}

playscreen.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

public class playscreen {

ImageIcon playbg;

ImageIcon playericon;

ImageIcon stair;

ImageIcon live;

Imageicon die;

int live;

int floor;

public playscreen(){

//create playscreen background

playbg = new ImageIcon ("image/bgimg.jpg");

JPanel playpanel = new JPanel(new GridBagLayout()){

protected void paintComponent(Graphics g) {

super.paintComponet(g);

g.drawImage(playbg.getImage(), 0, 0, null);

}

};

GridBagConstraints b = new GridBagConstraints();

playpanel.setOpaque( false );

playpanel.setPreferredSize( new Dimension(400, 400) );

getContentPane().add(playpanel);

}

}

[4489 byte] By [nickgoldgodla] at [2007-11-27 3:59:22]
# 1
I tried to load your program and debug it. I could not build it at all. There are a number of syntax errors. If you get it to compile and resubmit it I will take a look.Or is that what you are trying to get help on?Richard.
Richard_Cromera at 2007-7-12 9:03:53 > top of Java-index,Security,Cryptography...
# 2
when i complie it show this:Downstair.java:15: illegal start of expressionpublic void startgame(){^Downstair.java:28: class, interface, or enum expected}
nickgoldgodla at 2007-7-12 9:03:53 > top of Java-index,Security,Cryptography...
# 3

Downstair.java:15: illegal start of expression

You have a misplaced curley brace. Learning to properly format your code will help

catch/eliminate these kinds of mistakes.

public class Downstair {

public Downstair(){

playscreen playpage;

// } need a curley brace here to end the constructor code block

public void startgame(){

playpage = new playscreen();

repaint();

}

public static void main(String[] args) {

GM app = new GM();

app.createGUI();

}

// } <- misplaced curly brace

}

Here's an idea:

One class deals with user input and controlling the game.

The other class deals with displaying the game.

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import javax.swing.*;

public class DS extends JFrame implements MouseListener {

JList menulist;

PlayScreen playPage;

public DS() {

// Create components.

String[] gameMenulist = {"New Game","Abouts","how to play","Options","Exit"};

menulist= new JList(gameMenulist);

ListSelectionModel menulistModel = menulist.getSelectionModel();

//menulist setting

menulist.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

menulist.setLayoutOrientation(JList.HORIZONTAL_WRAP);

menulist.setVisibleRowCount(-1);

menulist.setBackground(Color.BLACK);

menulist.setForeground(Color.WHITE);

menulist.addMouseListener(this);

playPage = new PlayScreen();

// Build west panel.

JPanel west = new JPanel(new GridBagLayout());

GridBagConstraints a = new GridBagConstraints();

a.insets = new Insets(5,5,5,5);

west.add(menulist, a);

// Add components to frame.

getContentPane().add(west, "Before");

getContentPane().add(playPage);

// Configure JFrame.

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(600, 600);

setLocationRelativeTo( null );

setVisible(true);

}

public void mouseClicked(MouseEvent e) {

switch(menulist.getSelectedIndex()) {

case 0:

playPage.startGame();

break;

case 4:

System.exit(0);

break;

default:

System.out.println("unexpected type: " +

menulist.getSelectedIndex());

}

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public static void main(String[] args) {

new DS();

}

}

class PlayScreen extends JPanel {

ImageIcon playbg;

ImageIcon playerIcon;

public PlayScreen(){

//create playscreen background

playbg = new ImageIcon(//"image/bgimg.jpg");

"images/Bird.gif");

playerIcon = new ImageIcon(//"image/002.jpg");

"images/Rabbit.gif");

setBorder(BorderFactory.createEtchedBorder());

}

public void startGame(){

System.out.println("startGame");

repaint();

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(playbg.getImage(), 0, 0, null);

g.drawImage(playerIcon.getImage(), 200, 200, null);

}

}

crwooda at 2007-7-12 9:03:53 > top of Java-index,Security,Cryptography...
# 4
thanks it work~
nickgoldgodla at 2007-7-12 9:03:53 > top of Java-index,Security,Cryptography...