# noch einmal MouseMotionListener



## fischebr (7. Jan 2017)

Hallo,
ich bin immer noch dabei, meine Objekte im Spielfeld zu bewegen. Ich hatte da schon am 03.01. einen Post. Ich habe meinen MouseMotionListener auf Anraten jetzt direkt in die Klasse des Objektes kopiert, welches gezogen werden soll. Es bewegt sich aber nach wie vor nicht, ich habe nicht mal eine Konsolenausgabe bei MousePressed. Kann mir jemand sagen, wo der Fehler liegt?

```
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() {
           
           @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);

           }


       });

   }
```
Vielen Dank!


----------



## Sogomn (8. Jan 2017)

Na Du musst den Listener natürlich noch auf dem Panel registrieren.

```
myPanel.addMouseMotionListener(myListener);
```


----------

