Setcolor State

Newb Java Programming Help?

Just started trying out Java, and I came across something weird.

Here is my code:

import java.applet.*;
import java.awt.*;

public class test extends Applet {
int width, height;
boolean j;
public void init() {
width = getSize().width;
height = getSize().height;
setBackground(Color.black);

}
public void paint(Graphics g) {
g.setColor(Color.red);
j = false;
if (j = true){
g.fillRect(10, 10, 100, 15);
}
g.drawRect(10, 10, 100, 15);
}

}

Even though I stated that j = false, it still shows a filled rectangle. Why is this?

Well, you set j to true in your if statement.

Remember that = is used to assign a value to a variable, while == is used to compare two values. This is very important.

That said, j is a boolean value. You don’t need to compare it to another boolean value to use it in an if statement. Just do this:

if(j) {
g.fillRect(10, 10, 100, 15);
}

The Negative Effects of Hair Dye