0
저는 Java가 처음이며 여섯 개의 사각형을 드래그하는 애플릿에 대해 작업하고 있습니다. 지금까지 오른쪽에서 왼쪽 방향으로 각 사각형을 독립적으로 이동할 수있는 코드가 있습니다. 그러나 오른쪽에있는 정사각형을 왼쪽으로 끌면 오른쪽을 클릭 할 수 없습니다. 사각형을 놓을 수있는 방법이 있습니까? 이전에 옮긴 사각형을 드래그하여 선택하십시오.6 개의 사각형을 움직이는 Java 애플릿
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) { }
}
감사합니다.
업데이트 응답을 더 구체적으로하고 예를 보여
대신 현재의 mouseReleased 이벤트의이보십시오. – sorifiend