简体   繁体   中英

MouseMotionListener doesn't work

I'm programming a Game called "Kulami". It is a board game for two players.The board is build with rectangular plates different by size. I can sign all plates I need, but I can't move them, the MouseMotionListener doesn't work. A rectangular plate is signed, but there is no contact to press the mouse. I can't find the mistake. Can anyone show on my code and help me? Thanks!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import Model.EFieldStateMarble;
import Model.EFieldStatePlate;


public class Plate extends JPanel {

    // Eigenschaften
    public String plate[][];

    // Konstruktor
    public Plate(int r, int c, EFieldStatePlate fieldStatePlate, EFieldStateMarble fieldStateMarble) {
        plate = new String[r][c];
        for (r = 0; r < plate.length; r++) { // waagerecht
            for (c = 0; c < plate[r].length; c++) { // senkrecht
                plate[r][c] = fieldStatePlate.toString() + fieldStateMarble.toString(); // Codierung Feld
            }

        }
    }

    // Methoden
    public void showPlate() { // Konsolenausgabe Spielfeldplatte
        for (int r = 0; r < plate.length; r++) {
            for (int c = 0; c < plate[r].length; c++) {
                System.out.print(plate[r][c]);
            }
            System.out.println();
        }
    }

    // Ändert Eintrage in r-ter Reihe und c-ter Spalte
    public void setEntryAt(int r, int c, EFieldStatePlate fieldStatePlate, EFieldStateMarble fieldStateMarble) {
        plate[r][c] = fieldStatePlate.toString() + fieldStateMarble.toString();
    }

    public String[][] getPlate() {
        return plate;
    }

    // dreht das Array um 90°
    public void turnPlate() {
        int m = plate.length;
        int n = plate[0].length;
        String[][] returnPlate = new String[n][m];
        for (int r = 0; r < plate.length; r++) {
            for (int c = 0; c < plate[0].length; c++) {
                returnPlate[c][m - 1 - r] = plate[r][c];
            }
        }plate = returnPlate;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        drawPlate(g2d, 60, 60);

    }

    private void drawPlateField(Graphics2D g2d, int x, int y) {
        int posX = x;
        int posY = y;
        g2d.setColor(Color.BLACK);
        g2d.drawRect(posX, posY, 60, 60);
        g2d.setColor(new Color(242, 203, 130));
        g2d.fillRect(posX + 1, posY + 1, 59, 59);
        g2d.setColor(Color.BLACK);
        g2d.drawOval(x + 15, y + 15, 30, 30);
        g2d.setColor(new Color(242, 203, 130));
        g2d.fillOval(x + 16, y + 16, 29, 29);
    }

    private void drawPlate(Graphics g2d, int plateX, int plateY ) {

        try {
            for (int r = 0; r < plate.length; r++) {
                for (int c = 0; c < plate[0].length; c++) {
                    drawPlateField((Graphics2D) g2d, plateX + c * 60, plateY + r * 60);
                }

            }

        } catch (Exception e) {
            System.out.print(e);
            }

    }

    private class PlateDragged implements MouseListener, MouseMotionListener {

        //Plattengröße
        int platesizeX = plate.length;
        int platesizeY = plate[0].length;

        //Plattenkoordinaten
        int plateX = 60;
        int plateY = 60;

        //Position, von der die Maus die Platte zieht
        int dragFromX = 0;
        int dragFromY = 0;

        //bei "true" kann die Platte nach Mausklick gezogen werden
        boolean candrag = false;

        public void mousePressed(MouseEvent mmp) {

            new Thread() {
                @Override
                public void run() {
                    int x = mmp.getX();
                    int y = mmp.getY();

                    if (x >= plateX && x <= (plateX + platesizeY) && y >= plateY && y <= (plateY + platesizeX)) {

                        candrag = true;
                        dragFromX = x - plateX;
                        dragFromY = y - plateY;
                    }
                    else {
                        candrag = false;
                    }
                    System.out.println("Hallo");
                }
            }.start();

        }

        @Override
        public void mouseDragged(MouseEvent mmd) {

            new Thread() {
                @Override
                public void run() {
                    if (candrag) {


                        //Plattenposition wechseln
                        plateX = mmd.getX() - dragFromX;
                        plateY = mmd.getY() - dragFromY;

                        //Entferne die Platte nicht aus dem Fenster
                        plateX = Math.max(plateX, 0);
                        plateX = Math.min(plateX, getWidth() - platesizeY);

                        plateY = Math.max(plateY, 0);
                        plateY = Math.min(plateY, getHeight() - platesizeX);
                        repaint();
                    }
                }
            }.start();

        }

        public void mouseExited(MouseEvent mme) {

            candrag = false;

        }

        @Override
        public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

//          Plate plate = new Plate();
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



                Plate g2d = new Plate(2, 3, EFieldStatePlate.B6, EFieldStateMarble.EMPTY);
                g2d.setPreferredSize(new Dimension(650, 650));

                f.add(g2d);
                f.pack();
                f.setLocation(200, 800);
                f.setVisible(true);

            }


        });

    }
}

No where in your code is there a call to addMouseListener(...) or addMouseMotionListener(...) . You need to add a listener to a Swing component for the listener to work -- In other words, a listener has to listen to something . eg,

// Konstruktor
public Plate(int r, int c, EFieldStatePlate fieldStatePlate,
        EFieldStateMarble fieldStateMarble) {
    plate = new String[r][c];
    for (r = 0; r < plate.length; r++) { 
        for (c = 0; c < plate[r].length; c++) { 
            plate[r][c] = fieldStatePlate.toString() + fieldStateMarble.toString(); 
        }
    }

    // ***** added *****
    PlateDragged plateDragged = new PlateDragged();
    addMouseListener(plateDragged);
    addMouseMotionListener(plateDragged);
}

Frankly questions of this nature, so basic with regards to MouseListener use suggest that you're coding by guessing and without studying the tutorial first. For your sake, don't do this. Please check the MouseListener Tutorial .

Other issue: why are you creating a thread off of the Swing event thread and calling code from it?

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