1

So I have a code that has a text field and when press a button and enter some text, that text will appear where ever it is clicked on the panel. How do I make it so that every time I click somewhere on the panel, the old string on the panel doesn't move to the new position and a new string is created and added on to the existing text?

Eric Z
  • 21
  • 1
  • 1
    Please add more details to the question and add some text so that people can help you. – Mohit Jain Mar 11 '16 at 11:36
  • Consider adding a JLabel rather than painting text.. That allows you to change the text at any time whereas painting means you'd need to keep track of its old position and the old text. – Neil Mar 11 '16 at 11:38
  • My idea is, create two **collections** for storing x and y coordinates and every time in the **MouseClicked()** method, use **getX()** and **getY()** to get the coordinates of the point where the user clicks and add the object of those integers (boxed) to the respective collections. In the **paint()** method, display the string at every point of the canvas whose coordinates are gotten from the collections. So every time, even if the paintComponent resets due to the `repaint();` statement in the **MouseClicked()** method, it displays the text from the first coordinate clicked at to the last. –  Mar 11 '16 at 12:39
  • Consider using `JTextPane/JextField [setEditable(false)]` which is put into `JScrollPane`. – krzydyn Mar 11 '16 at 12:42

1 Answers1

0

Here's the code I wrote. It works perfectly.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class APP extends Applet implements MouseListener{

    TextField text;
    public ArrayList<Integer> X=new ArrayList<Integer>();
    public ArrayList<Integer> Y=new ArrayList<Integer>();

    @Override
    public void init(){
        text=new TextField(10);
        add(text);
        addMouseListener(this);
        text.setText("");
    }

    @Override
    public void paint(Graphics g){
        String s=text.getText();
        for(int i=0;i<X.size();i++)
        g.drawString(s,(X.get(i)).intValue(), (Y.get(i)).intValue());
    }

    @Override
    public void MouseClicked(MouseEvent m){
        X.add(new Integer(m.getX()));
        Y.add(new Integer(m.getY()));
        repaint();
    }

    /*Compulsory overriding of abstract methods of MouseListener interface*/

    @Override
    public void mouseEntered(MouseEvent m){}

    @Override
    public void mouseExited(MouseEvent m){}

    @Override
    public void mousePressed(MouseEvent m){}

    @Override
    public void mouseReleased(MouseEvent m){}
}

How it works: Enter a text in text box and press enter. Then click where you want the text to be displayed and it will, without erasing the previous drawn strings. (For the logic, refer to my comment on your question.)

EDIT

Note: If you want to erase all the strings from the canvas and start with the same or a different text from a fresh screen, add the following snippet after the first line of the paint() method:

if(s.equals("")){
    X=new ArrayList<Integer>();
    Y=new ArrayList<Integer>();
}

Just empty the text box and press enter to reset the canvas. :-)