Huge Java Project! Danger: Brain Melting!

Discussion in 'Computer Science & Culture' started by TruthSeeker, Nov 26, 2004.

Thread Status:
Not open for further replies.
  1. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    Yep... yeah...

    Please Register or Log in to view the hidden image!



    I have to design a hangman game.
    I got a menu and a start window, but I've been trying to open another file from within my main file and I haven't been able to find a method that does that. Any thoughts?


    Details:
    One of my MenuItems is "newGame" and I'm trying to make a method in which when I press "newGame" the computer go get the file which opens a new game...

    Also, the code seems fine, but I cannot see any menus nor a title....

    Please Register or Log in to view the hidden image!



    Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GameMain extends Frame implements ActionListener, WindowListener{
    
    	private HangmanMenus hangman;
    
        private MenuItem newGame, statistics, exit;
        private MenuItem player1, random, difficulty;
        private MenuItem credits;
    
        private File game;
    
        public static void main(String[] args){
            Frame f= new GameMain();
            f.setSize(500,500);
            f.setVisible(true);
        }
    
        public void GameMain(){
            setTitle("Hangman");
            setLayout(new FlowLayout());
    
            MenuBar menu= new MenuBar();
    
            //File Menu
            Menu fileMenu= new Menu("File");
    
            [B]newGame= new Menu("New Game");
            fileMenu.add(newGame);
            newGame.addActionListener(this);[/B]
    
            statistics= new Menu("Statistics");
            fileMenu.add(statistics);
            statistics.addActionListener(this);
    
            exit= new Menu("Quit");
            fileMenu.add(exit);
            exit.addActionListener(this);
    
            menu.add(fileMenu);
    
            //Options Menu
            Menu optionsMenu= new Menu("Options");
    
            player1= new Menu("Player1");
            optionsMenu.add(player1);
            player1.addActionListener(this);
    
            random= new Menu("Random");
            optionsMenu.add(random);
            random.addActionListener(this);
    
            difficulty= new Menu("Difficulty");
            optionsMenu.add(difficulty);
            difficulty.addActionListener(this);
    
            menu.add(optionsMenu);
    
            //About Menu
            Menu about= new Menu("About");
    
            credits= new Menu("Credits");
            about.add(credits);
            credits.addActionListener(this);
    
            menu.add(about);
    
    
            //Add Menus
            setMenuBar(menu);
            this.addWindowListener(this);
    
             hangman= new HangmanMenus();
    
        }
    
        public void actionPerformed(ActionEvent event){
            /*
    
            [B]if (event.getSource()==newGame)
            HangmanMenus.newGame();[/B]
    
            if (event.getSource()==statistics)
            HangmanMenus.statistics()*/
    
            if (event.getSource()==exit)
            HangmanMenus.exit();
    
            /*if (event.getSource()==player1)
            HangmanMenus.player1();
    
            if (event.getSource()==random)
            HangmanMenus.random();
    
            if (event.getSource()==difficulty)
            HangmanMenus.difficulty();
    
            if (event.getSource()==credits)
            HangmanMenus.credits();*/
    
        }
    
        /*public void paint(Graphics g){
    		hangman.display(g);
    	}*/
    
    
        public void windowClosing(WindowEvent event){
            System.exit(0);
        }
    
        public void windowIconified(WindowEvent e){
        }
    
        public void windowOpened(WindowEvent e){
        }
    
        public void windowClosed(WindowEvent e){
        }
    
        public void windowDeiconified(WindowEvent e){
        }
    
        public void windowActivated(WindowEvent e){
        }
    
        public void windowDeactivated(WindowEvent e){
        }
    
        [U]static class HangmanMenus{[/U]
    
    	   public static void exit(){
               System.exit(0);
    	   }
    
    	   [B]public static void newGame(){
    		   game = new File("M:\Comp132\Game.java");
    		   start= game.getPath();
                    }[/B]
    
       }
    }
    
     
    Last edited: Nov 27, 2004
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    Wait! Is there some method I could use in the System class!?!?
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. shoffsta Geek Registered Senior Member

    Messages:
    60
    The code u wrote above doesn't quite make sense.

    First of all, u need double backslashes:
    game = new File("M:\\Comp132\\Game.java");
    or forward slashes:
    game = new File("M:/Comp132/Game.java");

    and then, why would u want to open a ".java" file for your game?

    I would also reckomend using swing instead of awt.
    start with something like:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MainFrame extends JFrame {
        public static void main(String[] args) {
            JFrame f = new MainFrame();
            f.pack();
            f.setVisible(true);
        }
        
        private JPanel mainPanel;
        
        public MainFrame() {
            super("Hangman");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout());
            JMenuBar menubar = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
            JMenuItem newItem = new JMenuItem("New Game");
            fileMenu.add(newItem);
            class NewItemListener implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(mainPanel, 
                           "Action \"New Game\" not yet implemented"); 
                }
            }
            newItem.addActionListener(new NewItemListener());
            JMenuItem quitItem = new JMenuItem("Quit");
            fileMenu.add(quitItem);
            class QuitItemListener implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            }
            quitItem.addActionListener(new QuitItemListener());
            menubar.add(fileMenu);
            mainPanel.add(menubar, BorderLayout.NORTH);
            this.setContentPane(mainPanel);
        }
    }
    
    [/CODE]
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Voodoo Child Registered Senior Member

    Messages:
    1,296
    Don't start coding immediately. First, figure out how it is going to work.

    I think you want to initialise a class that resides in another file. If the file is in the same directory then you merely initialise it as you would normally. eg.

    Game game = new Game();
    Do you want to access a file with hangman words in them?
    You could either have the Game class open a file or pass a file object to the game constructor/use a game method to set it's file.
     
  8. Stryder Keeper of "good" ideas. Valued Senior Member

    Messages:
    13,105
    Voodoo's right again, In all programming projects you have to plan out how the program is comprised. Usually taught is the Jackson System Development although slightly awkward to master along with the usage of "Pseudocode".

    Pseudocode is basically writing a program that's partially in English in the philosophy of how it works with a little bit of Basic thrown in.

    http://perl.about.com/od/beginningperl/a/072604.htm

    I know thats from perl, but all programming starts with Pseudocode and Pseudocode is actually kept as a preportion to the Development Documentation on a project. It's useful to come back to later if the project needs to be expanded or even converted to a different platform.
     
  9. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    I usually work better plunging into it. The difficulty here is that I've never dealt with this kind of programming before - that is, using more than one file...

    Yes... I realized that @~3 in the morning, waking up suddenly...

    Please Register or Log in to view the hidden image!


    I work better when I'm asleep...

    Thanks for confirming it anyways...

    Please Register or Log in to view the hidden image!

     
  10. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    Yes, that's actually very helpful....

    Please Register or Log in to view the hidden image!


    Thx
     
  11. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    I should't use swing. My friend and I, we are going to use Java2D....

    Btw... thanks for the "
    Code:
    ", I will be using that.... :D
     
  12. Voodoo Child Registered Senior Member

    Messages:
    1,296
    I believe that is written on the gates of hell.
     
  13. Aborted_Fetus Bored Registered Senior Member

    Messages:
    277
    :: shiver ::

    I hate Java almost as much as I hate going to the dentist. And I hate going to the dentist. Sure, it can run in any OS environment, but it runs shitty in all of them.

    Oh well.
     
Thread Status:
Not open for further replies.

Share This Page