Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Norm

Thread: Java applet newbie question of the day

  1. #1
    o2cool is offline Member
    Join Date
    Mar 2012
    Location
    earth
    Posts
    9
    Rep Power
    0

    Default 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;
    }
    }
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    o2cool is offline Member
    Join Date
    Mar 2012
    Location
    earth
    Posts
    9
    Rep Power
    0

    Default 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.

  4. #4
    o2cool is offline Member
    Join Date
    Mar 2012
    Location
    earth
    Posts
    9
    Rep Power
    0

    Default Re: Java applet newbie question of the day

    Quote Originally Posted by Norm View Post
    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

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Java applet newbie question of the day

    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
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    o2cool is offline Member
    Join Date
    Mar 2012
    Location
    earth
    Posts
    9
    Rep Power
    0

    Default Re: Java applet newbie question of the day

    Quote Originally Posted by Norm View Post
    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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    o2cool is offline Member
    Join Date
    Mar 2012
    Location
    earth
    Posts
    9
    Rep Power
    0

    Default Re: Java applet newbie question of the day

    figured it out, i forgot the == for while loops

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Java applet newbie question of the day

    That should help the logic of the loops a bit.
    o2cool likes this.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    o2cool is offline Member
    Join Date
    Mar 2012
    Location
    earth
    Posts
    9
    Rep Power
    0

    Default 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;
    }
    }
    }
    }

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Java applet newbie question of the day

    Quote Originally Posted by o2cool View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. A newbie Java question
    By Swick in forum New To Java
    Replies: 8
    Last Post: 02-19-2012, 07:58 AM
  2. Replies: 7
    Last Post: 01-01-2012, 12:29 PM
  3. A newbie Java question
    By jeftphph in forum New To Java
    Replies: 17
    Last Post: 07-14-2011, 09:56 AM
  4. Java newbie question on linkedlist index search
    By Mjall in forum New To Java
    Replies: 5
    Last Post: 03-25-2011, 10:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •