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