Reputation: 111
I am new to programming and am having difficulty with this problem. I am trying to build a wall based on the number entered in the JTextField but the number cannot exceed 20. I cannot get my error message to display nor can I get my brick wall to build. Could someone please help me figure out what I am doing wrong?
public class Wall extends JApplet implements ActionListener {
JTextField enter;
Boolean submit;
JLabel bricks;
JButton build;
JPanel top;
Image zombie;
int value;
public void init() {
setLayout(new BorderLayout());
top = new JPanel();
build = new JButton("AHHHHHH...ZOMBIES!"); //button for building wall
bricks = new JLabel("Enter between 1 & 20 rows to contruct:");
enter = new JTextField(2);
top.add(build); //add zombie button
top.add(bricks); //add intructions
top.add(enter); //add text field
add(top, BorderLayout.NORTH);
build.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == build) {
int value = Integer.parseInt(build.getText());
if (value > 0 && value < 21) {
submit = true;
repaint();
} else {
submit = false;
repaint();
}
}
}
public void paint(Graphics g) {
super.paint(g);
//add zombie image
Image zombie = getImage(getCodeBase(), "Zombie.jpg");
g.drawImage(zombie, 0, 45, this);
if (submit = false) //add error message
{
g.setColor(Color.WHITE);
g.setFont(new Font("TimesRoman", Font.BOLD, 40));
g.drawString("You must enter a number between 1 & 20!", 400, 100); //add message
}
int brick_width = 50;
int brick_height = 20;
int spacing = 1;
int x = 0;
while (x < 21) {
drawBrick(g, nextInt(brick_width + spacing), nextInt(brick_height + spacing));
x = x + getWidth() + 50;
x = x - 25 + getWidth() + 50;
x++;
}
}
public void drawBrick(Graphics g, int x, int y) {
g.setColor(new Color(150, 0, 0));
g.fillRect(0, 635, 50, 20);
}
}
Upvotes: 4
Views: 146
Reputation: 13465
Two issues:
1) Your submit variable is not initialized, you should initialize it to true/false. If you dont want to initialize it, you can simple try with
if(submit!=null && !submit)
2) Secondly, "=" is used for assigning a value where as "==" is used for comparing Change
if (submit = false) //add error message
{
g.setColor(Color.WHITE);
g.setFont(new Font("TimesRoman", Font.BOLD, 40));
g.drawString("You must enter a number between 1 & 20!", 400, 100); //add message
}
to
if (submit == false) //add error message
{
g.setColor(Color.WHITE);
g.setFont(new Font("TimesRoman", Font.BOLD, 40));
g.drawString("You must enter a number between 1 & 20!", 400, 100); //add message
}
Upvotes: 0
Reputation: 46841
Instead of painting on top-level containers such as JApplet
directly, you have to use JPanel
and paint on it. Just override paintComponent()
method of JPanel()
. Never forget to call super.paintComponent(g)
in overridden method.
sample code:
top = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
...
// your custom painting code goes here
}
};
Note:
submit == false
and !submit
are the correct way to check it but it still will result in NullPointerException
because you have never initialized instance variable submit
.
Use primitive boolean
instead of Boolean
to avoid such exception or initialize it properly.
Boolean submit = false;
Upvotes: 4
Reputation: 9894
To solve your error display issue,
submit = false
should be submit == false
=
is for assignment
==
is for comparison
Also, initialize your variable to avoid null pointer exception:
boolean submit = false;
Upvotes: -1
Reputation: 137
Change your if statement to:
if (submit == false)
That'll get your error message to pop up
Upvotes: 0