File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes A few questions on the paint() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A few questions on the paint() method" Watch "A few questions on the paint() method" New topic
Author

A few questions on the paint() method

J Jacks
Greenhorn

Joined: Feb 13, 2012
Posts: 8
I have an assignment this week where I need to write code that will display my name in a specified font from small font progressively larger (4 to 24 pica in size). While I have the program done using labels and a loop...I was wondering if I could do the same thing using the paint method. I have messed around with the code in my book but am having no luck so far. Here is the base code I am working with:



But I want to add my loop so I don't have to print out each line and make the code more bloated than it needs to be (I want to have it print each size progressively down the window) so it ends up looking like this:



I am not sure how I would go about putting these snippets together or even if it is possible at all. Could you point me in the right direction please? Like I said...I already have the assignment done another way...but I am curious how to do it using the paint() method.

Thanks for your time in advance.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Add a custom JPanel to your frame (or set it as the content pane). Don't override its paint() method, but override the paintComponent() method instead.

Now, as for your problem, why don't you just stick the for loop in the paintComponent() method and let it draw the text a couple of times? What issue are you running into?
J Jacks
Greenhorn

Joined: Feb 13, 2012
Posts: 8
Well...when I do I only get the name printed once on the screen. I'm sorry I'm such a noob at this...but I am a loss as to how to get the next lines to print in descending order. :/




For some reason it doesn't like my "I" variable....but I am not sure how to fix it.


Thanks.

PS... Here is a list of the errors that Eclipse is throwing back:


Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
i cannot be resolved to a variable

at JDemoFont.paint(JDemoFont.java:13)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
at sun.awt.RepaintArea.paint(RepaintArea.java:241)
at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
at apple.awt.CWindow.handleEvent(CWindow.java:545)
at java.awt.Component.dispatchEventImpl(Component.java:4811)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:652)
at java.awt.EventQueue$2.run(EventQueue.java:650)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
for(int i = 4; i <= 24; i += 2) ;

what's that tiny little thing at the end of the loop?
J Jacks
Greenhorn

Joined: Feb 13, 2012
Posts: 8
yep...that little semicolon had me thrown too....but Eclipse hates my code worse without it. :/



I know it can work...as I found some similar code here: http://javaboutique.internet.com/tutorials/Java_by_Example/section3_1.html

But I'd rather run a loop if possible rather than having to code all the lines individually and have it position the lines automatically too. :S
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you really need to start over.

1) as already mentioned, do your painting in a JPanel and its paintComponent().
2) what's this doing inside a painting method?
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3) your for loop needs to be 'blocked'--> {...}
4) when you had it working with labels, did you put all the labels over each other?
if not, why drawString in the same spot all the time?
J Jacks
Greenhorn

Joined: Feb 13, 2012
Posts: 8
Thanks Michael...I'll try what you suggested. (yes...and start over! ) and block my loop...(I'm getting loopy myself and probably need to step away from the computer but the problem just won't let me).

When I had it as labels...I had it in a grid layout so it flowed quite nicely. I am wondering if I can draw the swing the same way(In a grid layout) or is there another way to lay out the strings so they flow neatly rather than having them paint at a specific coordinate?

Like I said...I am a complete noob at this (only been doing this for 8 weeks) but I love this java thing and want to learn all I can about it. Unfortunately...the book I have from school doesn't go into that much detail so I have to bother you poor guys for silly information like this.

Thanks so much for all of your help.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I am wondering if I can draw the swing the same way(In a grid layout)

gridLayout is for components, but you can simulate it:

drawString is at x, y
so. x can stay the same, you just need to increment y in each loop,
and the size of the increment you should tie into the size of the font.
i.e. as the font increases, you'll need more height
J Jacks
Greenhorn

Joined: Feb 13, 2012
Posts: 8
Ahhh...Fantastic! that is Just what I needed! I think I can muddle through the rest and get it the way I want tonight. thanks so much Michael!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A few questions on the paint() method