i have a Jframe.in order to paint it i not using paintComponent but ordinary function containing g.drawstring("sd",0,0).

when ever i click the mouse the string should appear at that point and it should move with the cursor until another click appears.

my problem after clicking the string is not appearing immediately it appears only after the mouse moves

iam calling paint method in both mouseclicked and mousemoved

anyone help me

Edited 6 Years Ago by jbalaji87: n/a

Answer my question, if the mouse stops moving, text disappears?
if yes, you need use paintComponent method

Answer my question, if the mouse stops moving, text disappears?
if yes, you need use paintComponent method

when i click its not appearing but when mouse start to move text appear

addMouseListener(this);
addMouseMotionListener(this);
//
use repaint(); inside mouse methods

public class MyPanel extends JPanel implements MouseListener, MouseMotionListener {

    private Point p;

    public MyPanel() {
        super();
        p = new Point(0, 50);
        setBackground(Color.red);
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void paint(Graphics g) {
        //super.paint(g); //UNCOMMENT THIS
        g.drawString("Error: Can't move!", p.x, p.y);
    }

    public void mousePressed(MouseEvent e) {
        p = e.getPoint();
        repaint();
    }
...

Edited 6 Years Ago by quuba: n/a

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