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