0

I am aware of getX() function in MouseEvent class, but I am not able to use it detect left or right motion. i was making a simple Pong game in which ball can be directed by mouse's motion I am having trouble in making use of the mouseMoved function to determine the motion of mouse,whether it is moved right or left

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PongGame extends JComponent implements ActionListener,MouseMotionListener
{

    int ballX = 400;//ball's x location
    int ballY = 150;//ball's y location
    int paddleX = 0;//paddle loaction
    int ballXS = 10;//ball x speed
    int ballYS = 10;//ball y speed

    public static void main(String args[])
    {
        JFrame window = new JFrame("Game");
        PongGame game = new PongGame();
        window.add(game);
        window.pack();
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setVisible(true);

        Timer t = new Timer(10, game);
        t.start();

        window.addMouseMotionListener(game);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(800, 600);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 800, 600);

        g.setColor(Color.GREEN);
        g.fillRect(paddleX, 580, 150, 15);

        g.setColor(Color.YELLOW);
        g.fillOval(ballX, ballY, 10, 10);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        ballX += ballXS;
        ballY += ballYS;
        //simple direction of ball to bounce around the screen
        if (ballX >= paddleX && ballX <= paddleX + 150 && ballY >= 570)
        {
            ballYS = -10;
        }
        if (ballX >= 770)
        {
            ballXS = -10;
        }
        if (ballX <= 0)
        {
            ballXS = 10;
        }
        if (ballY <= 0)
        {
            ballYS = 10;
        }
        if (ballY > 600)
        {
            ballY = 0;
            ballX = 0;
        }
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {

    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        paddleX = e.getX() - 75;                
        int x1 = e.getX();        
        int x2 = e.getX();
        /*here i wanted to get a left or right motion by using x1 and x2
                so that ball can be directed accordingly*/

        repaint();
    }
}
Rishi Pandey
  • 154
  • 11
  • 4
    Store the previous value and compare? – martinez314 Feb 03 '16 at 19:00
  • that is the problem i,the previous value and the current one is same..I dont know whether there should be some time interval bw these two comparisons so that the positions are different ? – Rishi Pandey Feb 03 '16 at 19:06
  • can you post some code to get clear picture where your problem occurs? – SomeDude Feb 03 '16 at 19:12
  • You might want to add your component as mouse motion listener instead of just mouse listener? Look [here](http://stackoverflow.com/questions/10819967/java-mousemoved-event-handling-in-swing) – SomeDude Feb 03 '16 at 19:20
  • When I move my mouse right or left the paddle moves accordingly. Aren't you already detecting the movement direction? – user1803551 Feb 03 '16 at 21:05
  • no the paddle is moving according to current value of the x coordinate given by getX() function.. – Rishi Pandey Feb 04 '16 at 12:29
  • Have a look at my [simple select and drag&drop C++ example](http://stackoverflow.com/a/20924609/2521214) especially all functions with operands like `void editor::???(bool q0,bool q1,int x,int y,int dx,int dy)` if you want to learn how to do more complicated mouse stuff ... – Spektre Feb 05 '16 at 11:22

1 Answers1

2
 int x = e.getX();        
 /*here i wanted to get a left or right motion by using x1 and x2
            so that ball can be directed accordingly*/
 if(x>prevx) ballXS=10; // right
 if(x<prevx) ballXS=-10; // left
 prevx=x;

where prevx is some global variable according to your whole plan.

gpasch
  • 2,672
  • 3
  • 10
  • 12