mouseMoved

trying to dock an object to the mouse

Page 1 of 1

5 Replies - 11767 Views - Last Post: 16 October 2008 - 09:48 AM Rate Topic: -----

#1 Qball522   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 06-October 08

mouseMoved

Posted 15 October 2008 - 05:54 PM

Hello all,
I am trying to get an object to be docked to the mouse, meaning that if your mouse moves over the program window you have control over that object.

I have been messing with it for hours now. I have had a few that sorta worked but then just couldn;t get it working 100%

I don't remember all the different code I tried to use.

But this is what I have now.

private void createPaddle() {
		paddle = new GRect(200, (HEIGHT - (PADDLE_Y_OFFSET + PADDLE_HEIGHT)), PADDLE_WIDTH, PADDLE_HEIGHT);
		paddle.setFilled(true);
		add (paddle);
}



 
public void mouseMoved(MouseEvent e) {
		paddle.move(1.0, 0.0); 
	}




When I try to move the object I can't get it. If i set move(1.0, 0.0) when I move the mouse the GRect goes to the right which it should.

I can't for the life of me figure out how to get this following the mouse without clicks.

Do I have to use some if statements?
Declare last points and use some math for it?

I am lost at the moment.

Is This A Good Question/Topic? 0
  • +

Replies To: mouseMoved

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: mouseMoved

Posted 15 October 2008 - 06:43 PM

Quote

I am trying to get an object to be docked to the mouse, meaning that if your mouse moves over the program window you have control over that object.


Which window ? If the window contains 10 labels and 10 buttons which object should be the selected one ?
Or do you mean:

I am trying to get a component to be docked to the mouse, meaning that if your mouse moves over a component you have control over that component ?

Or do you mean the whole window ?

What do you mean by "have control over that object" ? That the object get the focus ?

A MouseListener on the component you want to monitor should do the trick.
Was This Post Helpful? 0
  • +
  • -

#3 Qball522   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 06-October 08

Re: mouseMoved

Posted 15 October 2008 - 08:09 PM

Its a simple game, there are some blocks(GRects) a ball(GOval and a paddle(GRect).

If the mouse is on the program window I want the paddle to follow the mouses X cords. The paddle wont move along the Y-Axis, it is a CONSTANT.

If the mouse moves off the program window the paddle will just stop moving until you move the mouse back on the game window and allow you to move the paddle left and right again.

I can't figure out how to use the paddle.move() with getX(). As I have the code in the mouseMoved() with paddle.move(1.0, 0.0) if I move the mouse anywhere the paddle moves, but it moves at a rate of one pixel to the right for every MoveEvent I invoke.

What I need is a way to getMouseX and move the paddle to the location. But I don't know how to do that just yet.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: mouseMoved

Posted 15 October 2008 - 08:21 PM

So you have to add a MouseListener to your "windows" I guess you mean a JPanel

JPanel implements MouseMotionListener
...

panel.addMouseMotionListener(this);

	public void mouseDragged(MouseEvent arg0) {
		
	}
	public void mouseMoved(MouseEvent arg0) {
		// and do what you have to do here
		
	}


Was This Post Helpful? 0
  • +
  • -

#5 Qball522   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 06-October 08

Re: mouseMoved

Posted 16 October 2008 - 09:33 AM

Ok I am trying to follow along with the Standford CS106A course. I assume that makes a difference because what the professor and the assignments have you do is not normal Java coding. He makes mention about some of the things in place to make it easier for the class. the import acm.'s

/*
 * File: Breakout.java
 * -------------------
 * Name:
 * Section Leader:
 * 
 * This file will eventually implement the game of Breakout.
 */

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

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Breakout extends GraphicsProgram {
	
/** Width and height of application window in pixels */
	public static final int APPLICATION_WIDTH = 400;
	public static final int APPLICATION_HEIGHT = 600;

/** Dimensions of game board (usually the same) */
	private static final int WIDTH = APPLICATION_WIDTH;
	private static final int HEIGHT = APPLICATION_HEIGHT;

/** Dimensions of the paddle */
	private static final int PADDLE_WIDTH = 60;
	private static final int PADDLE_HEIGHT = 10;

/** Offset of the paddle up from the bottom */
	private static final int PADDLE_Y_OFFSET = 30;

/** Number of bricks per row */
	private static final int NBRICKS_PER_ROW = 10;

/** Number of rows of bricks */
	private static final int NBRICK_ROWS = 10;

/** Separation between bricks */
	private static final int BRICK_SEP = 4;

/** Width of a brick */
	private static final int BRICK_WIDTH =
	  (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;

/** Height of a brick */
	private static final int BRICK_HEIGHT = 8;

/** Radius of the ball in pixels */
	private static final int BALL_RADIUS = 10;

/** Offset of the top brick row from the top */
	private static final int BRICK_Y_OFFSET = 70;

/** Number of turns */
	private static final int NTURNS = 3;
	
	private static final int DELAY = 50;

/**	Random Generator */
	private RandomGenerator rgen = RandomGenerator.getInstance();

/** Ball Placement*/	
	private double vx = 3.0;		
	private double vy;
	
	/*Not used yet*/
	private boolean gameOver = false;
	
	
	/* Method: run() */
/** Runs the Breakout program. */
	public void run() {
		setUp();		
	}
	
	private void setUp() {
		createBricks();
		createPaddle();
		addMouseListeners();
	}
	
	private void createBricks() {
		for (int i = 0; i < NBRICK_ROWS; i++) { 
			for (int j = 0; j < NBRICKS_PER_ROW; j++) {
				GRect brick = new GRect(2 + ((BRICK_WIDTH + BRICK_SEP) * j), 
						(BRICK_Y_OFFSET + (BRICK_HEIGHT + BRICK_SEP) * i), BRICK_WIDTH, BRICK_HEIGHT);
				brick.setFilled(true);
				if (i <= 1) {
					brick.setColor(Color.RED);
				}
				else if (i <= 3) {
					brick.setColor(Color.ORANGE);
				}
				else if (i <= 5) {
					brick.setColor(Color.YELLOW);
				}
				else if (i <= 7) {
					brick.setColor(Color.GREEN);
				}
				else if (i <= 9) {
					brick.setColor(Color.CYAN);
				}
				add (brick);
			}
		}
	}
	private void createPaddle() {
		paddle = new GRect(200, (HEIGHT - (PADDLE_Y_OFFSET + PADDLE_HEIGHT)), PADDLE_WIDTH, PADDLE_HEIGHT);
		paddle.setFilled(true);
		add (paddle);		
	}
		
	public void mouseClicked(MouseEvent e) {
		ball();
		play();				
	}
	
	private void ball() {
		theBall = new GOval(WIDTH / 2, HEIGHT / 2, BALL_RADIUS * 2, BALL_RADIUS * 2);
		theBall.setFilled(true);
		add (theBall);
	}
	
	private void play() {
		for (int i = 0; i < 20; i++) {  /*This is for testing*/
			moveBall();
			pause(DELAY);
		}
		GLabel Test = new GLabel("Testing Over", WIDTH / 2, HEIGHT / 2);
		add (Test);		
	}
	
	private void moveBall() {
		theBall.move(1.0, 5.0);		
	}
	
	
	
	private GOval theBall;
	private GRect paddle;
	
}



With this I am not getting the ball to move in the standard sense, it will move, but you don't see it moving, it just does the .move and waits until it is done to display the ball.
Was This Post Helpful? 0
  • +
  • -

#6 Qball522   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 06-October 08

Re: mouseMoved

Posted 16 October 2008 - 09:48 AM

Wow, I just realized that I kinda jumped around a bit.

I want to follow along with the course and this seems to be on of the fundamental programs to understanding.

There is allot going on that I am unsure of.
The paddle moving.
The ball moving on its own.

I haven't even gotten into the collision of the ball yet, but if I can't even see the ball moving or move the paddle in the way that I want I shouldn't worry about that just yet.

I used a segment from a program that the Professor provided and got the paddle moving if you clicked on it and dragged it around, but that didn't work so well for trying to introduce the ball then. It was just for clicking on an object and moving it, so the ball didn't have a life of its own. You had to move the ball by clicking on it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1