Results 1 to 17 of 17
- 03-29-2012, 07:22 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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();
}
}
- 03-29-2012, 07:25 PM #2
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!
- 03-29-2012, 07:33 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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
- 03-29-2012, 07:38 PM #4
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!
- 03-29-2012, 07:42 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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)...
- 03-29-2012, 07:57 PM #6
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!
- 03-30-2012, 12:42 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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(); } }
- 03-30-2012, 02:45 PM #8
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!
- 03-30-2012, 06:01 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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?
- 03-30-2012, 07:57 PM #10
Re: paint error, with a simple boolean
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!
- 03-31-2012, 06:44 PM #11
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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?
- 03-31-2012, 09:33 PM #12
Re: paint error, with a simple boolean
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-05-2012, 07:35 AM #13
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
Re: paint error, with a simple boolean
What would be a reason not to use a swing timer?
- 04-05-2012, 07:53 AM #14
Re: paint error, with a simple boolean
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-05-2012, 08:14 AM #15
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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.
- 04-05-2012, 08:45 AM #16
- 04-05-2012, 09:08 AM #17
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05-05-2011, 09:04 AM -
my simple boolean code keeps saying true
By shazakala in forum New To JavaReplies: 8Last Post: 03-27-2011, 10:30 AM -
an error in paint method
By hopey in forum Java 2DReplies: 7Last Post: 04-24-2009, 10:12 PM -
Simple Paint program question
By StressaJune in forum New To JavaReplies: 1Last Post: 03-30-2009, 08:46 PM -
Simple Boolean
By jigglywiggly in forum New To JavaReplies: 3Last Post: 01-01-2009, 05:01 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks