// JavaProject DraggableShapes

// DraggableShapes.java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
class Canvas extends JPanel {
private static final long serialVersionUID = 0L;
private DraggableRectangle rectangle;
private DraggableEllipse ellipse;
int order = 0;
// order == 0: Zuerst wird das Rechteck gezeichnet.
// order == 1: Zuerst wird die Ellipse gezeichnet.
boolean isRhit = false; // Ist das Rechteck angeklickt worden?
boolean isEhit = false; // Ist die Ellipse angeklickt worden?
public Canvas() {
Adapter adapter =
new Adapter();
addMouseMotionListener(adapter);
addMouseListener(adapter);
rectangle =
new DraggableRectangle(70, 125, 40, 80);
ellipse = new DraggableEllipse(150, 70, 120, 80);
}
class DraggableRectangle
extends Rectangle2D.Float {
private static final long serialVersionUID = 0L;
public DraggableRectangle (float x, float y, float width, float height) {
setRect(x, y, width, height);
}
public boolean isHit(float x, float y) {
return contains(x, y);
}
public void changeX(float dx) {
this.x += dx;
//
Operatoren
}
public void changeY(float dy) {
this.y += dy;
}
public void changeWidth(float dw) {
this.width += dw;
}
public void changeHeight(float dh) {
this.height += dh;
}
}
class DraggableEllipse extends Ellipse2D.Float {
private static final long serialVersionUID = 0L;
public DraggableEllipse(float x, float y, float width, float height) {
setFrame(x, y, width, height);
}
public boolean isHit(float x, float y) {
return contains(x, y);
}
public void changeX(float dx) {
this.x += dx;
}
public void changeY(float dy) {
this.y += dy;
}
public void changeWidth(float dw) {
this.width += dw;
}
public void changeHeight(float dh) {
this.height += dh;
}
}
private void defineRendering(Graphics2D g) {
RenderingHints rh;
rh = new RenderingHints (
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
g.setRenderingHints(rh);
}
private void drawShapes(Graphics2D g, int ord) {
if (ord == 0) {
g.setPaint(new Color(244, 172, 0));
g.fill(rectangle);
g.setPaint(new Color(52, 81, 142));
g.fill(ellipse);
} else {
g.setPaint(new Color(52, 81, 142));
g.fill(ellipse);
g.setPaint(new Color(244, 172, 0));
g.fill(rectangle);
}
}
private void defineDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
defineRendering(g2d);
drawShapes(g2d, order);
g2d.dispose();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
defineDrawing(g);
}
class Adapter extends MouseAdapter { //
Ereignisse
private int x;
private int y;
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
@Override
public void mouseDragged(MouseEvent e) {
handleEvent(e);
}
@Override
public void mouseReleased(MouseEvent e) {
isEhit = false;
isRhit = false;
}
private boolean haveCommonArea(DraggableEllipse el, DraggableRectangle rc) {
boolean result = false;
if (el.intersects(rc.x, rc.y, rc.width, rc.height)) result = true;
return result;
}
private void moveEllipse(int dx, int dy) {
ellipse.changeX(dx);
ellipse.changeY(dy);
repaint();
}
private void moveRectangle(int dx, int dy) {
rectangle.changeX(dx);
rectangle.changeY(dy);
repaint();
}
private void handleEvent(MouseEvent e) {
int dx = e.getX() - x;
int dy = e.getY() - y;
isRhit = (rectangle.isHit(x, y)) ? true : false;
isEhit = (ellipse.isHit(x, y)) ? true : false;
if ((isRhit)&&(!isEhit)) {
if (!haveCommonArea(ellipse, rectangle)) order = 1;
moveRectangle(dx, dy);
}
if ((!isRhit)&&(isEhit)) {
if (!haveCommonArea(ellipse, rectangle)) order = 0;
moveEllipse(dx, dy);
}
if ((isRhit)&&(isEhit)) {
if (order == 0) moveEllipse(dx, dy);
else moveRectangle(dx, dy);
}
x += dx;
y += dy;
}
}
}
class CFrame extends JFrame {
private static final long serialVersionUID = 0L;
Image icon;
public CFrame() {
setTitle("Verschiebbare Figuren");
icon = Toolkit.getDefaultToolkit().getImage("dh.png");
setIconImage(icon);
setSize(349, 301);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new Canvas());
setVisible(true);
}
}
public class DraggableShapes {
public static void main(String[] args) {
new CFrame();
}
}
Download DraggableShapes