Results 1 to 3 of 3
Thread: Moving a Paddle with the Mouse
- 08-06-2012, 11:07 PM #1
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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-06-2012 at 11:15 PM.
- 08-06-2012, 11:19 PM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-06-2012, 11:26 PM #3
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
Moving a ball by dragging the mouse
By stevo812 in forum Java AppletsReplies: 5Last Post: 03-31-2012, 12:49 AM -
Paddle Class: Trying to code details to ball & paddle..
By CuppaCoffee in forum New To JavaReplies: 1Last Post: 01-09-2012, 12:14 AM -
Help with creating a shape and moving it with mouse and keyboard!
By konhee93 in forum New To JavaReplies: 1Last Post: 06-08-2011, 10:22 PM -
Moving java shapes with mouse and buttons
By Haraldjjones in forum New To JavaReplies: 1Last Post: 01-20-2011, 09:26 PM -
Help with mouse moving object and colisions
By macwadu in forum AWT / SwingReplies: 12Last Post: 07-01-2010, 11:18 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks