-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoxDiagram.java
68 lines (56 loc) · 1.84 KB
/
BoxDiagram.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
import acm.graphics.*;
import acm.program.ConsoleProgram;
import acm.program.GraphicsProgram;
import acm.program.GraphicsProgram.*;
import acm.program.Program.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import javax.swing.*;
public class BoxDiagram extends GraphicsProgram {
private static final double BOX_WIDTH = 120;
private static final double BOX_HEIGHT = 50;
public void init(){
nameField = new JTextField(30);
add = new JButton("Add");
remove = new JButton("Remove");
clear = new JButton("Clear");
add(new JLabel("Name"),SOUTH);
add(nameField,SOUTH);
add(add,SOUTH);
add(remove,SOUTH);
add(clear,SOUTH);
nameField.addActionListener(this);
addActionListeners();
addMouseListeners();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==add){
addNewGCompound();
}
if(e.getSource()==remove){
GCompound objectToRemove = boxes.get(nameField.getText());
remove(objectToRemove);
}
if(e.getSource()==clear){
removeAll();
}
}
private void addNewGCompound() {
GCompound item = new GCompound();
GRect rect = new GRect(getWidth()/2 - BOX_WIDTH/2,getHeight()/2 + BOX_HEIGHT/2,BOX_WIDTH,BOX_HEIGHT);
item.add(rect);
String commandLabel = nameField.getText();
GLabel name = new GLabel(commandLabel,getWidth()/2-BOX_WIDTH/4,getHeight()/2 + BOX_HEIGHT);
name.setFont("Courier 24");
item.add(name);
add(item);
boxes.put(commandLabel,item);
}
/* instance vars*/
HashMap<String,GCompound> boxes = new HashMap<String,GCompound>();
private JTextField nameField;
private JButton add;
private JButton remove;
private JButton clear;
}