• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My GUI's catch block won't compile

 
Richard Mateosian
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! This seems like a simple question, though maybe it belongs in the intermediate forum, but my code has a problem. When I try to compile, I get this message:

Java:37: paintBetterComponent(java.awt.Graphics) in CoolerSmear.MyCoolPanel cannot be applied to () youbrokeit.paintBetterComponent();
1 error


I was trying to modify one of the ready-bake-code classes from the book (the first animation) to make it re-smear when you click a button. Here's my code. I think the problem is in my catch block, ironically enough (serves me right for writing "System.out.println("Error at " + Math.random());", doesn't it?). Thanks in advance for your help!!

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it tells you the problem

public void paintBetterComponent(Graphics g){

and when called
youbrokeit.paintBetterComponent();

which needs a Graphics object

(and don't even think about using getGraphics())
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us which bookand use the CODE button, or your posting becomes illegible.
 
Richard Mateosian
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It needs a Graphics object, okay, but how do I do that? (And what's getGraphics()?)

And I didn't know that about the formatting, thanks. The book I referenced is Head First Java, second edition.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EJ Amber wrote:
And I didn't know that about the formatting, thanks..


You can use code tags for your existing post by clicking on the button
 
Richard Mateosian
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked for that but I didn't see an edit button.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to show you a major problem in your code, lets use getGraphics() so it will compile and run

change
youbrokeit.paintBetterComponent();
to
youbrokeit.paintBetterComponent(youbrokeit.getGraphics());

run the program and at any time, while the animation is on, or over, click the go button.

the app will freeze until the animation is finished - even try closing the app while the go button is still depressed.

hopefully this should prove why you should never have Thread.sleep() in the Swing thread (EDT)

and don't use getGraphics for other things - it also has major problems
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread now more suitable for the Swing forum. Moving.
 
Richard Mateosian
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if I can't use Thread.sleep(), how do I keep the animation from moving too fast?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just briefly, because I can't remember the details:

Start a new Thread (maybe with the SwingWorker class) and invoke Thread.sleep() in that Thread.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use a Swing.Timer, it'll do all the background threading for you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic