943,540 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1816
  • Java RSS
Aug 31st, 2009
0

strategy when drawing String on JPanel

Expand Post »
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?

java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008
Aug 31st, 2009
1

Re: strategy when drawing String on JPanel

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.

Java Syntax (Toggle Plain Text)
  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?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Aug 31st, 2009
0

Re: strategy when drawing String on JPanel

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
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008
Aug 31st, 2009
1

Re: strategy when drawing String on JPanel

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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Aug 31st, 2009
0

Re: strategy when drawing String on JPanel

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.
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Maths equations + help button
Next Thread in Java Forum Timeline: Continuing RMI Issues





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC