| | |
strategy when drawing String on JPanel
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hi,
I'm starting working with the Graphics and related and strugling to understand it.
I'm trying to do something as simple as drawing a String on a panel. The string changes on runtime. I post the code below.
In effect, every new number is drawn on top of the old one. My questions are:
What happens with the old strings? Are they still on memory?
If they are, Is there a better method?
I'm starting working with the Graphics and related and strugling to understand it.
I'm trying to do something as simple as drawing a String on a panel. The string changes on runtime. I post the code below.
In effect, every new number is drawn on top of the old one. My questions are:
What happens with the old strings? Are they still on memory?
If they are, Is there a better method?
java Syntax (Toggle Plain Text)
import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; import java.io.*; class PrintStringOnPanel extends JPanel { String string; PrintStringOnPanel(String string) { super(); this.string = string; } void display(String string) { this.string = string; repaint(); } public void paintComponent(Graphics comp) { Graphics2D g2d = (Graphics2D) comp; g2d.drawString(string, 10,30); } } public class DrawString extends JFrame { String phrase = ""; PrintStringOnPanel pan = null; DrawString() { super("draw string"); setSize(150,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pan = new PrintStringOnPanel(phrase); add(pan); setVisible(true); } void display(String string) { phrase = string; pan.display(phrase); add(pan); repaint(); } public static void main(String[] args) throws InterruptedException { DrawString d = new DrawString(); String is = ""; int i = 0; while (i < 100000) { is = String.valueOf(i); d.display(is); // Thread.sleep(500); i++; } } }
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
I can't imagine that these two lines in red are doing anything good:
You add a PrintStringOnPanel to your JFrame (the same one over and over again, I think) every time the first red line is called, so that's 100,000 of them. What problems that causes, I'm not 100% sure, but at best, they certainly don't HELP. And there's already a repaint () in your PrintStringOnPanel's
If you get a whole bunch of Strings panting on top of each other as a result, you may want to stick this line at the top of your PrintStringOnPanel's paintComponent function.
I don't think your problem is 100,000 Strings, but you may have 100,000 layers of Graphics or something, which probably hogs a lot of memory. Does this program run to completion or do you run out of memory?
void display(String string) {
phrase = string;
pan.display(phrase);
add(pan);
repaint();
}You add a PrintStringOnPanel to your JFrame (the same one over and over again, I think) every time the first red line is called, so that's 100,000 of them. What problems that causes, I'm not 100% sure, but at best, they certainly don't HELP. And there's already a repaint () in your PrintStringOnPanel's
display(String) function so you don't need one here. I'd delete the two lines in red and see what happens.If you get a whole bunch of Strings panting on top of each other as a result, you may want to stick this line at the top of your PrintStringOnPanel's paintComponent function.
Java Syntax (Toggle Plain Text)
super.paintComponent (comp);
I don't think your problem is 100,000 Strings, but you may have 100,000 layers of Graphics or something, which probably hogs a lot of memory. Does this program run to completion or do you run out of memory?
Thanks VernonDozier.
The code as I showed it runs. After you answered I just noticed there are dozens of nullPointerExceptions showing on command line as in
The loop however finishes.
You were also wright, I could do without two of the lines: If I comment out lines 44 and 45, every number goes on top of each other and in the end all you have on the window is just a black rectangle.
Following your cue, commenting out lines 18 and 44 does exactly what I was looking for: drawing the string, refreshing the previous panel and without any of the NullPointerException.
But my problem remains, do I still have this 100000 layers one on top of each other, or does the garbage collector deletes the previous ones?
Also, when is this paintComponent() method called?
I coded like this simply because I can't think of any other method. Is there a way to delete the previous panel, or do people just don't bother to handle that because it's not necessary?
I really appreciate your answers.
The code as I showed it runs. After you answered I just noticed there are dozens of nullPointerExceptions showing on command line as in
Java Syntax (Toggle Plain Text)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
The loop however finishes.
You were also wright, I could do without two of the lines: If I comment out lines 44 and 45, every number goes on top of each other and in the end all you have on the window is just a black rectangle.
Following your cue, commenting out lines 18 and 44 does exactly what I was looking for: drawing the string, refreshing the previous panel and without any of the NullPointerException.
But my problem remains, do I still have this 100000 layers one on top of each other, or does the garbage collector deletes the previous ones?
Also, when is this paintComponent() method called?
I coded like this simply because I can't think of any other method. Is there a way to delete the previous panel, or do people just don't bother to handle that because it's not necessary?
I really appreciate your answers.
•
•
Join Date: Aug 2008
Posts: 207
Reputation:
Solved Threads: 13
No, you do not have 100,000 panels on top of each other. But if the screen is still being filled with black, you might want to insert the line:
as the first line in your paintComponent() method, as VernonDozier. That removes the previously painted Graphics before painting the next String (I think that's what it does, if I'm wrong, please, somebody correct me, lol). As for the paintComponent method, it's called by the Window that holds the JPanel, in this case your JFrame. The JFrame class has a method paintComponents() inherited from the Container class. I believe this method is called from the JFrame's paint() method. Hope that helps!
Java Syntax (Toggle Plain Text)
super.paintComponent(comp);
Last edited by llemes4011; Aug 31st, 2009 at 11:48 am.
![]() |
Similar Threads
- why is this simple code using 100% CPU? (Java)
- Drawing Line In JPanel (Java)
- drawing oval in a jpanel (Java)
- Making text blink in a JPanel? (Java)
- StringTokenizer problem (Java)
- "Cannot Resolve Symbol" (Java)
Other Threads in the Java Forum
- Previous Thread: Maths equations + help button
- Next Thread: Continuing RMI Issues
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






