ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I just have to do this, which leads to another thing, then I can beta my program!
I'm getting a head of myself.
I have this hangman program, which needs to paint a scaffold/gallows and obviously the guy. Problem: when I try to paint this: nothing turned up. disapointed, I tested a LARGE rectangle, only a small square turned up.
will post the code.
if all else fails, will have to do a complete rewrite.
import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
import javax.swing.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
public class hangman extends JFrame implements ActionListener {
/**
* hangman.java
* @author Alison
* @version .6 (alpha)
*
*/
//initialize objects
//the buttons for the alphabet
JButton a = new JButton("A");
JButton b = new JButton("B");
JButton c = new JButton("C");
JButton d = new JButton("D");
JButton e = new JButton("E");
JButton f = new JButton("F");
JButton g = new JButton("G");
JButton h = new JButton("H");
JButton i = new JButton("I");
JButton j = new JButton("J");
JButton k = new JButton("K");
JButton l = new JButton("L");
JButton m = new JButton("M");
JButton n = new JButton("N");
JButton o = new JButton("O");
JButton p = new JButton("P");
JButton q = new JButton("Q");
JButton r = new JButton("R");
JButton s = new JButton("S");
JButton t = new JButton("T");
JButton u = new JButton("U");
JButton v = new JButton("V");
JButton w = new JButton("W");
JButton x = new JButton("X");
JButton y = new JButton("Y");
JButton z = new JButton("Z");
//the menu stuff
//game menu declarations
JMenuItem nuevo = new JMenuItem("New");
JMenuItem salida = new JMenuItem("Exit");
JMenuItem adicion = new JMenuItem("Add New Words");
//network or singleplayer choice
JRadioButtonMenuItem singp = new JRadioButtonMenuItem("Single Player", true);
JRadioButtonMenuItem netwk = new JRadioButtonMenuItem("Network");
//help menu declarations
JMenuItem ayuda = new JMenuItem("help me!");
JMenuItem como = new JMenuItem("how to play");
JMenuItem quien = new JMenuItem("credits");
//settings menu declarations
JRadioButtonMenuItem easy = new JRadioButtonMenuItem("easy");
JRadioButtonMenuItem medi = new JRadioButtonMenuItem("medium");
JRadioButtonMenuItem hard = new JRadioButtonMenuItem("hard");
//checkbox for hints on/off
JCheckBoxMenuItem hintson = new JCheckBoxMenuItem("On", true);
//the popup window stuff
//the help popups. The labels are changed and added to thelpish(the panel)accordingly.
JFrame thelps = new JFrame();
JButton quit = new JButton("OK");
JLabel fur = new JLabel("quote by Micah: I think it had something to do with fish.");
JLabel cave = new JLabel("quote by Peter: Thats what I think.");
JLabel saurus = new JLabel("these guys are the best");
JLabel storm = new JLabel("Javafriends 2003-2004.");
JLabel kid = new JLabel("go the .cow extension!");
JLabel nap = new JLabel("hail to you, newbie or old-hat programer, reading my code. ");
JLabel cabin = new JLabel("I AM A GEEK!!!! and proud of it.");
// The stuff for Word Builder.
JFrame wbuilder = new JFrame();
// the components
JTextField word = new JTextField(25);
JLabel words = new JLabel("Enter Word:", JLabel.RIGHT);
JTextArea hint = new JTextArea(3, 25);
JLabel hints = new JLabel("Enter Hint:", JLabel.RIGHT);
JLabel setss = new JLabel("Difficulty level", JLabel.RIGHT);
JButton add = new JButton("add word");
JButton nwe = new JButton("new entry");
JButton clr = new JButton("clear all fields");
JButton ext = new JButton("done");
//"new word" warning popup stuff
JFrame warning = new JFrame();
JLabel aresure = new JLabel("Do you want to make a new word?", JLabel.CENTER);
JButton yis = new JButton("Yes");
JButton noo = new JButton("No");
JButton sav = new JButton("Save then make new word");
// The stuff for a networked game
JFrame popupnet = new JFrame();
JFrame netsetup = new JFrame();
//the components for popupnet
JRadioButton me = new JRadioButton("I'll contact him");
JRadioButton him = new JRadioButton("He'll contact me");
JButton donit = new JButton("OK");
//the components for netsetup
JTextField ipadd = new JTextField(25);
JLabel ipask = new JLabel("What is your friend's IP address?", JLabel.RIGHT);
JButton aldon = new JButton("OK");
// the picture that replaces the java cup icon. (in the corner)
ImageIcon icon = new ImageIcon("icohang.gif");
Image image = icon.getImage();
//this is the string that is used to set dificulty level
static String dif = "e";
//this is the "easil"
JPanel center = new JPanel();
//this is the canvas
JPanel canvas = new painter();
//hangman method
public hangman() {
super("Hangman, by JavaFriends");
setIconImage(image);
setSize(700, 550);
//the menus for the game
JMenu gemnu = new JMenu("Game");
gemnu.setMnemonic(KeyEvent.VK_G);
gemnu.add(nuevo);
gemnu.addSeparator();
/* this is for version 2
//group the radio buttons
ButtonGroup apple = new ButtonGroup();
apple.add(singp);
apple.add(netwk);
gemnu.add(singp);
gemnu.add(netwk);
gemnu.addSeparator(); */
gemnu.add(adicion);
gemnu.addSeparator();
gemnu.add(salida);
JMenu semnu = new JMenu("Settings");
semnu.setMnemonic(KeyEvent.VK_S);
//group the radio buttons
ButtonGroup orange = new ButtonGroup();
orange.add(easy);
orange.add(medi);
orange.add(hard);
semnu.add(easy);
semnu.add(medi);
semnu.add(hard);
semnu.addSeparator();
JMenu memnu = new JMenu("Hints");
memnu.setMnemonic(KeyEvent.VK_M);
memnu.add(hintson);
semnu.add(memnu);
JMenu hemnu = new JMenu("Help!");
hemnu.setMnemonic(KeyEvent.VK_H);
hemnu.add(ayuda);
hemnu.add(como);
hemnu.add(quien);
//the menubar
JMenuBar mb = new JMenuBar();
mb.add(gemnu);
mb.add(semnu);
mb.add(hemnu);
setJMenuBar(mb);
//the Action Listeners
// create button action listeners
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
e.addActionListener(this);
f.addActionListener(this);
g.addActionListener(this);
h.addActionListener(this);
i.addActionListener(this);
j.addActionListener(this);
k.addActionListener(this);
l.addActionListener(this);
m.addActionListener(this);
n.addActionListener(this);
o.addActionListener(this);
p.addActionListener(this);
q.addActionListener(this);
r.addActionListener(this);
s.addActionListener(this);
t.addActionListener(this);
u.addActionListener(this);
v.addActionListener(this);
w.addActionListener(this);
x.addActionListener(this);
y.addActionListener(this);
z.addActionListener(this);
//add action listener for the menus
nuevo.addActionListener(this);
salida.addActionListener(this);
adicion.addActionListener(this);
ayuda.addActionListener(this);
como.addActionListener(this);
quien.addActionListener(this);
//radio button action listeners
singp.addActionListener(this);
netwk.addActionListener(this);
easy.addActionListener(this);
medi.addActionListener(this);
hard.addActionListener(this);
//checkbox action listener
//hintson.addItemListener(this);
//add action listener for help ok button
quit.addActionListener(this);
//the action listener for the word builder
add.addActionListener(this);
nwe.addActionListener(this);
clr.addActionListener(this);
ext.addActionListener(this);
//add action listener for network stuff
me.addActionListener(this);
him.addActionListener(this);
donit.addActionListener(this);
aldon.addActionListener(this);
//add action listener for popup new "are you sure?"
yis.addActionListener(this);
noo.addActionListener(this);
sav.addActionListener(this);
//add alphabet buttons to panel with grid layout "grim"
JPanel al = new JPanel();
GridLayout grim = new GridLayout(2, 13);
al.setLayout(grim);
//set button color to orange
a.setBackground(Color.ORANGE);
b.setBackground(Color.ORANGE);
c.setBackground(Color.ORANGE);
d.setBackground(Color.ORANGE);
e.setBackground(Color.ORANGE);
f.setBackground(Color.ORANGE);
g.setBackground(Color.ORANGE);
h.setBackground(Color.ORANGE);
i.setBackground(Color.ORANGE);
j.setBackground(Color.ORANGE);
k.setBackground(Color.ORANGE);
l.setBackground(Color.ORANGE);
m.setBackground(Color.ORANGE);
n.setBackground(Color.ORANGE);
o.setBackground(Color.ORANGE);
p.setBackground(Color.ORANGE);
q.setBackground(Color.ORANGE);
r.setBackground(Color.ORANGE);
s.setBackground(Color.ORANGE);
t.setBackground(Color.ORANGE);
u.setBackground(Color.ORANGE);
v.setBackground(Color.ORANGE);
w.setBackground(Color.ORANGE);
x.setBackground(Color.ORANGE);
y.setBackground(Color.ORANGE);
z.setBackground(Color.ORANGE);
//add buttons to panel "al"-phabet
al.add(a);
al.add(b);
al.add(c);
al.add(d);
al.add(e);
al.add(f);
al.add(g);
al.add(h);
al.add(i);
al.add(j);
al.add(k);
al.add(l);
al.add(m);
al.add(n);
al.add(o);
al.add(p);
al.add(q);
al.add(r);
al.add(s);
al.add(t);
al.add(u);
al.add(v);
al.add(w);
al.add(x);
al.add(y);
al.add(z);
//this acts as a spacer (for the graphics stuff)
JPanel menu = new JPanel();
FlowLayout flem = new FlowLayout(FlowLayout.LEFT);
menu.setLayout(flem);
//add canvas to center
center.add(canvas);
//this is the main window
JPanel pain = new JPanel();
pain.setLayout(new BorderLayout(5, 5));
pain.setBackground(Color.BLUE);
pain.add("South", al);
pain.add("North", menu);
pain.add("Center", center);
setContentPane(pain);
show();
//THE POPUP STUFF...
//the panel for the jframe thelps for the help, etc.
JPanel thelpish = new JPanel();
thelpish.add(fur);
thelpish.add(cave);
thelpish.add(saurus);
thelpish.add(storm);
thelpish.add(kid);
thelpish.add(nap);
thelpish.add(cabin);
thelpish.add(quit);
thelps.setContentPane(thelpish);
thelps.setIconImage(image);
thelps.setBounds(100, 100, 400, 200);
quit.setBackground(Color.ORANGE);
//the stuff for the wordbuilder
JPanel form = new JPanel();
FlowLayout frost = new FlowLayout(FlowLayout.CENTER);
form.setLayout(frost);
hint.setLineWrap(true);
hint.setWrapStyleWord(true);
form.add(words);
form.add(word);
form.add(hints);
form.add(hint);
form.add(setss);
JComboBox sets = new JComboBox();
sets.addItem("easy");
sets.addItem("medium");
sets.addItem("hard");
sets.setBackground(Color.BLUE);
sets.setForeground(Color.WHITE);
form.add(sets);
JPanel buts = new JPanel();
buts.setLayout(frost);
add.setBackground(Color.ORANGE);
nwe.setBackground(Color.ORANGE);
clr.setBackground(Color.ORANGE);
ext.setBackground(Color.ORANGE);
buts.add(add);
buts.add(nwe);
buts.add(clr);
buts.add(ext);
JPanel blank = new JPanel();
JPanel ouch = new JPanel();
ouch.setLayout(new BorderLayout(5, 5));
ouch.setBackground(Color.BLUE);
ouch.add("North", blank);
ouch.add("Center", form);
ouch.add("South", buts);
wbuilder.setContentPane(ouch);
wbuilder.setIconImage(image);
wbuilder.setBounds(250, 225, 400, 200);
// the popup warning for the new button
JPanel warnu = new JPanel();
warnu.add(aresure);
warnu.add(yis);
warnu.add(noo);
warnu.add(sav);
yis.setBackground(Color.ORANGE);
noo.setBackground(Color.ORANGE);
sav.setBackground(Color.ORANGE);
warning.setBounds(100, 100, 200, 200);
warning.setContentPane(warnu);
warning.setIconImage(image);
//the networked game setup gui stuff
JPanel newtwork = new JPanel();
donit.setBackground(Color.ORANGE);
//group the radio buttons
ButtonGroup net = new ButtonGroup();
net.add(me);
net.add(him);
newtwork.add(me);
newtwork.add(him);
newtwork.add(donit);
popupnet.setBounds(100, 100, 100, 200);
popupnet.setContentPane(newtwork);
popupnet.setIconImage(image);
JPanel newtipask = new JPanel();
GridLayout grip = new GridLayout();
newtipask.setLayout(grip);
aldon.setBackground(Color.ORANGE);
newtipask.add(ipask);
newtipask.add(ipadd);
newtipask.add(aldon);
netsetup.setBounds(100, 100, 200, 200);
netsetup.setContentPane(newtipask);
netsetup.setIconImage(image);
}
/*
* public xmlstuff() {
* //this is for all the xml database stuff that I'll have for the words.
* /
*/
//exiting the program, action listeners etc
public static void main(String[] arguments) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use system look and feel" + "look and feel: " + e);
}
JFrame frame = new hangman();
ExitWindow exit = new ExitWindow();
frame.addWindowListener(exit);
frame.show();
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == a) {
a.setEnabled(false);
}
if (source == b) {
b.setEnabled(false);
}
if (source == c) {
c.setEnabled(false);
}
if (source == d) {
d.setEnabled(false);
}
if (source == e) {
e.setEnabled(false);
}
if (source == f) {
f.setEnabled(false);
}
if (source == g) {
g.setEnabled(false);
}
if (source == h) {
h.setEnabled(false);
}
if (source == i) {
i.setEnabled(false);
}
if (source == j) {
j.setEnabled(false);
}
if (source == k) {
k.setEnabled(false);
}
if (source == l) {
l.setEnabled(false);
}
if (source == m) {
m.setEnabled(false);
}
if (source == n) {
n.setEnabled(false);
}
if (source == o) {
o.setEnabled(false);
}
if (source == o) {
o.setEnabled(false);
}
if (source == p) {
p.setEnabled(false);
}
if (source == q) {
q.setEnabled(false);
}
if (source == r) {
r.setEnabled(false);
}
if (source == s) {
s.setEnabled(false);
}
if (source == t) {
t.setEnabled(false);
}
if (source == u) {
u.setEnabled(false);
}
if (source == v) {
v.setEnabled(false);
}
if (source == w) {
w.setEnabled(false);
}
if (source == x) {
x.setEnabled(false);
}
if (source == y) {
y.setEnabled(false);
}
if (source == z) {
z.setEnabled(false);
}
// action stuff from the menus
if (source == salida) {
System.exit(0);
}
if (source == nuevo) {
a.setEnabled(true);
b.setEnabled(true);
c.setEnabled(true);
d.setEnabled(true);
e.setEnabled(true);
f.setEnabled(true);
g.setEnabled(true);
h.setEnabled(true);
i.setEnabled(true);
j.setEnabled(true);
k.setEnabled(true);
l.setEnabled(true);
m.setEnabled(true);
n.setEnabled(true);
o.setEnabled(true);
p.setEnabled(true);
q.setEnabled(true);
r.setEnabled(true);
s.setEnabled(true);
t.setEnabled(true);
u.setEnabled(true);
v.setEnabled(true);
w.setEnabled(true);
x.setEnabled(true);
y.setEnabled(true);
z.setEnabled(true);
}
if (source == adicion) {
wbuilder.setTitle("Add a word");
wbuilder.show();
}
if (source == quien) {
thelps.setTitle("Credits");
fur.setText(" Hangman was written by me, Alison as a part of my ");
cave.setText("Java Friends AP class, during the 2003-2004 school year ");
saurus.setText("Special Thanks go to Micah and Peter, who made fun");
storm.setText("of my code, but taught me a lot in the process.");
kid.setText("Thanks guys.");
nap.setText("Thanks also go to my family, who put up with me, and my friends, who, even ");
cabin.setText("though they have no idea what I'm doing, but have been very supportive. ");
thelps.show();
repaint();
}
if (source == como) {
thelps.setTitle("How to Play");
fur.setText(" Select new game from the game menu. The default level of dificulty is easy. ");
cave.setText("If you want to change it, change it through the settings menu. You can also ");
saurus.setText("turn Hints on or off. After you select the dificulty level, a word will be ");
storm.setText("drawn from the database, and you will have to guess what it is. Every wrong ");
kid.setText("answer, another body part will be drawn. If the whole body is drawn, you lose!");
nap.setText("For a networked game, choose whether you or your friend will make up a word. ");
cabin.setText("Then follow the same format, picking a letter, trying to guess the word.");
thelps.show();
repaint();
}
if (source == ayuda) {
thelps.setTitle("Help");
fur.setText(" For help: email Alison at t2rods@hotmail.com. otherwise the help-file is ");
cave.setText("hosted at http://www.freewebz.com/titanium_gee.../hanghelp.html ");
saurus.setText("or you could write your own. if you do, please email it to me. ");
storm.setText("translations are also ok.");
kid.setText(" will have the spanish one done soon, so do other languages please.");
nap.setText(".");
cabin.setText("'");
thelps.show();
repaint();
}
if (source == netwk) {
popupnet.setTitle("Networked Game");
popupnet.show();
}
// if (source == singp) {
// }
//stuff for closing the popups.
if (source == quit) {
thelps.dispose();
}
/* if (source == add) {
//xml stuff for adding the new word to the word file
xmlstuffs()
}
*/
if (source == nwe) {
warning.show();
}
if (source == clr) {
word.setText(" ");
hint.setText(" ");
}
if (source == ext) {
wbuilder.dispose();
}
// for the new "Are you sure" buttons
if (source == yis) {
word.setText(" ");
hint.setText(" ");
warning.dispose();
}
else if (source == noo) {
warning.dispose();
}
else if (source == sav) {
//xml stuff
warning.dispose();
}
//networking stuff: for version 2.0
if (source == donit) {
if (source == me) {
popupnet.dispose();
netsetup.setTitle("Show me where to connect");
netsetup.show();
}
else if (source == him) {
//for now, close the window
popupnet.dispose();
netsetup.dispose();
//get ready for a network connection
}
}
if (source == aldon) {
netsetup.dispose();
}
//the stuff for setting dificulty level
if (source == easy) {
dif = "e";
}
if (source == medi) {
dif = "m";
}
if (source == hard) {
dif = "h";
}
}
// public static void itemPerformed(Itemevent e);
}
class painter extends JPanel {
//the painting stuff
painter() {
}
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D)comp;
comp2D.setColor(Color.BLACK);
comp2D.fillRect(0,0, 10000, 10000);
}
}
//thanks for reading my code! 2003-2004
Last edited by titanium_geek; 12-29-2003 at 05:43 PM.
It looks like you are not setting the size on the canvas properly. Not all layout managers do that for you. Try calling canvas.setPreferredSize(new Dimension(<whatever>)) before you add it to the panel.
Are you somewhat new to programming in Java?
Please don't take offense to this question - but I have a couple suggestions for you:
You should initialize your instance variables in the constructor. I know Java lets you get away with this but its good practice.
Also, you should probably have an array of JButton objects to make your code more orderly.
You might want to add some more methods instead of doing everything in the constructor - it would make your code more readable.
Also, could you not simply use: "source.setEnabled(false);" instead of checking all the letters of the alphabet in separate if's??
Anyways, just simple things like that - its mainly about readability, your programming otherwise seems fine.
Sorry if I'm presuming too much here - I don't want to offend you - just offer some suggestions about things that have helped me.
About your question, what is it exactly you are trying to "add stuff to" with if statements?
yes it is my first "real" program. The reason I'm not using arrays in my program, is partly because I have already written my program twice (read: rewritten) and don't feel like doing it again... and to have a dig at my friend, who is in the same class, uses arrays but has less idea about what his code does. heheh. will use arrays when I fully comprehend them.
You should initialize your instance variables in the constructor. I know Java lets you get away with this but its good practice.
?? what do you mean?
will try the source.setEnabled(false); then. thanks.
Please don't be concerned about stepping on my toes, I am a complete n00b and have little or no idea what I'm doing. ( )
You should initialize your instance variables in the constructor. I know Java lets you get away with this but its good practice.
?? what do you mean?
[/B]
For example,
if you declare your JButtons globally, then do it like this:
private JButton myButton;
public hangman ( ) {
myButton = new JButton("A");
}
That way, when you actually create the hangman object (by the way, your class names should start with Capitals - I know that's picky), you actually "create" everything that has to do with that object - since the constructor is the first thing that is 'called' when you create an object, as in:
Hangman h = new Hangman();
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.