Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: paint error, with a simple boolean

  1. #1
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default paint error, with a simple boolean

    its extremely odd, to me anyway, i tried to put a section of the paint method, thats in a frame, into a conditional so that it only executes the first time, to cut down on the flickering when the animation happens, but the paint methods inside it dont execute, when i put in system.out.println to test it, it executes but nothing else in the conditional. i used this method in an applet and it worked, i dont know why...

    here is code:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class CFrame extends Fram implements ActionListener
    {
    Image image1,image2;
    Button b = new Button("Enter");
    Button show = new Button("Show Answer");
    Button reset = new Button("RESET");
    TextField entry = new TextField("",5);
    Timer timer = new Timer();
    int xloc=180,x=0,yloc=490,rate;
    boolean first=false,q=true;
    Font big = new Font("Serif",Font.BOLD,40);
    Label ans = new Label("Correct answer is: d(theta)/dt = 1 rad/sec");
    Label tryt = new Label("");
    Label rr = new Label("Related Rates");
    boolean att=false, cor = false;;
    CFrame()
    {
    super("CFRAME");
    setSize(1000,850);
    setLayout(null);
    add(b);
    add(show);
    add(entry);
    add(rr);
    add(reset);
    reset.addActionListener(this);
    reset.setBounds(50,130,60,30);
    b.setBounds(630,50,60,30);
    b.addActionListener(this);
    show.setBounds(630,100,80,40);
    show.addActionListener(this);
    entry.setBounds(710,50,60,25);
    rr.setBounds(25,25,250,100);
    rr.setFont(big);
    rr.setBackground(Color.BLUE);
    setVisible(true);
    setBackground(Color.BLUE);
    //System.out.print(b.getX());
    //repaint();
    //start();
    }
    public void paint(Graphics g)
    {
    Toolkit tool = Toolkit.getDefaultToolkit();
    image2 = tool.getImage("E:\\programmin\\WhiteBook\\pics\\ax is.jpg");
    g.drawImage(image2,50,150,this);
    image2 = tool.getImage("E:\\programmin\\WhiteBook\\pics\\bo lt.jpg");
    g.drawImage(image2,110,470,this);
    image2 = tool.getImage("E:\\programmin\\WhiteBook\\pics\\wo rk.jpg");
    g.drawImage(image2,400,500,this);
    g.setFont(big);
    //g.setColor(Color.BLACK);
    //g.drawString("Related Rates",25,25);
    if(first==true)
    {

    // here is conditional that im talking about

    if(q==true)
    {
    //System.out.print(b.getX());
    //image1 = tool.getImage("E:\\programmin\\WhiteBook\\pics\\bo lt.jpg");
    //System.out.print(":gjdfgjkl");
    //g.drawImage(image1,900,985,this);
    g.fillOval(900,980,50,50);
    q=false;
    }
    g.setColor(Color.YELLOW);
    g.fillOval(xloc,yloc,25,25);
    if(x>=52)
    {
    g.setColor(Color.RED);
    g.drawLine(485,500,xloc,yloc+12);
    }
    //g.setColor(Color.BLACK);
    //g.drawLine(500,200,xloc,yloc+25);
    try
    {
    if(x<50)
    {
    xloc+=5;
    //yloc-=x^2;
    ++x;
    Thread.sleep(10);
    }
    if(x>=50&&x<77)
    {
    xloc+=10;
    yloc-=(x-49)^2;
    ++x;
    Thread.sleep(100);
    }
    if(x==77)
    {
    //g.setColor(Color.MAGENTA);
    //g.drawLine(485,500,xloc,yloc+12);
    if(att==false)
    {
    add(ans);
    ans.setBounds(330,150,180,30);
    ans.setForeground(Color.RED);
    g.setColor(Color.GREEN);
    g.drawArc(340,450,200,200,90,-38);
    ++x;
    }
    else
    {
    if(cor==true)
    {
    add(tryt);
    tryt.setBounds(330,150,180,30);
    g.setColor(Color.GREEN);
    g.drawArc(340,450,200,200,90,-38);
    ++x;
    }
    else
    {
    add(tryt);
    tryt.setBounds(330,150,180,30);
    add(ans);
    ans.setBounds(330,150,180,30);
    g.setColor(Color.GREEN);
    if(Integer.parseInt(entry.getText())>1)
    {
    g.drawArc(340,450,200,200,90,-45);
    System.out.print(b.getX());
    }
    else
    {
    if(Integer.parseInt(entry.getText())<1)
    {
    g.drawArc(340,450,200,200,90,-25);
    System.out.print(b.getX());
    }
    }
    ++x;
    }
    }

    }
    }
    catch(InterruptedException e)
    {
    }
    if(x<78)
    repaint();
    }

    }
    public void start()
    {
    timer.schedule(new TimerTask()
    {
    public void run()
    {
    xloc-=5;
    repaint();
    //System.out.println("hey");
    timer.cancel();
    }
    },500,500);

    }
    public void actionPerformed(ActionEvent e)
    {
    String buttonClicked=e.getActionCommand();
    if(buttonClicked.equals("Enter"))
    {
    att=true;
    if(1==Integer.parseInt(entry.getText()))
    {
    tryt.setText("Correct");
    first=true;
    cor=true;
    repaint();
    }
    else
    {
    tryt.setText("InCorrect");
    first=true;
    repaint();
    }
    }
    else
    {
    if(buttonClicked.equals("Show Answer"))
    {
    first=true;
    repaint();
    }
    else
    {
    if(buttonClicked.equals("RESET"))
    {
    first=false;
    remove(tryt);
    remove(ans);
    xloc=180;
    yloc=490;
    x=0;
    repaint();
    }
    }
    }
    //System.out.println("hey");
    //rate=Integer.parseInt(entry.getText());
    //first=true;
    //repaint();
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,860
    Rep Power
    8

    Default Re: paint error, with a simple boolean

    Some advice:

    When posting code, use the code tags to preserve formatting. And make sure you paste your actual code in SSCCE form (what's a Fram?) without any extra code.

    Don't do any loading inside your painting code.

    Don't call Thread.sleep inside your painting code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: paint error, with a simple boolean

    sorry for not explaining fram is my window adapter, what do you mean by add tags, and why not do those two things

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,860
    Rep Power
    8

    Default Re: paint error, with a simple boolean

    I mean surround your code with code tags to preserve the formatting.

    And you shouldn't do those things because it ties up the EDT, which will result in terrible things, including the symptom you described.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: paint error, with a simple boolean

    while i bing EDT can you tell me how to tag the code, i did it the first time i did a post by accident (on other thread)...

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,860
    Rep Power
    8

    Default Re: paint error, with a simple boolean

    EDT = Event Dispatching Thread. It's where things like event handlers and Swing code (including painting) gets called. So if you do something that takes a long time on the EDT (like reading files), then the EDT stops responding- which prevents your program from responding to events or handling painting, which will cause the flickering behavior you described, as well as other symptoms. One of the reasons Java has a bad rep amongst the uneducated for being slow is because novice programmers deal with the EDT wrong and cause their program to freeze up or appear laggy.

    And you should have a little button in the advanced editor that automatically wraps the selection in code tags. It does this:

    Java Code:
    System.out.println("ooh look how pretty");
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: paint error, with a simple boolean

    Java Code:
    //ok ill try wraping... anyway should i move the loading stuff to the constructor and try to actionPerformed, what would be the most efficient?
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class CFrame extends Fram implements ActionListener
    {
    Image image1,image2;
    Button b = new Button("Enter");
    Button show = new Button("Show Answer");
    Button reset = new Button("RESET");
    TextField entry = new TextField("",5);
    Timer timer = new Timer();
    int xloc=180,x=0,yloc=490,rate;
    boolean first=false,q=true;
    Font big = new Font("Serif",Font.BOLD,40);
    Label ans = new Label("Correct answer is: d(theta)/dt = 1 rad/sec");
    Label tryt = new Label("");
    Label rr = new Label("Related Rates");
    boolean att=false, cor = false;;
    CFrame()
    {
    super("CFRAME");
    setSize(1000,850);
    setLayout(null);
    add(b);
    add(show);
    add(entry);
    add(rr);
    add(reset);
    reset.addActionListener(this);
    reset.setBounds(50,130,60,30);
    b.setBounds(630,50,60,30);
    b.addActionListener(this);
    show.setBounds(630,100,80,40);
    show.addActionListener(this);
    entry.setBounds(710,50,60,25);
    rr.setBounds(25,25,250,100);
    rr.setFont(big);
    rr.setBackground(Color.BLUE);
    setVisible(true);
    setBackground(Color.BLUE);
    //System.out.print(b.getX());
    //repaint();
    //start();
    }
    public void paint(Graphics g) 
    {
    Toolkit tool = Toolkit.getDefaultToolkit();
    image2 = tool.getImage("E:\\programmin\\WhiteBook\\pics\\ax is.jpg");
    g.drawImage(image2,50,150,this);
    image2 = tool.getImage("E:\\programmin\\WhiteBook\\pics\\bo lt.jpg");
    g.drawImage(image2,110,470,this);
    image2 = tool.getImage("E:\\programmin\\WhiteBook\\pics\\wo rk.jpg");
    g.drawImage(image2,400,500,this);
    g.setFont(big);
    //g.setColor(Color.BLACK);
    //g.drawString("Related Rates",25,25);
    if(first==true)
    {
    
    // here is conditional that im talking about
    
    if(q==true)
    {
    //System.out.print(b.getX());
    //image1 = tool.getImage("E:\\programmin\\WhiteBook\\pics\\bo lt.jpg");
    //System.out.print(":gjdfgjkl");
    //g.drawImage(image1,900,985,this);
    g.fillOval(900,980,50,50);
    q=false;
    }
    g.setColor(Color.YELLOW);
    g.fillOval(xloc,yloc,25,25);
    if(x>=52)
    {
    g.setColor(Color.RED);
    g.drawLine(485,500,xloc,yloc+12);
    }
    //g.setColor(Color.BLACK);
    //g.drawLine(500,200,xloc,yloc+25);
    try
    {
    if(x<50)
    {
    xloc+=5;
    //yloc-=x^2;
    ++x;
    Thread.sleep(10);
    }
    if(x>=50&&x<77)
    {
    xloc+=10;
    yloc-=(x-49)^2;
    ++x;
    Thread.sleep(100);
    }
    if(x==77)
    {
    //g.setColor(Color.MAGENTA);
    //g.drawLine(485,500,xloc,yloc+12);
    if(att==false)
    {
    add(ans);
    ans.setBounds(330,150,180,30);
    ans.setForeground(Color.RED);
    g.setColor(Color.GREEN);
    g.drawArc(340,450,200,200,90,-38);
    ++x;
    }
    else
    {
    if(cor==true)
    {
    add(tryt);
    tryt.setBounds(330,150,180,30);
    g.setColor(Color.GREEN);
    g.drawArc(340,450,200,200,90,-38);
    ++x;
    }
    else
    {
    add(tryt);
    tryt.setBounds(330,150,180,30);
    add(ans);
    ans.setBounds(330,150,180,30);
    g.setColor(Color.GREEN);
    if(Integer.parseInt(entry.getText())>1)
    {
    g.drawArc(340,450,200,200,90,-45);
    System.out.print(b.getX());
    }
    else
    {
    if(Integer.parseInt(entry.getText())<1)
    {
    g.drawArc(340,450,200,200,90,-25);
    System.out.print(b.getX());
    }
    }
    ++x;
    }
    }
    
    }
    }
    catch(InterruptedException e)
    {
    }
    if(x<78)
    repaint();
    }
    
    }
    public void start() 
    {
    timer.schedule(new TimerTask() 
    {
    public void run() 
    {
    xloc-=5;
    repaint();
    //System.out.println("hey");
    timer.cancel();
    }
    },500,500);
    
    }
    public void actionPerformed(ActionEvent e)
    {
    String buttonClicked=e.getActionCommand();
    if(buttonClicked.equals("Enter"))
    {
    att=true;
    if(1==Integer.parseInt(entry.getText()))
    {
    tryt.setText("Correct");
    first=true;
    cor=true;
    repaint();
    }
    else
    {
    tryt.setText("InCorrect");
    first=true;
    repaint();
    }
    }
    else
    {
    if(buttonClicked.equals("Show Answer"))
    {
    first=true;
    repaint();
    }
    else
    {
    if(buttonClicked.equals("RESET"))
    {
    first=false;
    remove(tryt);
    remove(ans);
    xloc=180;
    yloc=490;
    x=0;
    repaint();
    }
    }
    }
    //System.out.println("hey");
    //rate=Integer.parseInt(entry.getText());
    //first=true;
    //repaint();
    }
    }

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,860
    Rep Power
    8

    Default Re: paint error, with a simple boolean

    Do you really not use indentation in your code? And yes, you still have to move the loading out of your paint method. And you still have to get rid of those calls to Thread.sleep(). These are what's causing your problems, so we can't really proceed until you fix them.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: paint error, with a simple boolean

    i actually do but at the time that i tried wrapping i only coppied and pasted the first post, if u wnat me to re-re-post the code i will, also what's ur take on when to use threads or timer?

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,860
    Rep Power
    8

    Default Re: paint error, with a simple boolean

    Quote Originally Posted by PRW56 View Post
    i actually do but at the time that i tried wrapping i only coppied and pasted the first post, if u wnat me to re-re-post the code i will, also what's ur take on when to use threads or timer?
    You can edit any of your previous posts to format the code correctly, no need to re-re-post the code.

    And whether you use a Timer or a Thread is a separate question. Either way, you should should NOT call Thread.sleep() while on the EDT.
    Last edited by KevinWorkman; 03-30-2012 at 08:03 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: paint error, with a simple boolean

    yeah i figured out i could do that immediately after... but yeah i get that i shouldnt use that method in this case, im just curious what you think is the optimum time to use timer and/or threads?

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,860
    Rep Power
    8

    Default Re: paint error, with a simple boolean

    Quote Originally Posted by PRW56 View Post
    yeah i figured out i could do that immediately after... but yeah i get that i shouldnt use that method in this case, im just curious what you think is the optimum time to use timer and/or threads?
    My general rule of thumb- use a Swing Timer until you have a reason not to.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: paint error, with a simple boolean

    What would be a reason not to use a swing timer?

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

    Default Re: paint error, with a simple boolean

    Quote Originally Posted by PRW56 View Post
    What would be a reason not to use a swing timer?
    One I can think of is when you require a timing accuracy greater than javax.swing.Timer can provide. Another could be when you want a fixed delay from the end of one action to the beginning of the next. There could be others.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: paint error, with a simple boolean

    That makes sense, but it is also why i despise using swing. The only things i have been taught were from a book around ten years old, and in said book it only used the word swing two pages, 1 being the index. Swing's methods seem to simplify what you would normally have to do when using the older classes from java.util, but when I use them I run into... unusual problems.

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

    Default Re: paint error, with a simple boolean

    Quote Originally Posted by PRW56 View Post
    That makes sense, but it is also why i despise using swing. The only things i have been taught were from a book around ten years old, and in said book it only used the word swing two pages, 1 being the index. Swing's methods seem to simplify what you would normally have to do when using the older classes from java.util, but when I use them I run into... unusual problems.
    Ah. And do you also drive a stick-shift that you have to hand-crank to start the engine?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: paint error, with a simple boolean

    No i take a horse-drawn buggy, only way to get to town after all, I can't afford the steam powered computers they have.

Similar Threads

  1. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  2. my simple boolean code keeps saying true
    By shazakala in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 10:30 AM
  3. an error in paint method
    By hopey in forum Java 2D
    Replies: 7
    Last Post: 04-24-2009, 10:12 PM
  4. Simple Paint program question
    By StressaJune in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 08:46 PM
  5. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 05:01 AM

Tags for this Thread

Posting Permissions

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