Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Getting my paddle to move left and right in a straight line-STUCK

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting my paddle to move left and right in a straight line-STUCK

    I want to be able to add a new feature that lets me control the paddle to move left and right in a straight line.

    I already included the event library for mouse listeners but I dont know what to fill in the MouseClicked and MouseDragged
    methods to create the motion of the paddle.



    Any hints or tips will be greatly appreciated.

    Here's the code:

    import acm.graphics.*;
    import acm.program.*;

    import java.awt.Color;
    import java.awt.event.*;

    public class paddle extends GraphicsProgram{

    /** Dimensions of the paddle */
    private static final int PADDLE_WIDTH = 60;
    private static final int PADDLE_HEIGHT = 10;
    /** Dimensions of game board */
    public static final int APPLICATION_WIDTH = 400;
    public static final int APPLICATION_HEIGHT = 600;

    /** Dimensions of game board */
    private static final int WIDTH = APPLICATION_WIDTH;
    private static final int HEIGHT = APPLICATION_HEIGHT;


    public void run(){
    paddleSetup();
    addMouseListeners();

    }

    private void paddleSetup(){
    GRect paddle = new GRect(200,450 ,PADDLE_WIDTH,PADDLE_HEIGHT);
    paddle.setFillColor(Color.MAGENTA);
    paddle.setFilled(true);
    add(paddle);
    }

    // mouse listeners to drag the mouse ONLY left and right
    public void MouseClicked(MouseEvent e){


    }

    public void MouseDragged(MouseEvent e){

    }

    // instance variable for the paddle
    private GRect paddle;
    }
    Last edited by warnexus; August 20th, 2011 at 08:20 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,790
    Thanks
    54
    Thanked 2,342 Times in 2,313 Posts

    Default Re: Getting my paddle to move left and right in a straight line-STUCK

    The usual way for animation is to have the listener change the x,y location of the thing to be moved, and then call repaint(). The repaint method will call the paint method which should use the x,y location to draw the item to be displayed.

    You need to override the paintComponent method in the component where the paddle will be shown and add listeners for what event that you want to control the movement of the paddle. The listener code will change the x,y for the paddle and call repaint.

    This is for the Java SE classes. I have no idea about the acm packages. You'll have to read the API doc for those classes to see how to use them.

Similar Threads

  1. Java:Evaluation of Mathematical Expression only from left to right only.
    By deepakl_2000 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 15th, 2011, 08:35 AM
  2. Left and Right rotation in a balanced binary tree problem
    By szuwi in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 22nd, 2010, 07:20 PM
  3. Straight and Straight Flush?!?!
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 10:44 PM
  4. breakout paddle algorithm
    By Brain_Child in forum Algorithms & Recursion
    Replies: 0
    Last Post: December 30th, 2009, 05:24 AM
  5. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 08:34 AM