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