0

I posted a question here not to long ago. I was having different issues with it. I've changed my code up as far as graphics handling goes but it's still not working right.

It's drawing the bottom frame perfectly fine(the one with the buttons and such) however. Above that, where the hangman game itself is supposed to be draw. It's not. It's not drawing anything at all. I don't know what to do. I'm going out of my mind because this is my final project that is due tonight! Can somebody please help me?

Here is my code: (I'm omitting as much code as possible, but if you need the entire program to test yourself, grab it here

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

import java.util.Random;

public class Hangman extends JFrame {

    private Hangman.Hangman_Panel canvas = new Hangman.Hangman_Panel();

    // How many times you can guess the wrong letter till you lose
    static final int DEAD = 7;   

    private int errors;        // amount of errors
    private String message;   // Message displaying either Error or Victory
    private String information; // Secondary Message
    private String RealWord;      // The Real word
    private StringBuffer GuessWord;// The Guessed word
    private Button StartBtn;      // The Restart Button
    private Button GoBtn;         // The Go Button
    private TextField LetterBox; // The LetterBox

    //Contructor
    public Hangman() { 

        this.add(canvas, BorderLayout.CENTER); // Add canvas to center  
        // Create a "Textbox" for the letter guessing
        LetterBox = new TextField();

        JPanel p = new JPanel();
        p.setLayout(new FlowLayout());

        p.add(StartBtn = new Button("Restart"));
        p.add(new Label("Guess a letter"));
        p.add(LetterBox);
        p.add(GoBtn = new Button("Go"));

        add(p, BorderLayout.SOUTH);

    }

    public static void main(String[] args) {

        JFrame frame = new Hangman();

        frame.setSize(300, 400);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    class Hangman_Panel extends JPanel implements ActionListener {

        @Override
        public void paint(Graphics g) {
            super.paintComponent(g);

            int BaseY = 250;

            //THE HANGING STAND
            if (errors > 0) { //1 Error
                g.drawLine(90, BaseY, 200, BaseY); //The ground
                g.drawLine(125, BaseY, 125, BaseY-100); //The bar going up
                g.drawLine(125, BaseY-100, 175, BaseY-100); //The sidebar
                g.drawLine(175, BaseY-100, 175, BaseY-75); //The Rope
            }

            //THE PERSON
            if (errors > 1) { 
               g.drawOval(170, BaseY-75, 10, 12); // The Head       
            }

            if (errors > 2) { 
               g.drawLine(175, BaseY-62, 175, BaseY-45); // The Body    
            }

            if (errors > 3) { 
               g.drawLine(165, BaseY-65, 175, BaseY-55); // Left Arm  
            }

            if (errors > 4) { 
               g.drawLine(185, BaseY-65, 175, BaseY-55); // Right Arm 
            }

            if (errors > 5) { 
               g.drawLine(170, BaseY-30, 175, BaseY-45); //Left Leg       
            }

            if (errors > 6) {  //7 Errors
               g.drawLine(175, BaseY-45, 180, BaseY-30); // Right Left 
            }

            //Show Messages/Errors
            g.drawString(message, 40, BaseY+25);
            g.drawString(information, 25, BaseY+45);
            g.drawString(new String (GuessWord), 140, BaseY-120);

            g.drawString(new String("WELCOME TO HANGMAN!"), 75, 40);
        }

        public void init() {

            //Make buttons event listeners
            StartBtn.addActionListener(this);
            GoBtn.addActionListener(this);

            //Startup the Game
            initGame();

        }

}

Here is also an image of what it's doing:

Not Drawing

The thing that if I'm not mistaken that I am using to add the hangman game part itself to the form are these lines of code:

private Hangman.Hangman_Panel canvas = new Hangman.Hangman_Panel();

//Constructor
public Hangman() { 

    this.add(canvas, BorderLayout.CENTER); // Add canvas to center  
Community
  • 1
  • 1
Richard Paulicelli
  • 129
  • 3
  • 6
  • 23
  • You're still following up with answers to your last question, and this question is really too broad for StackOverflow. – admdrew Dec 09 '13 at 23:20
  • I wouldn't say you have too much code at all - but if you do, it doesn't really make any difference whether it's in pastebin or here. Better to have it here, so that we don't have to click back and forth between the question and the code. I suspect the "too much code" criticism would have been around your failure to make a small program that demonstrates your problem; not around how much space the code takes up on the page. – Dawood ibn Kareem Dec 09 '13 at 23:24
  • What happens if you change `super.paintComponent(g);` to `super.paint(g);` ? – Dawood ibn Kareem Dec 09 '13 at 23:36
  • Same result (I also changed the formatting of the page for you :) If it makes any difference when i start the form these errors show up in the output console: http://pastebin.com/7bFU6e2V – Richard Paulicelli Dec 09 '13 at 23:37
  • OK, I'm a bit busy right now, but I'll have time in about 8 hours or so to investigate this, if nobody else has given you an answer by then. – Dawood ibn Kareem Dec 09 '13 at 23:39
  • I appreciate that but it's due by 11:59 PM tonight lol :x – Richard Paulicelli Dec 09 '13 at 23:39
  • I don't know when that is. I don't know your time zone. – Dawood ibn Kareem Dec 09 '13 at 23:42
  • I don't think your code even compiles. Your Hangman_Panel class did not implement the mandatory `public void actionPerformed(ActionEvent arg0) {` and you don't have definition of `initGame()` method. – gerrytan Dec 09 '13 at 23:42
  • @gerrytan If you noticed I stated that I omitted alot of code for simplicity to view all of my code please go here: http://pastebin.com/66Q70f5X – Richard Paulicelli Dec 09 '13 at 23:45
  • @DavidWallace I'm in the EST zone so it's almost 7 PM here – Richard Paulicelli Dec 09 '13 at 23:46
  • Yeah, I won't be able to help you before your midnight then. Sorry. – Dawood ibn Kareem Dec 09 '13 at 23:47
  • Avoid mixing AWT and Swing components, they don't play well together. Use `JLabel`, `JButton` and `JTextField` instead. – MadProgrammer Dec 09 '13 at 23:56

2 Answers2

3

Quickly trying your code gives an obvious exception where you forgot to check strings for null on your paint method

g.drawString(message, 40, BaseY+25);
g.drawString(information, 25, BaseY+45);
g.drawString(new String (GuessWord), 140, BaseY-120);

I know your homework is due soon but I strongly suggest you to learn how to read exception stack trace, use IDE, and debug your code. Think of it as an investment for your future programming career.

Community
  • 1
  • 1
gerrytan
  • 40,313
  • 9
  • 84
  • 99
2

There are at least three issues which are working against you...

Firstly...

You are overriding paint, but calling super.paintComponent. This is a really bad idea, you've skipped a bunch of links in the paint chain.

Instead, override paintComponent and call super.paintComponent.

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    //...

Secondly...

Some of the values you are relying on in your paint method have not being initialised...

g.drawString(message, 40, BaseY + 25);
g.drawString(information, 25, BaseY + 45);

message and information are null when paint is first called, causing a NullPointerException

As a side note new String("WELCOME TO HANGMAN!") is generally unnecessary. "WELCOME TO HANGMAN!" doesn't need to be past through the String constructor, the compiler will do this for you automatically...

You could use

g.drawString("WELCOME TO HANGMAN!", 75, 40);

instead...(but GuessWord is still null), try initialising these values first, for example...

private String message = "";   // Message displaying either Error or Victory
private String information = ""; // Secondary Message
private String RealWord = "";      // The Real word
private StringBuffer GuessWord = new StringBuffer();// The Guessed word

Thirdly

You are mixing heavy (AWT) and light weight (Swing) components. These don't tend to play to well together.

I'd recommend using JButton, JLabel and JTextField instead

Also, you may find reading through Code Conventions for the Java Programming Language of some help. It will make your code easier for other people to read...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I've done all that you said, there are a couple of this though. First: just using `g.drawString(GuessWord, 140, BaseY - 120);` didn't work because it's a stringbuffer, not a string. Secondly Something now does show up! But it's only the Welcome To Hangman string. Nothing else displays :(. – Richard Paulicelli Dec 10 '13 at 01:02
  • Firstly, I've updated the answer to remove the comment about `GuessWord`, but you could have used `GuessWord.toString()`. I wouldn't expect anything else to show up as 1- You never call `init` on the `Hangman_Panel`, but you could move this to the constructor, nor have you changed the state of the game... – MadProgrammer Dec 10 '13 at 01:29
  • I'd either move it to or call it from `Hangman_Panel`'s constructor – MadProgrammer Dec 10 '13 at 01:53
  • I tried doing this: `public Hangman_Panel() { init(); }` and it gave me a nullpointerexception. So something else has gone wrong. – Richard Paulicelli Dec 10 '13 at 02:08
  • When you call `init` `StartBtn` and `GoBtn` are `null` as they've not being initialized yet. Not sure you need a reference to them there or not – MadProgrammer Dec 10 '13 at 02:23
  • Okay, so i did this `private JButton GoBtn = new JButton("Go");` in the when I assigned it at the beginning in the Hangman Class (not the constructor) – Richard Paulicelli Dec 10 '13 at 02:38
  • It depends. There isn't enough code to 100%, but my gut filling is `Hangman_Panel` doesn't need to know about the buttons... – MadProgrammer Dec 10 '13 at 02:45
  • I don't understand but the problem is still occuring, would you like to send you my project file zipped? or here's all of the code in full as of right now http://pastebin.com/0RRVTBrb – Richard Paulicelli Dec 10 '13 at 02:51