Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By aortell24

Thread: Moving a Paddle with the Mouse

  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Moving a Paddle with the Mouse

    Hello everyone I have a quick question I am trying to construct a Pong game I got the ball moving correctly but I want to move the paddle with the mouse
    motion listener mouse Dragged preferred how do I do that I can't find any examples in my book.I just want to move the paddle up and down.I had it set up
    with the arrow keys but the paddle does not move fast enuff to keep up with the ball.Thanks ahead of time.Here is my code if it helps.This is just the board class.
    Java Code:
    public class Board extends JFrame {
        private int boardHeight =585;
        private int boardWidth =790;
        private JPanel panel1 = new JPanel();
        private JMenuBar menuBar = new JMenuBar();
        private JMenu menu = new JMenu("System");
        private JLabel leftPaddle = new JLabel();
        private JLabel rightPaddle = new JLabel();
        private int leftPaddleY = 240;
        private int rightPaddleY = 250;
        private Ball ball = new Ball();
        Timer timer = new Timer(100,new TimerEventHandler());
    
    
        Board()
        {
            timer.start();
            this.setLayout(null);
            panel1.setLayout(null);
            panel1.setBounds(0, 0, 800, 600);
            this.add(panel1);
            this.setJMenuBar(menuBar);
            menuBar.add(menu);
            panel1.add(ball);
            panel1.setBackground(Color.BLACK);
            Action exitAction = new AbstractAction("Exit") {
                @Override
                public void actionPerformed(ActionEvent actionEvent)
                {
                    System.exit(0);
                }
            };
            menu.add(exitAction);
            leftPaddle.setOpaque(true);
            rightPaddle.setOpaque(true);
            leftPaddle.setBackground(Color.RED);
            rightPaddle.setBackground(Color.BLUE);
            panel1.add(rightPaddle);
            panel1.add(leftPaddle);
            leftPaddle.setBounds(770, 240, 10, 50);
            rightPaddle.setBounds(20, 240, 10, 50);
            leftPaddle.addMouseMotionListener(new paddleEventHandler());
    
        }
        public boolean ballHitsSides()
        {
            if((ball.getX() >= boardWidth)|| (ball.getX() <= 0))
            {
                return true;
            }
            else
                return false;
        }
        public boolean ballHitsTopOrBottom()
        {
            if((ball.getY() >= boardHeight ) || (ball.getY() <= 0))
            {
                return true;
            }    
            else
                return false;
        }
        class TimerEventHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
    
                ball.setBounds(ball.getXPoint()+ ball.getXSpeed(), ball.getYPoint() + ball.getYSpeed(),15,15);
                ball.update();
                if(ballHitsSides())
                {
                    ball.setXSpeed(-ball.getXSpeed());
    
                }
                if(ballHitsTopOrBottom())
                {
                    ball.setYSpeed(-ball.getYSpeed());
                }
                if(ballHitsPaddle1())
                {
                    ball.setYSpeed(-ball.getYSpeed());
                }
                if(ballHitsPaddle2())
                {
                    ball.setYSpeed(-ball.getYSpeed());
                }
                System.out.print("\t" +ball.getXPoint());
                System.out.print("\t" + ball.getYPoint());
                System.out.print("\t" +ball.getXSpeed());
                System.out.print("\t" + ball.getYSpeed());
                System.out.println(ballHitsTopOrBottom());
                System.out.println();
                repaint();
            }
    
        }
        class paddleEventHandler implements MouseMotionListener
        {
            @Override
            public void mouseDragged(MouseEvent mouseEvent)
            {
                leftPaddleY = mouseEvent.getY();
            }
            @Override
            public void mouseMoved(MouseEvent mouseEvent)
            {
    
            }
        }
     }
    Last edited by aortell24; 08-07-2012 at 12:15 AM.

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Moving a Paddle with the Mouse

    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Moving a Paddle with the Mouse

    Thanks alot That is exactly what I needed.
    awinston likes this.

Similar Threads

  1. Moving a ball by dragging the mouse
    By stevo812 in forum Java Applets
    Replies: 5
    Last Post: 03-31-2012, 01:49 AM
  2. Paddle Class: Trying to code details to ball & paddle..
    By CuppaCoffee in forum New To Java
    Replies: 1
    Last Post: 01-09-2012, 01:14 AM
  3. Replies: 1
    Last Post: 06-08-2011, 11:22 PM
  4. Moving java shapes with mouse and buttons
    By Haraldjjones in forum New To Java
    Replies: 1
    Last Post: 01-20-2011, 10:26 PM
  5. Help with mouse moving object and colisions
    By macwadu in forum AWT / Swing
    Replies: 12
    Last Post: 07-02-2010, 12:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •