Calling the MouseMotion Sensor Method

I ran into a problem for which no matter how long I study the class and superclass APIs, I can not understand.

Suppose I want to create a game in which the movement of the mouse controls the movement of the block, which is used to bounce the ball, which then destroys the multi-colored bricks.

How do you specifically make the block “listen” to the mouse? The following code is that I tried to achieve the desired results.

/** Breakout Program*/ public class Breakout extends GraphicsProgram implements MouseMotionListener { ... /** The Paddle Itself */ private GRect paddle = new GRect(0, HEIGHT-PADDLEBOTTOM_OFFSET, PADDLEWIDTH, PADDLEHEIGHT); ... /** Run the Breakout program. */ public void run() { paddle.setFillColor(Color.BLACK); paddle.setFilled(true); add(paddle); paddle.addMouseListener(this); ... } /** Move the horizontal middle of the paddle to the x-coordinate of the mouse position - * -but keep the paddle completely on the board. */ public void mouseMoved(MouseEvent e) { GPoint p= new GPoint(e.getPoint()); double x = p.getX(); paddle.setLocation(x, HEIGHT-PADDLEBOTTOM_OFFSET); } } 

Any clarification as to why / what I am doing wrong would be helpful, thanks.

+4
source share
4 answers

Your class is set up to be used as a mouse listener - you just need to tell some component to send you MouseEvents. To do this, you need to implement the MouseMotionListener that you have already done, so you are most there.

All that remains to be done is to call the addMouseMotionListener(this) method on your JFrame, JDialog, or any other window that you use.

In the future, it may be worth creating a separate class to serve as a listener, just to save the code. the most common solution is called an anonymous inner class, which you might want on Google. But as your deadline approaches, what you have will work fine.

+1
source

After mouseMoved() updates the location of the paddle, you usually call repaint() on the display component. Is there anything in GraphicsProgram about this?

0
source

It seems that all classes belong to your application, so I assume that you are working with AWT or Swing.

Try calling repaint () on a paddle.

0
source

Just add a comment to "Etaoin" when you get the time, and if you are serious about doing OO, do a search in the "is-a" and "has-a" relationships in OO.

If the is-a relation is true (the apple is-a), then it is legal to use implements for the class, otherwise, if its has-a relation (car) has a wheel, but a car is not "wheel"), then implements NOT suitable - you need to use composition, in other words, a member variable of the class.

In the code, can you say that the Breakout "is-a" MouseMotionListener ? The answer is no, by the way! Breakout is-a game or application, but MOUSEMotionListener is part of the implementation .

As Etaoin said, you should implement MouseMotionListener as an inner class, although I prefer private inner classes rather than anonymous classes (to keep the constructor short and precise, among other reasons).

When you “get” OO, its great and very strong, but make real efforts to make a “paradigm shift” from procedural thinking.

0
source

Source: https://habr.com/ru/post/1308191/


All Articles