Hey guys

How can i make instances of drawString editable? For example changing their x, y and value?

Do i use variables for example

g.drawString(str1, x1, y1);

Will the change be reflected as soon as i change the values of the variables?

Thanks very much for your time!

No, in java all parameters are passed by value, so changing only x,y values will do nothing, you have to call g.drawString again.

No, in java all parameters are passed by value, so changing only x,y values will do nothing, you have to call g.drawString again.

Wouldn't that just draw a completely new drawString though? How do i edit one i have already created?

Not if you are drawing the string in, for example paintComponent() method.
Saying that you paint the string on a JPanel.
You create a class MyPanel that extends JPanel and over writes paintComponent() method:

public void paintComponent(Graphics g){
   g.drawString(text, x, y); // draw 1 string
}

then you have a method that changes the x, y and/or text, then calls MyPanel.repaint() which at some point calls paintComponent(). At this point the new string will be painted in the new position, and the old one not.

If you do something like this:

g.drawString(text, x, y);
x = x+3; y = y + 6;  // modify position
g.drawString(text, x, y);

then you are right, you will have to strings in 2 different positions(even if you call g.drawString twice without modifying any parameter you will have 2 strings, but you will see only 1 since they are 1 over the other);

GL

So do you suggest i use variables in the drawString parameters, and then repaint the interface the same way i painted it?

So do you suggest i use variables in the drawString parameters, and then repaint the interface the same way i painted it?

Yes

This article has been dead for over six months. Start a new discussion instead.