|
I have to write a program for my final project I chose to do a hangman game, but im running into a few problems ~_~ It's an applet, and I am trying to add in a text box for the user to enter in the letters they are going to guess.
I am fairly new to applets and gui, and ive been sort of trying to guess my way through using the text book and checking open source hangman games.
i want to add the text box either above or below the gallows image. Any gosu programmer help me out plz? here is my pitiful program so far + Show Spoiler +/** * @(#)HangmanGame.java * * HangmanGame Applet application * * @author * @version 1.00 2008/6/7 */ import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*;
public class HangmanGame extends Applet{
int wrongguesses = 0; String word = ("intuitive"); String line = ("---------"); public void init() { JTextField guessletter = new JTextField(1); JPanel panel = new JPanel (); panel.setBackground(Color.yellow); panel.setPreferredSize(new Dimension(300, 75)); panel.add(guessletter); JFrame frame = new JFrame ("Letter");
//frame.getContentPane().add (panel);
}
public void paint(Graphics page) { setBackground(Color.white); page.fillRect(10, 250, 150, 20); page.fillRect(40,70,10,200); page.fillRect(40,70,60,10); page.fillRect(95,70,5,25); Font abc = new Font("Courier",Font.BOLD,36); page.setFont(abc); page.drawString (line, 100, 230); } }
|
The frame needs to know, that the Panel you created is the right panel. So you should invoke setContentPane(panel) on the JFrame.
Then you can use different LayoutManagers to make the Layout. For your case a null layout will be suitable, where you just tell the components where they are. You won't resize the applet anyway and only need 2 components, so no resizing needed, so thats fine.
|
On June 08 2008 06:15 h3r1n6 wrote: The frame needs to know, that the Panel you created is the right panel. So you should invoke setContentPane(panel) on the JFrame.
Then you can use different LayoutManagers to make the Layout. For your case a null layout will be suitable, where you just tell the components where they are. You won't resize the applet anyway and only need 2 components, so no resizing needed, so thats fine.
if you put the null Layout when you resize the window it will fuck up. Either use a GridbagLayout or BorderLayour. You can find how to use them at the java tutorials.
I also recommend that you post this in the java sun forums. They'll be far more helpful than here.
|
United States24480 Posts
On June 08 2008 06:05 alphafuzard wrote: Any gosu programmer help me out plz?
Sounds to me like you don't need a gosu programmer haha.
In java I made a program years ago which would animate a snake moving around. It was kyoot.
|
On June 08 2008 07:01 RtS)Night[Mare wrote: if you put the null Layout when you resize the window it will fuck up. Either use a GridbagLayout or BorderLayour. You can find how to use them at the java tutorials.
I also recommend that you post this in the java sun forums. They'll be far more helpful than here.
He said he wants it in an applet, and you don't resize applets, so.
Don't mistake me, I like the Gridbaglayout, but for 2 elements its total overkill. And yes, actual programmer forums will be more helpfull than this.
|
I have no experience in applets so I have no clue if any of this is valid:
If you're trying to get some organization to your GUI, you need to use a layout. The three most common ones are BorderLayout, FlowLayout, and GridLayout, but there more (the java api is your friend). I'll leave it up to you to investigate the others, but BorderLayout is like this:
North
East Center West
South
Let's say you wanted to put the text box above the hangman picture. You'll need to make a JPanel and give it a BorderLayout with the method JPanel.setLayout(new BorderLayout()). Then you'll put whatever you're containing the hangman image in the middle of the layout by using the method JPanel.add(someGUIComponent, BorderLayout.CENTER). Pick some sort of text box (there are a few to choose from) and add it to the north section in a similar fashion. Let's say you use a JTextField and name it textfield, simply do JPanel.add(textfield, BorderLayout.NORTH). That would basically give you something that looks like:
JTextField
Nothing Hangman Picture Nothing
Nothing
but since the other areas of the BorderLayout have nothing, it would appear just as:
JTextField Hangman Picture
You can pretty much make any layout you want by using layouts within layouts, but I find keeping it as simple as possible to be the way to go.
As for the text box interacting with the user so as to allow them to guess letters, you'd normally use the ActionListener or KeyListener interfaces, but I have no clue if applets run the same way. It'll probably involve getting the user's input, checking to make sure it's a letter (in other words, within a certain range of char values), checking to see if the letter has already been guessed, searching the hangman word to see if the letter is present, and then taking the correct course of action based on the result of the search.
That probably wasn't any help at all but good luck anyway.
Oh crap, my diagrams don't look the same when the post is on the forum.
|
|
|
|