I'm making a simple Java game of pong, where there is a racquet and a ball. However, I successfully implemented a mouseWheel on the racquet to move. I want to make that to a mouseDragged event instead. I'm new to Java and these mouseEvents are really confusing.
Here is my Racquet class:
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
public class Racquet {
private static final int Y = 330;
private static final int HEIGHT = 10;
int x = 0;
int xa = 0;
static int WIDTH = 60;
private Game game;
public Racquet(Game game) {
this.game = game;
}
public Racquet(Game game, int WIDTH) {
this.WIDTH = WIDTH;
}
public void move() {
if (x + xa > 0 && x + xa < game.getWidth() - WIDTH)
x = x + xa;
}
public void paint(Graphics2D g) {
g.fillRect(x, Y, WIDTH, HEIGHT);
}
public void keyReleased(KeyEvent e) {
xa = 0;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -1;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 1;
}
public Rectangle getBounds() {
return new Rectangle(x, Y, WIDTH, HEIGHT);
}
public int getTopY() {
return Y;
}
public void mouseDragged(MouseEvent m) {
xa = 0;
}
}
And here is my "GAME" class where I also tried to implement the racquet:
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.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent m) {
racquet.mouseDragged(m);
}
@Override
public void mouseMoved(MouseEvent arg0) {
}