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

Recommended Answers

All 5 Replies

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();
    }
...

thank you guys for replying i will try these steps

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.