简体   繁体   中英

How would I separate the MotionListener to a different class?

I'm trying to teach myself java and I've gotten stumped. I've either been practicing too much today or I'm just having a dumb moment.

I'm playing around with paint and MouseMotionListener to be able to drag graphics across the screen and I wanted to make the MouseMotionListener as a seperate class.

On to the question:

How would I separate the listener in my code into a separate class? When I tried to put it in another class I just ended up making a circular reference.

Code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;


@SuppressWarnings("serial")
class Class1 extends JFrame implements MouseMotionListener{

Point p,p2;


public Class1(){
    p = new Point(0,0);
    this.setSize(500,500);
    //this.setUndecorated(true);
    //this.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.01f));
    this.addMouseMotionListener(this);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[]args){
    new Class1();
}

public void paint(Graphics g){
    g.setColor(Color.gray);
    g.fillRect(0, 0, 500, 500);
}

public void paintSquare(Graphics g){
    g.fillRect(p.x, p.y, 50, 50);
}

public void paintCover(Graphics g){
    g.setColor(Color.gray);
    g.fillRect(p2.x, p2.y, 50, 50);
}

@Override
public void mouseDragged(MouseEvent e) {
    p2=p;
    p=e.getPoint();
    p.translate(-25, -25);
    paintCover(this.getGraphics());
    paintSquare(this.getGraphics());
}

@Override
public void mouseMoved(MouseEvent e) {
    //do nothing
}

}

Start be separating your logic into areas of responsibility...

You need:

  • Something to render the output
  • Something to maintain the current state of the output
  • Something to change the current state of the output

Let's start with the model...

The model maintains information about the current state of the output, it provides a means by which the state could be changed and notifications to tell interested parities that the state has changed could be generated.

The view is responsible for rendering the state of the model and listening for changes to the state of the model so that it can update itself when the model changes...

The MouseMotionListener (in this case) would be used to modify the state of the model...

The view and the MouseMotionListener would both have a reference to the model, this way, the model would act as the bridge between the various components. The MouseMotionListener would be used to update the model and the model would trigger notifications to the view, which would paint the current state of the model.

Take a look at model-view-controller for more details.

Also, custom painting in Swing is generally done by overriding the paintComponent method of classes that extend from JComponent . You should avoid overriding paint of top level containers like JFrame or using getGraphics . Take a look at Performing Custom Painting for more details

MouseMotionListener is an interface see here . The only way you can effectively "remove" it from your Class1 implementation is to move it to an abstract class, see here or create some object that handles the logic required.

You can have an abstract class implement MouseMotionListener and override the required methods as defined in MouseMotionListener and handle the logic in said abstract class. Then you would extend your Class1 with said abstract class.

Please note that you do not have to make it an abstract class. You could also have some sort of MouseMotionHandler class that could do the same and you would create it and add it to the JFrame like so:

public class MouseMotionHandler implements MouseMotionListener {
    @Override
    public void mouseDragged(MouseEvent e) {
       // do something here
    }

    @Override
    public void mouseMoved(MouseEvent e) {
       //do something here
    }
}

And in your JFrame

MouseMotionHandler mmh = new MouseMotionHandler();
this.addMouseMotionListener(mmh);

Another thing you could take a look at is MouseAdapter . http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html

Here are some references:

http://docs.oracle.com/javase/tutorial/java/concepts/interface.html http://www.javaworld.com/article/2077421/learn-java/abstract-classes-vs-interfaces.html http://www.tutorialspoint.com/java/java_object_classes.htm http://docs.oracle.com/javase/tutorial/java/javaOO/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM