strategy when drawing String on JPanel

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 87
Reputation: freelancelote is an unknown quantity at this point 
Solved Threads: 2
freelancelote's Avatar
freelancelote freelancelote is offline Offline
Junior Poster in Training

strategy when drawing String on JPanel

 
0
  #1
Aug 31st, 2009
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?

  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import java.awt.*;
  4. import java.io.*;
  5.  
  6. class PrintStringOnPanel extends JPanel {
  7. String string;
  8.  
  9. PrintStringOnPanel(String string) {
  10. super();
  11. this.string = string;
  12. }
  13.  
  14.  
  15.  
  16. void display(String string) {
  17. this.string = string;
  18. repaint();
  19. }
  20. public void paintComponent(Graphics comp) {
  21. Graphics2D g2d = (Graphics2D) comp;
  22. g2d.drawString(string, 10,30);
  23. }
  24. }
  25.  
  26. public class DrawString extends JFrame {
  27. String phrase = "";
  28. PrintStringOnPanel pan = null;
  29.  
  30.  
  31. DrawString() {
  32. super("draw string");
  33. setSize(150,100);
  34. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.  
  36. pan = new PrintStringOnPanel(phrase);
  37. add(pan);
  38. setVisible(true);
  39. }
  40.  
  41. void display(String string) {
  42. phrase = string;
  43. pan.display(phrase);
  44. add(pan);
  45. repaint();
  46. }
  47.  
  48. public static void main(String[] args) throws InterruptedException {
  49. DrawString d = new DrawString();
  50.  
  51. String is = "";
  52. int i = 0;
  53. while (i < 100000) {
  54. is = String.valueOf(i);
  55. d.display(is);
  56. // Thread.sleep(500);
  57. i++;
  58. }
  59. }
  60. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: strategy when drawing String on JPanel

 
1
  #2
Aug 31st, 2009
I can't imagine that these two lines in red are doing anything good:

	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.

  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 87
Reputation: freelancelote is an unknown quantity at this point 
Solved Threads: 2
freelancelote's Avatar
freelancelote freelancelote is offline Offline
Junior Poster in Training

Re: strategy when drawing String on JPanel

 
0
  #3
Aug 31st, 2009
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
  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  2. at javax.swing.JComponent._paintImmediately(Unknown Source)
  3. at javax.swing.JComponent.paintImmediately(Unknown Source)
  4. at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
  5. at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
  6. at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
  7. at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
  8. at java.awt.event.InvocationEvent.dispatch(Unknown Source)
  9. at java.awt.EventQueue.dispatchEvent(Unknown Source)
  10. at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  11. at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  12. at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  13. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  14. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  15. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 207
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: strategy when drawing String on JPanel

 
1
  #4
Aug 31st, 2009
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:
  1. super.paintComponent(comp);
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!
Last edited by llemes4011; Aug 31st, 2009 at 11:48 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 87
Reputation: freelancelote is an unknown quantity at this point 
Solved Threads: 2
freelancelote's Avatar
freelancelote freelancelote is offline Offline
Junior Poster in Training

Re: strategy when drawing String on JPanel

 
0
  #5
Aug 31st, 2009
OK, thanks to you both, that helped a lot.
I've been doing some more reading and inserted a new line of code after the while loop int count = d.getComponentCount(); . It actually returns only one component.

Thanks again.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC