-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkAreaPanel.java
More file actions
201 lines (170 loc) · 6.96 KB
/
Copy pathWorkAreaPanel.java
File metadata and controls
201 lines (170 loc) · 6.96 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
194
195
196
197
198
199
200
201
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class WorkAreaPanel extends JPanel implements MouseListener, MouseMotionListener, ActionListener {
private int x1, x2, y1, y2;
private BlockSpawner step;
private BlockSpawner paintRed;
private BlockSpawner paintBlue;
private BlockSpawner paintGreen;
private BlockSpawner turn;
private BlockSpawner loop;
private TrashCan trashCan;
int offsetX, offsetY;
private Block selectedBlock;
JPanel buttonPanel = new JPanel();
public WorkAreaPanel() {
addMouseListener(this);
addMouseMotionListener(this);
this.setPreferredSize(new Dimension(700, 500));
setLayout(new BorderLayout());
this.setBackground(Color.WHITE);
step = new BlockSpawner(450, 100, Color.ORANGE, "Step");
turn = new BlockSpawner(450, 100, Color.ORANGE, "Turn");
paintRed = new BlockSpawner(450, 100, Color.RED, "Paint Red");
paintRed.setBackground(Color.RED);
paintRed.setOpaque(true);
paintRed.setBorderPainted(false);
paintBlue = new BlockSpawner(450, 100, Color.BLUE, "Paint Blue");
paintBlue.setBackground(Color.BLUE);
paintBlue.setOpaque(true);
paintBlue.setBorderPainted(false);
paintGreen = new BlockSpawner(450, 100, Color.GREEN, "Paint Green");
paintGreen.setBackground(Color.GREEN);
paintGreen.setOpaque(true);
paintGreen.setBorderPainted(false);
loop = new BlockSpawner(450, 100, Color.MAGENTA, "Loop");
loop.setBackground(Color.MAGENTA);
loop.setOpaque(true);
loop.setBorderPainted(false);
buttonPanel.add(step);
buttonPanel.add(turn);
buttonPanel.add(paintRed);
buttonPanel.add(paintBlue);
buttonPanel.add(paintGreen);
buttonPanel.add(loop);
step.addActionListener(this);
paintRed.addActionListener(this);
paintBlue.addActionListener(this);
paintGreen.addActionListener(this);
turn.addActionListener(this);
loop.addActionListener(this);
buttonPanel.setLayout(new GridLayout(6, 1));
this.add(buttonPanel, BorderLayout.EAST);
trashCan = new TrashCan(40, 450);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(Block block: DataSource.getInstance().getBlockArrayInstance()){
block.draw(g);
Block temp = block.next;
while(temp!= null){
temp.draw(g);
temp = temp.next;
}
}
trashCan.draw(g);
}
@Override
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
for(Block block: DataSource.getInstance().getBlockArrayInstance()){
if (((block instanceof ActionBlock) || (block instanceof LoopBlockDecorator))&& (block.getX1() <= x1 && x1 <= block.getX1() + 70) && (block.getY1() <= y1 && y1 <= block.getY1() + 15)) { // change to follow if inside block
if(block.getName() == "Loop Block"){selectedBlock = (LoopBlockDecorator) block;}
else{selectedBlock = (ActionBlock) block;}
offsetX = x1 - selectedBlock.getX1();
offsetY = y1 - selectedBlock.getY1();
break;
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
int x_diff = x2 - offsetX;
int y_diff = y2 - offsetY;
if (selectedBlock != null){
int blockLength = selectedBlock.getX_len();
int blockHeight = selectedBlock.getY_len();
if (((x_diff > 40 || x_diff + blockLength < 60) && (y_diff > 435 || y_diff + blockHeight < 495))){
trashCan.delete(selectedBlock);
}
for(int i = 0; i < DataSource.getInstance().getBlocksRunInstance().size();i++) {
System.out.println(DataSource.getInstance().getBlocksRunInstance().get(i).getName());
}
selectedBlock.moving(x_diff, y_diff);
Block temp = selectedBlock.next;
int shift_y = selectedBlock.getY_len();
while(temp != null){
temp.moving(x_diff, y_diff + shift_y);
shift_y += selectedBlock.getY_len();
temp = temp.next;
}
}
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
if (selectedBlock != null){
selectedBlock.connect();
repaint();
selectedBlock = null;
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().getClass().getName().equals("BlockSpawner")) {
if (((JButton) e.getSource()).getText().equals("Step")) {
ActionBlock ab = new ActionBlock(375, 50, "Step");
DataSource.getInstance().getBlockArrayInstance().add(ab);
DataSource.getInstance().getBlocksRunInstance().add(ab);
repaint();
}
if (((JButton) e.getSource()).getText().equals("Turn")) {
ActionBlock ab = new ActionBlock(375, 125, "Turn");
DataSource.getInstance().getBlockArrayInstance().add(ab);
DataSource.getInstance().getBlocksRunInstance().add(ab);
repaint();
}
if (((JButton) e.getSource()).getText().equals("Paint Red")) {
ActionBlock ab = new ActionBlock(375, 200, "Paint Red");
DataSource.getInstance().getBlockArrayInstance().add(ab);
DataSource.getInstance().getBlocksRunInstance().add(ab);
repaint();
}
if (((JButton) e.getSource()).getText().equals("Paint Blue")) {
ActionBlock ab = new ActionBlock(375, 275, "Paint Blue");
DataSource.getInstance().getBlockArrayInstance().add(ab);
DataSource.getInstance().getBlocksRunInstance().add(ab);
repaint();
}
if (((JButton) e.getSource()).getText().equals("Paint Green")) {
ActionBlock ab = new ActionBlock(375, 350, "Paint Green");
DataSource.getInstance().getBlockArrayInstance().add(ab);
DataSource.getInstance().getBlocksRunInstance().add(ab);
repaint();
}
if (((JButton) e.getSource()).getText().equals("Loop")) {
LoopBlockDecorator b = new LoopBlockDecorator(375, 425);
DataSource.getInstance().getBlockArrayInstance().add(b);
DataSource.getInstance().getBlocksRunInstance().add(b);
repaint();
}
}
}
}