-1

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) {


            }
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Conceptually, the idea is not that hard. You have a "press" point which marks the start location and a "drag" location, which marks the current location, the difficulty is, you need to define a "offset", which defines the difference between the point the mouse was "pressed" and the origin of what ever you are dragging. So when the "drag" even occurs, you can apply the "offset" to the "current" location, which becomes the origin for what ever you are dragging - clear as mud :P – MadProgrammer May 17 '18 at 00:14
  • Lucky for you, this kind of thing has been asked before, [for example](https://stackoverflow.com/questions/33033384/moving-an-ellipse2d-on-mouse-drag/33033779#33033779), [example](https://stackoverflow.com/questions/21616900/imageicon-click-and-drag-around-the-window/21617375#21617375), [example](https://stackoverflow.com/questions/20011091/move-over-a-jpanel-with-mouse-dragged/20013077#20013077) ... oh well, you get the idea – MadProgrammer May 17 '18 at 00:14
  • Thanks for the help but I'm still confused, I'm new to java so all these words are new to me, can you describe me some steps? – helppmedoe May 17 '18 at 00:21

1 Answers1

2

The basic idea is...

  • When a mousePressed event occurs, you need to determine if the event occurred in your Racquet. Rectangle has a neat contains method which can do that (this is known as "hit detection")
  • If the Racquet was "pressed", you then need to calculate the difference between the mouse event and the origin of the Racquet (ie, it's current x/y point)
  • When a mouseDragged event occurs, you need to determine if the Racquet is actually been dragged, if it is, you need to calculate the difference between the mouse point and the original click "offset", this provides you with the new origin point for the Racquet
  • When a mouseReleased event occurs, you need to mark the Racquet as not been dragged, so future drag events to "accidentally" cause the Racquet to be moved

Maybe something like...

MouseAdapter mouseHandler = new MouseAdapter() {

    private Point offset;
    private Racquet racquet;

    @Override
    public void mousePressed(MouseEvent e) {
        // Did we click on a racquet?
        if (racquet.getBounds().contains(e.getPoint())) {
            // The difference between the mouse point and the original of the racquet
            offset = new Point(e.getPoint().x - racquet.getBounds().x, e.getPoint().y - racquet.getBounds().y);
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // Are we dragging a racquet
        if (offset != null) {
            int newX = e.getPoint().x - offset.x;
            int newY = e.getPoint().y - offset.y;

            // This should be applied as a setting, but here we are
            racquet.x = newX;
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // Release racquet
        offset = null;
    }

};

// The mouseHandler needs to be added to the primary rendering surface
// component.    
addMouseListener(mouseHandler);
addMouseMotionListener(mouseHander);

Side note: I'm work with an incomplete picture and have based the solution on other examples of similar issues. If you need more information, you will need to provide a Minimal, Complete, and Verifiable example in order to provide more context to the problem.

I would however, recommend having a read of:

to better understand how the mouse listener API works

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366