Results 1 to 5 of 5
  1. #1
    Gunther is offline Member
    Join Date
    May 2007
    Posts
    2
    Rep Power
    0

    Default problem for games

    i am trying to make a game .Now i am starting form game menu,but it isn't work.

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;

    public class GameMenu extends JFrame implements MouseListener
    {
    ImageIcon icon;
    Image image;
    ImageIcon icon2;
    JList menulist;
    GameMenu frame;
    public GameMenu(){



    //Create background
    icon = new ImageIcon("image/bgimg.jpg");
    icon2 = new ImageIcon("image/002.jpg");
    JPanel panel = new JPanel(new GridBagLayout())
    {
    protected void paintComponent(Graphics g)
    {
    // Dispaly image at at full size
    g.drawImage(icon.getImage(), 0, 0, null);
    g.drawImage(icon2.getImage(), 0, 10, null);
    super.paintComponent(g);
    }
    };

    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.SINGL E_INTERVAL_SELECTION);
    menulist.setLayoutOrientation(JList.HORIZONTAL_WRA P);
    menulist.setVisibleRowCount(-1);
    menulist.setBackground(Color.BLACK);
    menulist.setForeground(Color.WHITE);
    menulist.addMouseListener(this);
    panel.add(menulist, a);

    panel.setOpaque( false );
    panel.setPreferredSize( new Dimension(400, 400) );
    getContentPane().add(panel);
    }

    public void mouseClicked(MouseEvent e) {
    switch(menulist.getSelectedIndex()){
    case 0:
    frame.setSize(300,300);
    case 1:
    frame.setSize(600,600);
    }

    }
    public void createGUI(){
    frame = new GameMenu();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(600, 600);
    frame.setLocationRelativeTo( null );
    frame.setVisible(true);
    }

    public static void main(String [] args){
    createGUI();
    }

    }

  2. #2
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    It does not even compile. Are you not getting a compilation error when you start it?
    In main you call createGui() and I am seeing this error:
    Cannot make a static reference to non-static method createGui().
    Instantiate the class in main and in the constructor call createGui();

  3. #3
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    Also there are unimplemented methods from the MouseListener.

  4. #4
    levent Guest

    Default

    Try making createUI method static.

    Instead of:

    Java Code:
    public void createGUI(){
    ...

    write

    Java Code:
    public static void createGUI(){
    ...

    And as Pegitha mentioned, you should implement all methods of MouseListener.

    Add following lines somewhere inside class to do that:

    Java Code:
    void mouseClicked(MouseEvent e) {};
    void mouseEntered(MouseEvent e) {};
    void mouseExited(MouseEvent e) {};
    void mousePressed(MouseEvent e) {};
    void mouseReleased(MouseEvent e) {};

    It should work in that way.
    Last edited by levent; 05-11-2007 at 10:20 PM.

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Granted this thread is mighty old, but I figured I'd post in case anyone was looking
    for it for whatever reason.

    Quote Originally Posted by levent View Post
    ...
    Java Code:
    void mouseClicked(MouseEvent e) {};
    void mouseEntered(MouseEvent e) {};
    void mouseExited(MouseEvent e) {};
    void mousePressed(MouseEvent e) {};
    void mouseReleased(MouseEvent e) {};
    ...
    The following above methods need the keyword public in front of each method's void return type. Thus:
    Java Code:
    public void mouseClicked(MouseEvent e) {}; // <-- Already implemented
    public void mouseEntered(MouseEvent e) {};
    public void mouseExited(MouseEvent e) {};
    public void mousePressed(MouseEvent e) {};
    public void mouseReleased(MouseEvent e) {};
    Also, make sure the frame variable is now static as a result:
    Java Code:
      static GameMenu frame;
    And watch out for the general spelling errors too that caused some compilation errors when I first ran it.

Similar Threads

  1. Browser-based games. Where do I start?
    By Brute in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 08:35 PM
  2. Replies: 3
    Last Post: 07-16-2007, 12:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •