aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes My GUI's catch block won't compile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "My GUI Watch "My GUI New topic
Author

My GUI's catch block won't compile

EJ Amber
Greenhorn

Joined: Aug 12, 2005
Posts: 8
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 13, 2005
Posts: 39773
    
  28
Please always tell us which bookand use the CODE button, or your posting becomes illegible.
EJ Amber
Greenhorn

Joined: Aug 12, 2005
Posts: 8
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

Joined: Jul 26, 2007
Posts: 10519
    
    9

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
EJ Amber
Greenhorn

Joined: Aug 12, 2005
Posts: 8
I looked for that but I didn't see an edit button.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 13, 2005
Posts: 39773
    
  28
Thread now more suitable for the Swing forum. Moving.
EJ Amber
Greenhorn

Joined: Aug 12, 2005
Posts: 8
So, if I can't use Thread.sleep(), how do I keep the animation from moving too fast?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
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

Joined: Jun 09, 2003
Posts: 4632
use a Swing.Timer, it'll do all the background threading for you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My GUI's catch block won't compile