View Full Version : Huge Java Project! Danger: Brain Melting!


TruthSeeker
11-26-04, 04:32 PM
Yep... yeah... :D

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.... :confused:


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

newGame= new Menu("New Game");
fileMenu.add(newGame);
newGame.addActionListener(this);

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){
/*

if (event.getSource()==newGame)
HangmanMenus.newGame();

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

static class HangmanMenus{

public static void exit(){
System.exit(0);
}

public static void newGame(){
game = new File("M:\Comp132\Game.java");
start= game.getPath();
}

}
}

TruthSeeker
11-26-04, 04:35 PM
Wait! Is there some method I could use in the System class!?!?

shoffsta
11-26-04, 08:08 PM
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:


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]

Voodoo Child
11-27-04, 02:44 AM
Don't start coding immediately. First, figure out how it is going to work.

public static void newGame(){
game = new File("M:\Comp132\Game.java");
start= game.getPath();

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.

Stryder
11-27-04, 11:56 AM
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.

TruthSeeker
11-27-04, 12:54 PM
Don't start coding immediately. First, figure out how it is going to work.
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...

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();
Yes... I realized that @~3 in the morning, waking up suddenly... :D
I work better when I'm asleep...

Thanks for confirming it anyways... ;)

TruthSeeker
11-27-04, 12:55 PM
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.
Yes, that's actually very helpful.... :)
Thx

TruthSeeker
11-27-04, 12:57 PM
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:
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

Voodoo Child
11-28-04, 10:39 PM
I usually work better plunging into it.

I believe that is written on the gates of hell.

Aborted_Fetus
12-04-04, 09:50 PM
:: 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.