-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboard.java
More file actions
193 lines (148 loc) · 5.22 KB
/
board.java
File metadata and controls
193 lines (148 loc) · 5.22 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//IGNORE ALL THE ETRA COMMENTED SHIT I GOT I DIDNT WANT TO LOSE ANYTHING.
public class board extends JPanel implements MouseListener, MouseMotionListener {
// board things
public static final int TILE_SIZE = 50;
public static final int ROWS = 12;
public static final int COLUMNS = 18;
// things that appear on the board
// elements
private glass glass;
private fiber fiber;
// results
private fiberglass fiberglass;
// other
private ActionListener listener;
private boolean isDragging = false;
private int dragOffsetX, dragOffsetY;
private int dragOffsetX2, dragOffsetY2;
private Point draggedItemPosition;
//public Point mousePoint;
public boolean fiberDrag = false;
public boolean glassDrag = false;
public board(ActionListener listener) {
this.listener = listener;
}
public board() { // create board/background
setPreferredSize(new Dimension(TILE_SIZE * COLUMNS, TILE_SIZE * ROWS)); // set the size
setBackground(new Color(232, 232, 232)); // set the bg color
// initialize elements
glass = new glass();
fiber = new fiber();
// initialize some resulting elements
fiberglass = new fiberglass();
// add mouse listeners
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawBackground(g);
drawGlass(g);
drawFiberglass(g);
drawFiber(g);
}
private void drawBackground(Graphics g) { // draw the bg, may look weird cuz its orginally supposed to be checkered
g.setColor(new Color(232, 232, 232));
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLUMNS; col++) {
// only color every other tile
if ((row + col) % 2 == 1) {
// draw a square tile at the current row/column position
g.fillRect(
col * TILE_SIZE,
row * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
);
}
}
}
}
private void drawGlass(Graphics g) {
glass.draw(g, this);
}
private void drawFiber(Graphics g) {
fiber.draw(g, this);
}
private void drawFiberglass(Graphics g) {
fiberglass.draw(g, this);
}
// mouse listener methods
@Override
public void mousePressed(MouseEvent e) {
int mouseX = e.getX();
int mouseY = e.getY();
//mousePoint = new Point(mouseX, mouseY);
// check if the mouse is pressed on the fiber object
if (fiber != null && fiber.contains(new Point(mouseX, mouseY)) ) {
isDragging = true;
dragOffsetX = mouseX - fiber.pos.x;
dragOffsetY = mouseY - fiber.pos.y;
fiberDrag = true; //THESE TWO LINES (this and v) ARE SUPER IMPORTANT. ALSO AS WE ADD MORE ITEMS WE WILL HAVE TO SET THEM AS "FALSE" IN EVERY NON INSTANCE AS WELL
glassDrag = false;
//System.out.println("fiber: " + fiberDrag + " glass: " + glassDrag);
}
if (glass != null && glass.contains(new Point(mouseX, mouseY)) ) {
isDragging = true;
dragOffsetX = mouseX - glass.pos.x;
dragOffsetY = mouseY - glass.pos.y;
glassDrag = true;
fiberDrag = false;
//System.out.println("fiber: " + fiberDrag + " glass: " + glassDrag);
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (isDragging) {
int mouseX = e.getX();
int mouseY = e.getY();
int newX = mouseX - dragOffsetX;
int newY = mouseY - dragOffsetY;
if (fiberDrag == true) {
//int newX = mouseX - dragOffsetX;
//int newY = mouseY - dragOffsetY;
fiber.setPosition(new Point(newX, newY));
}
if (glassDrag == true) {
//int newX = mouseX - dragOffsetX2;
//int newY = mouseY - dragOffsetY2;
glass.setPosition(new Point(newX, newY));
}
//fiber.setPosition(new Point(newX, newY));
/*if (mouseX == fiber.pos.x && mouseY == fiber.pos.y) {
fiber.setPosition(new Point(newX, newY));
}
if (mouseX == glass.pos.x && mouseY == glass.pos.y) {
glass.setPosition(new Point(newX, newY));
}*/
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
// when the mouse button is released
isDragging = false;
fiberDrag = false;
glassDrag = false;
}
@Override
public void mouseClicked(MouseEvent e) {
// when the mouse button has been clicked (pressed and released)
}
@Override
public void mouseEntered(MouseEvent e) {
// when the mouse enters a component
}
@Override
public void mouseExited(MouseEvent e) {
// when the mouse exits a component
}
@Override
public void mouseMoved(MouseEvent e) {
// when the mouse button is moved ?!
}
}