2016-06-07 20 views
0

Ich bin neu in Java und arbeite an einem Applet, das sechs Quadrate zieht. Bisher habe ich einen Code, mit dem ich jedes Quadrat unabhängig von rechts nach links bewegen kann. Ich kann jedoch nicht auf das Quadrat rechts klicken, nachdem ich das unmittelbar links davon gezogen habe. Gibt es eine Möglichkeit, das Quadrat, das ich gerade gezogen habe, freizugeben und ein zuvor verschobenes auszuwählen?Java Applet, das sechs Quadrate bewegt

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class DragSixSquares extends JApplet 
       implements MouseListener, MouseMotionListener { 


    int x1, y1; // Coords of top-left corner of the red square. 
    int x2, y2; // Coords of top-left corner of the blue square. 
    int x3, y3; // Coords of top-left corner of the cyan square. 
    int x4, y4; // Coords of top-left corner of the magenta square. 
    int x5, y5; // Coords of top-left corner of the pink square. 
    int x6, y6; // Coords of top-left corner of the orange square. 
    int x0, y0; // Coords of top-left corner of the black square. 
    /* Some variables used during dragging */ 

    boolean dragging;  // Set to true when a drag is in progress. 
    int offsetX, offsetY; // Offset of mouse-click coordinates from 
          // top-left corner of the square that was 
          // clicked.     
    JPanel drawSurface; // This is the panel on which the actual 
         // drawing is done. It is used as the 
         // content pane of the applet. It actually 
         // belongs to an anonymous class which is 
         // defined in place in the init() method. 
boolean dragRedSquare; 
boolean dragBlueSquare; 
boolean dragPinkSquare; 
boolean dragMagentaSquare; 
boolean dragCyanSquare; 
boolean dragOrangeSquare; 

    public void init() { 
     // Initialize the applet by putting the squares in a 
     // starting position and creating the drawing surface 
     // and installing it as the content pane of the applet. 

     x1 = 0; // Set up initial positions of the squares. 
     y1 = 0; 
     x2 = 50; 
     y2 = 0; 
     x3 = 100; 
     y3 = 0; 
     x4 = 150; 
     y4 = 0; 
     x5 = 200; 
     y5 = 0; 
     x6 = 250; 
     y6 = 0; 
     x0 = 300; 
     y0 = 0; 

     drawSurface = new JPanel() { 
       // This anonymous inner class defines the drawing 
       // surface for the applet. 
      public void paintComponent(Graphics g) { 
        // Draw the six squares and a black frame 
        // around the panel. 
       super.paintComponent(g); // Fill with background color. 
       g.setColor(Color.red); 
       g.fillRect(x1, y1, 30, 30); 
       g.setColor(Color.blue); 
       g.fillRect(x2, y2, 30, 30); 
       g.setColor(Color.CYAN); 
       g.fillRect(x3, y3, 30, 30); 
       g.setColor(Color.MAGENTA); 
       g.fillRect(x4, y4, 30, 30); 
       g.setColor(Color.PINK); 
       g.fillRect(x5, y5, 30, 30); 
       g.setColor(Color.ORANGE); 
       g.fillRect(x6, y6, 30, 30); 
       g.setColor(Color.black); 
       g.drawRect(0,0,getSize().width-1,getSize().height-1); 
      } 
     }; 

     drawSurface.setBackground(Color.lightGray); 
     drawSurface.addMouseListener(this); 
     drawSurface.addMouseMotionListener(this); 

     setContentPane(drawSurface); 

    } // end init(); 


    public void mousePressed(MouseEvent evt) { 
      // Respond when the user presses the mouse on the panel. 
      // Check which square the user clicked, if any, and start 
      // dragging that square. 

     if (dragging) // Exit if a drag is already in progress. 
     return; 

     int x = evt.getX(); // Location where user clicked. 
     int y = evt.getY(); 

     if(x >= x1 && x < x1+30 && y >= y1 && y < y1+30) { 
      // It's the red square. 
     dragging = true; 
     dragRedSquare = true; 
     offsetX = x - x1; // Distance from corner of square to (x,y). 
     offsetY = y - y1; 
     } 

     else if (x >= x2 && x < x2+60 && y >= y2 && y < y2+60) { 
      // It's the blue square 
     dragging = true; 
     dragBlueSquare = true; 
     offsetX = x - x2; // Distance from corner of square to (x,y). 
     offsetY = y - y2; 
     } 

     else if (x >= x3 && x < x3+30 && y >= y3 && y < y3+30) { 
      // It's the cyan square 
     dragging = true; 
     dragCyanSquare = true; 
     offsetX = x - x3; // Distance from corner of square to (x,y). 
     offsetY = y - y3; 
     } 

     else if (x >= x4 && x < x4+30 && y >= y4 && y < y4+30) { 
      // It's the magenta square 
     dragging = true; 
     dragMagentaSquare = true; 
     offsetX = x - x4; // Distance from corner of square to (x,y). 
     offsetY = y - y4; 
     } 

     else if (x >= x5 && x < x5+30 && y >= y5 && y < y5+30) { 
      // It's the pink square) 
     dragging = true; 
     dragPinkSquare = true; 
     offsetX = x - x5; // Distance from corner of square to (x,y). 
     offsetY = y - y5; 
     } 

     else { 
      // It's the orange square 
     dragging = true; 
     dragOrangeSquare = true; 
     offsetX = x - x6; // Distance from corner of square to (x,y). 
     offsetY = y - y6; 
     } 

    } 


    public void mouseReleased(MouseEvent evt) { 
      // Dragging stops when user releases the mouse button. 
     dragging = false; 
    } 


    public void mouseDragged(MouseEvent evt) { 
      // Respond when the user drags the mouse. If a square is 
      // not being dragged, then exit. Otherwise, change the position 
      // of the square that is being dragged to match the position 
      // of the mouse. Note that the corner of the square is placed 
      // in the same position with respect to the mouse that it had 
      // when the user started dragging it. 
     if (dragging == false) 
     return; 
     int x = evt.getX(); 
     int y = evt.getY(); 
     if (dragRedSquare) { // Move the red square. 
      x1 = x - offsetX; 
      y1 = y - offsetY; 
     } 
     else if (dragBlueSquare){ // Move the blue square. 
      x2 = x - offsetX; 
      y2 = y - offsetY; 
     } 
     else if (dragCyanSquare) { // Move the cyan square. 
      x3 = x - offsetX; 
      y3 = y - offsetY; 
     } 
     else if (dragMagentaSquare) { // Move the magenta square. 
      x4 = x - offsetX; 
      y4 = y - offsetY; 
     } 
     else if (dragPinkSquare) { // Move the pink square. 
      x5 = x - offsetX; 
      y5 = y - offsetY; 
     } 
     else if (dragOrangeSquare) { // Move the orange square. 
      x6 = x - offsetX; 
      y6 = y - offsetY; 
     } 
     else { // 
      x0 = x - offsetX; 
      y0 = y - offsetY; 
     } 
     drawSurface.repaint(); 
    } 


    public void mouseMoved(MouseEvent evt) { } 
    public void mouseClicked(MouseEvent evt) { } 
    public void mouseEntered(MouseEvent evt) { } 
    public void mouseExited(MouseEvent evt) { } 


} 

Vielen Dank!

Antwort

0

Sie haben vergessen, Ihre Drag-Tests zurückzusetzen, als die Maus losgelassen wurde. Also jedes Mal, wenn Sie versuchen, zukünftige ziehen zu tun, wird es immer nur auf der linken am meisten angeklickt Quadrat arbeiten.

Versuchen Sie stattdessen Ihre aktuelle Ereignis mouse:

public void mouseReleased(MouseEvent evt) { 
      // Dragging stops when user releases the mouse button. 
     dragging = false; 
     dragRedSquare = false; 
     dragBlueSquare = false; 
     dragPinkSquare = false; 
     dragMagentaSquare = false; 
     dragCyanSquare = false; 
     dragOrangeSquare = false; 
    } 
+0

aktualisiert Antwort präziser und zeigen ein Beispiel zu sein. – sorifiend