Hello i have a little problem over here im trying to make a mouse listener and on mouse click the ball to come to the mouse pointer... i have maked a small "game" which is not bunch of confusing code... i have maked the ball to be controlled by the keyboard arrows but i want to use the mouse too, so when ill click somewhere in the area the ball to come to the mouse, here is the code...

package fungame;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public final class FunGame extends JPanel implements MouseListener, ActionListener, KeyListener {

    Timer t = new Timer(5, this);
    int points = 20;
    double x = 0, y = 16, muvx = 0, muvy = 0;
    static int F_WIDTH = 800;
    static int F_HEIGHT = 600;
    static int BALL_SIZE_X = 40;
    static int BALL_SIZE_Y = 40;
    static int MIN_X = 0; // left - ball
    static int MIN_Y = 16; // up - ball
    static int MAX_X = 742; // right - ball
    static int MAX_Y = 521; // down - ball

    public static void main(String[] args) {

        JFrame f = new JFrame();
        FunGame fungame = new FunGame();
        f.add(fungame);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(F_WIDTH, F_HEIGHT);
    }

    public FunGame() {
        t.start();
        addMouseListener(this);
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        graphics.fill(new Ellipse2D.Double(x, y, BALL_SIZE_X, BALL_SIZE_Y));
        g.drawString("|     Points: " + points, 170, 12);
        g.drawString("|     Time: ", 260, 12);
        g.drawLine(0, 15, 8000, 15);
    }

    public void up() {
        muvy = -2;
        muvx = 0;
    }

    public void down() {
        muvy = 2;
        muvx = 0;
    }

    public void left() {
        muvx = -2;
        muvy = 0;
    }

    public void right() {
        muvx = 2;
        muvy = 0;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
        x += muvx;
        y += muvy;
        if (x < MIN_X) {
            x = MIN_X;
        }
        if (x > MAX_X) {
            x = MAX_X;
        }
        if (y < MIN_Y) {
            y = MIN_Y;
        }
        if (y > MAX_Y) {
            y = MAX_Y;
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1) {
            int xpos = e.getX();
            int ypos = e.getY();
            System.out.println("Position: x: " + xpos + " y: " + ypos);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //code here
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //code here
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //code here
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        //code here
    }

    @Override
    public void keyTyped(KeyEvent e) {
        //code here
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //code here
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == KeyEvent.VK_UP) {
            up();
        }
        if (code == KeyEvent.VK_DOWN) {
            down();
        }
        if (code == KeyEvent.VK_RIGHT) {
            right();
        }
        if (code == KeyEvent.VK_LEFT) {
            left();
        }
    }
}

Edited 2 Years Ago by Stefan_1

so: actually, the problem is that you haven't written the code yet?
what is the concrete question you want to ask. if it is how to.

step 1: clear the screen.
step 2: get the right coordinates you clicked at
step 3: create a circle with the correct radius and that center
step 4: draw it.

short question: in your actionPerformed: why do you repaint before changing the values? it seems more logical to repaint after the new values are set.

I have the coordinates on mouse click and i have the ball already i need just the ball to move to the mouse cursor when ill put Graphics g in the mousePressed class its showing me error... so what should i do to make the ball move to the cursor

Just setting x and y to to mouse's coordinates should be enough - the next timer event will call repaint which will paint the ball at that position.

Come on now...
You have the x,y coordinates of the mouse click.
You have two variables: x and y that define the position of the ball
Set the position x and y equal to the mouse click coordinates

It's two simple assignment statements. You can do it.

I have this code but its shows me error at @Override

@Override
    public void mousePressed(MouseEvent e, Graphics g) {
        if(e.getButton() == MouseEvent.BUTTON1) {
            int xpos = e.getX();
            int ypos = e.getY();
            Graphics2D graphics = (Graphics2D) g;
            graphics.fill(new Ellipse2D.Double(xpos, ypos, BALL_SIZE_X, BALL_SIZE_Y));
            System.out.println("Position: x: " + xpos + " y: " + ypos);
        }
    }

That's because you changed the method signature (added the Graphics), so it's no longer a valid mousePressed method.
Perhaps you didn't read my previous two posts? What you are doing is nothing like what I posted.
Just set x and y equal to the mouse coordinates and leave the timer actionPerformed to call repaint() as usual. That's all it needs.

Oh i have not focussed on reading thank i have make it :)

@Override
    public void mousePressed(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1) {
            int xpos = e.getX();
            int ypos = e.getY();
            x = xpos;
            y = ypos;
            System.out.println("Position: x: " + xpos + " y: " + ypos);
        }
    }
This article has been dead for over six months. Start a new discussion instead.