Results 1 to 6 of 6
  1. #1
    stevo812 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Moving a ball by dragging the mouse

    Hi I have just signed up and am sure that you guys can help me. I am currently writing a java program in eclipse based on a pool game. I have hit a block as to how to make the cue ball (b[0]) move from a stationary position using the mouseEvent() methods. Any help would be greatly appreciated as I really need to get this working. Here is the code for my main class.

    Thanks in advance for your help

    public class PoolHustler extends Applet implements Runnable, MouseListener, MouseMotionListener{

    // Image which passes in an image to be copied.
    private Image i;

    //Graphics which passes in graphics to be copied.
    private Graphics doubleBuffer;

    Ball b[] = new Ball[16];

    int mouseDrag = 0;

    //initialise method which is called the very first time the applet is called upon.
    public void init(){

    //sets the size of the window giving it a width and height.
    setSize(400, 600);

    //sets the background colour of the window.
    setBackground(Color.GREEN);

    //adds the Mouse Listener to the window on running the program the very first time.
    addMouseListener(this);

    //adds the Mouse Motion listener to the window on running the program the very first time.
    addMouseMotionListener(this);

    }

    //start method is called every time the applet is started.
    public void start(){

    for(int i = 0; i < b.length; i ++){

    //initialises the first instance of the Ball class using the defined methods.
    b[i] = new Ball();

    }

    b[0] = new Ball();


    }

    //run method which contains the code needed to run the applet.
    public void run() {

    //infinite loop which will always be true and loops through and repaints the canvas 60 times a second.
    while(true){

    /**calls the update method in the Ball class which contains the code for moving
    * the ball and rebounding off the cushions.
    */
    b[0].update(this);

    /**called 60 times a second initially goes to the update method then clears the
    * screen and calls the paint method.
    */
    repaint();

    /**A try and catch statement which asks the thread to try and sleep. If it fails
    * to do so then an exception occurs and the catch statement implemented.
    */
    try{

    /**Thread.sleep() method which refers to the Thread class and
    * defines the frame rate of the game by dividing 1000 by 60.
    * The Thread.sleep() is used to allow the program to reset so
    * that the program doesn't crash.
    */
    Thread.sleep(20);

    }

    //catches the exception if the program does not sleep for 20ms.
    catch(InterruptedException e){

    //If an error occurs the error is printed to the stack trace in the debugger.
    e.printStackTrace();

    }

    }



    }

    /**Update method which copies the image currently on the screen and replaces the clear
    * screen with the copied image so when the paint method is called it paints on top of
    * the copied image.
    */
    public void update(Graphics g) {

    /**an if statement which when a blank screen occurs replaces the blank screen with the
    * image on the screen originally.
    */
    if(i == null){

    //creates an image which sets the width and height the same as the applet.
    i = createImage(this.getSize().width, this.getSize().height);

    //sets the graphics to the image which has been created.
    doubleBuffer = i.getGraphics();

    }

    //sets colour to the current background colour.
    doubleBuffer.setColor(getBackground());

    /**fills the rectangle from the top left x and y coordinates and sets its
    * its width and height to that of the set size of the window.
    */
    doubleBuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);

    //sets the colour to the foreground colour when used.
    doubleBuffer.setColor(getForeground());

    //calls the paint method and passes in the doubleBuffer image.
    paint(doubleBuffer);

    /**g refers to the Graphics parameter passed into the Update method draws
    * the i which was set up and places it at the starting point 0 for both
    * x and y coordinates. Image observer uses 'this'.
    */
    g.drawImage(i, 0, 0, this);

    }

    /**paint method with object Graphics and names the variable g paints all
    * the graphics to the applet.
    */
    public void paint(Graphics g) {

    //calls the display method in the Ball class and sets the colour of ball one to white.
    b[0].display(g, Color.WHITE);

    public void mouseDragged(MouseEvent e) {

    if(mouseDrag > 0){



    }

    }




    public void mousePressed(MouseEvent e) {

    mouseDrag = 1;

    }

    public void mouseReleased(MouseEvent e) {



    }



    }
    Last edited by stevo812; 03-30-2012 at 08:39 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: Moving a ball by dragging the mouse

    That is WAAAY too much code to ask other people to read through, for free, in their spare time, on a weekend. Post an SSCCE instead. Plus, please use the code tags when posting code to preserve formatting.

    Which part of this is giving you trouble? Without wading through that code, it sounds like a job for a Swing Timer to me.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    stevo812 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Moving a ball by dragging the mouse

    Sorry about that. I only put it in because in a lot of the forums I've looked at a lot of people ask to see the classes code. I'm having trouble with making the cue ball (b[0]) move by dragging the mouse. When I hover over the cue ball I want the to press the mouse then as I drag back a line to appear. When the mouse is released I want the ball to move down the table. It's in the main class which I have updated in my first post. Thanks for any help you can give.
    Last edited by stevo812; 03-30-2012 at 08:25 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: Moving a ball by dragging the mouse

    Like I said, code you post should be between code tags and should be in SSCCE form.

    But the general idea is this: keep track of the location you want to draw the ball. Update the location in your MouseListener and call repaint. In paintComponent, simply draw the ball at that location. That's really all I can say until I see an SSCCE. Otherwise I'm just guessing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Moving a ball by dragging the mouse

    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Moving a ball by dragging the mouse

    Quote Originally Posted by KevinWorkman View Post
    That is WAAAY too much code
    Yup. And what's with all the inane comments that just add clutter?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Ball not moving
    By Eleeist in forum New To Java
    Replies: 7
    Last Post: 01-04-2012, 11:27 PM
  2. Moving a ball with Arrow Keys
    By kekcklemen in forum Java Applets
    Replies: 5
    Last Post: 02-25-2011, 10:15 PM
  3. Draw rectangle by dragging mouse
    By Tyler in forum SWT / JFace
    Replies: 11
    Last Post: 08-09-2010, 07:50 PM
  4. drawing Line by dragging mouse !!!
    By h9h in forum Java 2D
    Replies: 14
    Last Post: 10-23-2009, 05:10 AM
  5. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 12:17 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
  •