-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchingShapesGame.java
151 lines (134 loc) · 5.54 KB
/
MatchingShapesGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MatchingShapesGame extends JPanel {
private List<Shape> shapes;
private List<Basket> baskets;
private Shape selectedShape;
private Point mouseOffset;
public MatchingShapesGame() {
shapes = new ArrayList<Shape>();
baskets = new ArrayList<Basket>();
baskets.add(new Basket(Color.RED, new Rectangle(50, 50, 100, 100)));
baskets.add(new Basket(Color.GREEN, new Rectangle(200, 50, 100, 100)));
baskets.add(new Basket(Color.BLUE, new Rectangle(350, 50, 100, 100)));
shapes.add(new Shape(Color.RED, new Rectangle(100, 200, 50, 50)));
shapes.add(new Shape(Color.GREEN, new Rectangle(200, 200, 50, 50)));
shapes.add(new Shape(Color.BLUE, new Rectangle(300, 200, 50, 50)));
Collections.shuffle(shapes); // randomize shape order
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
selectedShape = null;
for (Shape shape : shapes) {
if (shape.getBounds().contains(e.getPoint())) {
selectedShape = shape;
mouseOffset = new Point(e.getX() - shape.getBounds().x, e.getY() - shape.getBounds().y);
break;
}
}
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
if (selectedShape != null) {
selectedShape.setBounds(e.getX() - mouseOffset.x, e.getY() - mouseOffset.y, selectedShape.getBounds().width, selectedShape.getBounds().height);
repaint();
}
}
});
}
private class Basket {
private Color color;
private Rectangle bounds;
public Basket(Color color, Rectangle bounds) {
this.color = color;
this.bounds = bounds;
}
public Color getColor() {
return color;
}
public Rectangle getBounds() {
return bounds;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Basket basket : baskets) {
g.setColor(basket.getColor());
g.fillRect(basket.getBounds().x, basket.getBounds().y, basket.getBounds().width, basket.getBounds().height);
}
for (Shape shape : shapes) {
g.setColor(shape.getColor());
g.fillRect(shape.getBounds().x, shape.getBounds().y, shape.getBounds().width, shape.getBounds().height);
}
if (selectedShape != null) {
g.setColor(selectedShape.getColor());
g.fillRect(selectedShape.getBounds().x, selectedShape.getBounds().y, selectedShape.getBounds().width, selectedShape.getBounds().height);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Matching Shapes to Baskets Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.add(new MatchingShapesGame());
frame.setVisible(true);
}
private class Shape {
private Color color;
private Rectangle bounds;
public Shape(Color color, Rectangle bounds) {
this.color = color;
this.bounds = bounds;
}
public Color getColor() {
return color;
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(int x, int y, int width, int height) {
bounds.setBounds(x, y, width, height);
for (Basket basket : baskets) {
if (basket.getBounds().contains(bounds)) {
if (basket.getColor().equals(color)) {
// Shape is in the correct basket
// Remove the shape and create a new one in a new location
shapes.remove(this);
if (!shapes.isEmpty()) {
shapes.add(new Shape(color, getRandomShapeBounds()));
}
selectedShape = null;
repaint();
System.out.println("Shape matched to basket!");
return;
} else {
// Shape is in the wrong basket, reset its position
bounds.setLocation(getRandomShapeBounds().getLocation());
selectedShape = null;
repaint();
System.out.println("Shape not matched to correct basket.");
return;
}
}
// Shape is not in any basket, reset its position
bounds.setLocation(getRandomShapeBounds().getLocation());
selectedShape = null;
repaint();
System.out.println("Shape not matched to any basket.");
}
}
}
private Rectangle getRandomShapeBounds() {
int x = (int) (Math.random() * (getWidth() - 50));
int y = (int) (Math.random() * (getHeight() - 50)) + 100;
return new Rectangle(x, y, 50, 50);
}
}