This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "g.drawString" Watch "g.drawString" New topic


Tim Boyadjian

Joined: May 05, 2012
Posts: 2

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
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167

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

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: 10167

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
Darryl Burke

Joined: May 03, 2008
Posts: 4523

More recommended reading

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: g.drawString
Similar Threads
Problem with Graphics
Create Bar Chart
Funny Exceed refresh when launching GUI app
Graphics2D window not being repaired?
Drawing not displayed on JPanel