-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProblem$08.java
195 lines (167 loc) · 5.67 KB
/
Problem$08.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
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
package chapter_twenty_four;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.nio.channels.Pipe;
/**
* *24.8 (Animation: array list) Write a program to animate search, insertion, and deletion
* in an array list, as shown in Figure 24.1a. The Search button searches the specified value
* in the list. The Delete button deletes the specified value from the list.
* The Insert button appends the value into the list if the index is not specified;
* otherwise, it inserts the value into the specified index in the list.
*
*
* @author Sharaf Qeshta
* */
public class Problem$08 extends Application
{
public static MyArrayList<Integer> list = new MyArrayList<>();
@Override
public void start(Stage primaryStage)
{
VBox pane = new VBox(10);
pane.setAlignment(Pos.CENTER);
Label status = new Label();
pane.getChildren().add(status);
Pane arrayListGUI = drawArrayList(list, 20, 30);
pane.getChildren().add(arrayListGUI);
HBox settings = new HBox(5);
settings.setAlignment(Pos.CENTER);
Label enterValue = new Label("Enter a value: ");
TextField value = new TextField();
value.setPrefColumnCount(6);
Label enterIndex = new Label("Enter an index: ");
TextField index = new TextField();
index.setPrefColumnCount(6);
Button search = new Button("Search");
Button insert = new Button("Insert");
Button delete = new Button("Delete");
Button trim = new Button("TrimToSize");
settings.getChildren().addAll(enterValue, value, enterIndex, index,
search, insert, delete, trim);
pane.getChildren().add(settings);
search.setOnAction(e ->
{
status.setText("");
int value_;
try
{
value_ = Integer.parseInt(value.getText());
}
catch (Exception exception)
{
return;
}
int index_ = list.indexOf(value_);
if (index_ != -1)
status.setText("The value " + value_ + " is at index " + index_ + " in the list");
else
status.setText("The value " + value_ + " is not in the list");
});
insert.setOnAction(e ->
{
status.setText("");
int value_;
int index_;
try
{
value_ = Integer.parseInt(value.getText());
try
{
index_ = Integer.parseInt(index.getText());
}
catch (Exception exception)
{
index_ = list.capacity;
}
}
catch (Exception exception)
{
return;
}
if (index_ < list.capacity)
list.add(index_, value_);
else
list.add(value_);
pane.getChildren().set(1, drawArrayList(list, 20, 30));
});
delete.setOnAction(e ->
{
status.setText("");
int value_;
try
{
value_ = Integer.parseInt(value.getText());
}
catch (Exception exception)
{
return;
}
int index_ = list.indexOf(value_);
if (index_ != -1)
list.remove(index_);
pane.getChildren().set(1, drawArrayList(list, 20, 30));
});
trim.setOnAction(e ->
{
status.setText("");
list.trimToSize();
pane.getChildren().set(1, drawArrayList(list, 20, 30));
});
Scene scene = new Scene(pane, 600, 500);
primaryStage.setScene(scene);
primaryStage.setTitle("Problem$08");
primaryStage.show();
}
public Pane drawArrayList(MyArrayList<Integer> list, int x, int y)
{
Pane pane = new Pane();
pane.setMinSize(500, 100);
pane.setStyle("-fx-border-color: black");
String status = (list.size() == 0) ? "array list is empty " : "";
status += "size = " + list.size() + " and capacity is " + list.capacity;
Text text = new Text(x+5, y+5, status);
pane.getChildren().add(text);
int x_ = x+10;
int y_ = y+20;
for (int i = 0; i < list.capacity; i++)
{
Integer currentElement = null;
if (i < list.size())
currentElement = list.get(i);
Element element = new Element(x_, y_, currentElement);
pane.getChildren().addAll(element, element.getDecorations());
x_ += 40;
}
return pane;
}
private static class Element extends Rectangle
{
Integer value;
public Element(int x, int y, Integer value)
{
super(x, y, 40, 20);
setFill(Color.WHITE); setStroke(Color.BLACK);
this.value = value;
}
public Node getDecorations()
{
if (value == null)
return new Line(getX() + getWidth(), getY(), getX(), getY()+getHeight());
else
return new Text(getX() + getWidth()/2 - 5, getY() + getHeight()/2 + 5, value + "");
}
}
}