-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
executable file
·214 lines (197 loc) · 5.38 KB
/
State.java
File metadata and controls
executable file
·214 lines (197 loc) · 5.38 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
202
203
204
205
206
207
208
209
210
211
212
213
214
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
/**
* Written by Daniel Barter, Alby Himelick, and Grace Whitmore.
* For CS204 - Software Design
* 4 June 2012
*
* Implements a state in the NFA. Handles both the drawing of the state and
* storing of transitions for running the NFA.
*
* state final int RADIUS: the globally defined state radius which is used
* for finding states and drawing transitions
* ints xPos, yPos: the center of the state
* boolean acceptState: true if accept state, else false
* boolean selected: true if last clicked object, else false
* boolean transitionSelected: true if clicked for the initial state in a
* transition (uses different highlight color)
* ArrayList<Transition>s transitionsIn, transitionsOut: lists of the
* transitions in and out of the state
*/
public class State
{
public static final int RADIUS = 25;
protected int xPos;
protected int yPos;
protected boolean acceptState;
protected boolean selected;
protected boolean transitionSelected;
protected ArrayList<Transition> transitionsIn = new ArrayList<Transition>();
protected ArrayList<Transition> transitionsOut = new ArrayList<Transition>();
/**
* Constructor for a state. Initializes a state with a position and tells
* whether or not the state is an accept state. Sets the state to be
* selected.
*
* int xPos, yPos: the center of the state
* boolean acceptState: true if accept state and false otherwise
*/
public State(int xPos, int yPos, boolean acceptState)
{
this.xPos = xPos;
this.yPos = yPos;
this.acceptState = acceptState;
this.selected = true;
this.transitionSelected = false;
}
/**
* Returns the x position of the state.
*/
public int getXPos()
{
return xPos;
}
/**
* Returns the y position of the state.
*/
public int getYPos()
{
return yPos;
}
/**
* Returns true if the state is an accept state and false otherwise.
*/
public boolean isAcceptState()
{
return acceptState;
}
/**
* Sets the state to be an accept state or not.
*
* boolean b: true to be an accept state, false for not
*/
public void setAcceptState(boolean b)
{
acceptState = b;
}
/**
* Sets the state to be selected.
*
* boolean b: true to be selected, false to not
*/
public void setSelected(boolean b)
{
selected = b;
}
/**
* Sets the state to be selected as the first state in a transition.
*
* boolean b: true to be transition selected, false to not
*/
public void setTransitionSelected(boolean b)
{
transitionSelected = b;
}
/**
* Returns the array list of transitions pointing to the state.
*/
public ArrayList<Transition> getTransitionsIn()
{
return transitionsIn;
}
/**
* Returns the array list of transitions pointing away from the state.
*/
public ArrayList<Transition> getTransitionsOut()
{
return transitionsOut;
}
/**
* Draws the state. Color depends on class booleans.
*
* Graphics g: graphics object passed from paint to draw the state
*/
public void draw(Graphics g)
{
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(new BasicStroke(5));
if (transitionSelected)
{
g2D.setColor(new Color(204, 0, 0));
}
else if (acceptState)
{
if (selected)
{
g2D.setColor(new Color(30, 144, 255));
}
else
{
g2D.setColor(Color.BLUE);
}
}
else
{
if (selected)
{
g2D.setColor(Color.GRAY);
}
else
{
g2D.setColor(Color.BLACK);
}
}
g2D.drawOval(xPos - RADIUS, yPos - RADIUS, 2*RADIUS, 2*RADIUS);
g2D.setColor(Color.BLACK);
}
/**
* Moves the state and changes the x and y coordinates of the state and
* appropriately adjusts the x and y positions of the transitions
* associated with the state.
*
* int x, y: the new location of the center of the state
*/
public void move(int x, int y)
{
xPos = x;
yPos = y;
}
/**
* Adds a transition to the ArrayList of transitions pointing to the state.
*
* Transition t: the transition to add
*/
public void addTransitionIn(Transition t)
{
transitionsIn.add(t);
}
/**
* Adds a transition to the ArrayListt of transitions pointing away from the state.
*
* Transition t: the transition to add
*/
public void addTransitionOut(Transition t)
{
transitionsOut.add(t);
}
/**
* Removes a transition from the ArrayList of transitions pointing to the state.
*
* Transition t: the transition to remove
*/
public void removeTransitionIn(Transition t)
{
transitionsIn.remove(t);
}
/**
* Removes a transition from the ArrayList of transitions pointing away from the state.
*
* Transition t: the transition to remove
*/
public void removeTransitionOut(Transition t)
{
transitionsOut.remove(t);
}
}