Java applet newbie question of the day
I am trying to make a checkerboard for my school assignment, here is my code and for some reason i cannot get it to draw the next line(s)
Any suggestions? and thank you in advance :)
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class Checker_Attempt_2 extends JApplet
{
public void init()
{
this.setSize(500, 500);
}
public void paint(Graphics fun)
{
int x, y, c, d;
boolean stBl = true;
Color c1, c2;
c1 = new Color(0x000000); //black
c2 = new Color(0xFFFFFF); //white
x = 0; //quard x
y = 0; //quard y
d = 40; //size of checkers
int i;
while (y != 8 * d)
{
while (x != 8 * d)
{
if (stBl = false)
{
for (i = 0; i < 4; i++)
{
fun.setColor(c1);
fun.fillRect(x, y, d, d);
x = x + d;
fun.setColor(c2);
fun.fillRect(x, y, d, d);
x = x + d;
}
stBl = true;
y = y + d;
}
if (stBl = true)
{
for (i = 0; i < 4; i++)
{
fun.setColor(c2);
fun.fillRect(x, y, d, d);
x = x + d;
fun.setColor(c1);
fun.fillRect(x, y, d, d);
x = x + d;
}
stBl = false;
y = y + d;
}
}
}
}
}
Re: Java applet newbie question of the day
Add some println statements in the loops in the paint() method to show the values of the varibles used there to control the loop and the location of where the lines are drawn. The printed output will show you what the computer is seeing and help you fix your code. Also print out the values used to control the drawing: stb1
Re: Java applet newbie question of the day
ok, found out to get the next row, i just removed the x while loop but now i cannot get the lines to alternate. any ideas? i ran into this last time and just wrote about 200 lines to do the same thing.
Re: Java applet newbie question of the day
Quote:
Originally Posted by
Norm
Add some println statements in the loops in the paint() method to show the values of the varibles used there to control the loop and the location of where the lines are drawn. The printed output will show you what the computer is seeing and help you fix your code. Also print out the values used to control the drawing: stb1
sout will not allow me to do anything says it needs ; but has one at the end
Re: Java applet newbie question of the day
Quote:
get the lines to alternate
Use a variable to keep track of the line. Change to the the other line when the variable's value changes.
alternate the lines by testing its value: If true do this line, if false do that line. Change its value at the end of each line: boolVal = !boolVal; //toggle the value
Re: Java applet newbie question of the day
Quote:
Originally Posted by
Norm
Use a variable to keep track of the line. Change to the the other line when the variable's value changes.
alternate the lines by testing its value: If true do this line, if false do that line. Change its value at the end of each line: boolVal = !boolVal; //toggle the value
i can just run it through the debugger, it changes the boolean value but does not run the if or while statement for the correct value, just checks, weather its true or false it runs the same lines until its done
Re: Java applet newbie question of the day
If you can see when the variables as they change values, then you should see what is happening and why.
In some cases I prefer using println so you can see what happens over many iterations of the loop without having to tend to the tracing too closely.
Re: Java applet newbie question of the day
figured it out, i forgot the == for while loops
Re: Java applet newbie question of the day
That should help the logic of the loops a bit.
Re: Java applet newbie question of the day
Finished code if anyone cares
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
public class Checker_Attempt_2 extends JApplet
{
public void init()
{
this.setSize(500, 500);
}
public void paint(Graphics fun)
{
int x, y, c, d, i, margin;
boolean stBl = true;
Color c1, c2;
c1 = new Color(0x000000); //black
c2 = new Color(0xFFFFFF); //white
x = 0; //quard x
y = 0; //quard y
//margin = 10;
//x = margin;
//c = 0; // row counter even num start with white odd num start with black and worthless
String input = JOptionPane.showInputDialog(null, "How big should the squares be, use a real number greater than 0 \n Input: ", "");
d = Integer.parseInt(input);
while (y != 8 * d)
{
while (stBl == true)
{
for (i = 0; i < 4; i++)
{
fun.setColor(c2);
fun.fillRect(x, y, d, d);
x = x + d;
fun.setColor(c1);
fun.fillRect(x, y, d, d);
x = x + d;
}
stBl = false;
y = y + d;
x = 0;
}
while (stBl == false)
{
for (i = 0; i < 4; i++)
{
fun.setColor(c1);
fun.fillRect(x, y, d, d);
x = x + d;
fun.setColor(c2);
fun.fillRect(x, y, d, d);
x = x + d;
}
stBl = true;
y = y + d;
x = 0;
}
}
}
}
Re: Java applet newbie question of the day
Quote:
Originally Posted by
o2cool
Finished code if anyone cares
People might care more if you take the trouble to go through the site FAQs and learn how to post code so that it retains its formatting.
db