aspose file tools*
The moose likes Applets and the fly likes g.drawString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "g.drawString" Watch "g.drawString" New topic
Author

g.drawString

Tim Boyadjian
Greenhorn

Joined: May 05, 2012
Posts: 2
Hello,

I'm writing an app to do draw some simple 2D graphics and text in a window (the window launches from a button in the app), but I'm having trouble with g.drawString(). It works perfectly the first time I launch the window from the app, however, it doesn't come up properly if I close my 2D my graphics window and reopen it again. Specifically, the window doesn't fully open, and if I minimize and reopen it appears all black. This doesn't happen if comment out the g.drawString command.


So when I remove the "test" string it works fine; I can close and reopen the window I am graphing 2D graphics in as many times as I like. However, as soon as I put the string in the window doesn't open properly. Any thoughts?
Right now I am running this app from Eclipse EE (ver 1.4.2.2012-2012)
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

Welcome to the Ranch.

In future, while posting code, please UseCodeTags. I have added them for you this time. As you can see the code tags, make the code much more easier to read and understand.

Based on the code you have provided, looks like you are using AWT instead of Swing. Any particular reason for it?
It is quite easy to switch to Swing. Almost every AWT component has a Swing replacement. Typically Swing components are named Jxxxx
e.g. Button (AWT) -> JButton(Swing)

Regarding custom painting, one important point to note is, instead of invoking the paint, you need to invoke paintComponent()
Also make sure that you call super.paintComponent as the first line from inside your paintComponent


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

Joined: May 05, 2012
Posts: 2
Thanks Maneesh.
I tried your suggestion, but unfortunately I couldn't get any image to display. I looked over a bunch of examples and I can't seem to find what I'm doing wrong; why it won't display. Here is my code, and the portion that I can't get to work is the paintComponent method, lines 36-42. Thanks for any help you can provide; I am a rookie at coding.



Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

Subclass JPanel
Move the paintComponent you currently have to that subclassed class.
Add an instance of this panel to the frame
See what happens

Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

More recommended reading
http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: g.drawString