Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

g.drawString

 
Tim Boyadjian
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Boyadjian
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More recommended reading
http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic