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);
}
}

