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?

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++;
		}
	}
}

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.

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?

commented: Thanks lad. You never fail +2

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

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.

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:

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!

commented: Thank you lad. Great help. +2

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.