My favorites | Sign in
Project Logo
                
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/** GeometricSeriesVisual.java
*
* Lil Applet for visualizing geometric series.
* Author: Ralph Gootee (c) 2007
* GPL License
**/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.Border;


public class GeometricSeriesVisual extends JApplet
implements ChangeListener, ActionListener, ListSelectionListener
{
private GeoVisual g;
private JSlider nSlider,aSlider,rSlider;

private JLabel nOut,aOut,rOut,convOut;

private JButton colorButton;

private JComboBox shapeCombo;

private JList pointList;

//------------------------------------------------------------------------------
public void init() {
//setBackground(Color.magenta);

GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
Container cp = getContentPane();

cp.setLayout(gridbag);

// Create the Geomertic Visual
g = new GeoVisual();

// Sames size for all sliders
Dimension outDim = new Dimension(30,20);
Dimension sDim = new Dimension(200,20);

int wxLabel = 1;
int wxSlider = 10;

// Geometric image
GeoImage img = new GeoImage();
c.insets = new Insets(5,5,5,5);
c.anchor = GridBagConstraints.CENTER;
c.gridx=0;c.gridy=0;c.gridwidth=1;c.gridheight=3;
c.weightx=1;c.weighty=.5;
cp.add(img,c);

// N
JLabel nLabel = new JLabel("N:");
c.insets = new Insets(0,0,0,0);
nLabel.setPreferredSize(outDim);
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.NONE;
c.gridx=1;c.gridy=0;c.gridwidth=1;c.gridheight=1;
c.weightx=wxLabel;
cp.add(nLabel,c);

nSlider = new JSlider(1,200,g.getN());
nSlider.addChangeListener(this);
nSlider.setMinimumSize(sDim);
c.fill = GridBagConstraints.BOTH;
c.gridx=2;c.gridy=0;
c.weightx=wxSlider;
cp.add(nSlider,c);

nOut = new JLabel(new Double(g.getN()).toString());
nOut.setMinimumSize(outDim);
nOut.setPreferredSize(outDim);
c.gridx=3;c.gridy=0;
c.weightx=.5;c.weighty=.5;
cp.add(nOut,c);

//A
JLabel aLabel = new JLabel("a:");
aLabel.setPreferredSize(outDim);
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.EAST;
c.gridx=1;c.gridy=1;
c.weightx=wxLabel;
cp.add(aLabel,c);

aSlider = new JSlider(1,200,aInv(g.getA()));
aSlider.addChangeListener(this);
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.WEST;
c.gridx=2;c.gridy=1;
c.weightx=wxSlider;
cp.add(aSlider,c);

aOut = new JLabel(new Double(g.getA()).toString());
aOut.setMinimumSize(outDim);
aOut.setPreferredSize(outDim);
c.gridx=3;c.gridy=1;
c.weightx=wxLabel;
cp.add(aOut,c);

// R
JLabel rLabel = new JLabel("r:");
rLabel.setPreferredSize(outDim);
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.EAST;
c.gridx=1;c.gridy=2;
c.weightx=wxLabel;
cp.add(rLabel,c);

rSlider = new JSlider(1,100,rInv(g.getR()));
c.fill = GridBagConstraints.BOTH;
rSlider.addChangeListener(this);
c.weightx=wxSlider;
c.gridx=2;c.gridy=2;
cp.add(rSlider,c);

rOut = new JLabel(new Double(g.getA()).toString());
rOut.setMinimumSize(outDim);
rOut.setPreferredSize(outDim);
c.weightx=wxLabel;
c.gridx=3;c.gridy=2;
cp.add(rOut,c);

// Visual
c.gridx=0;c.gridy=3;c.gridwidth=4;
c.weightx=1;c.weighty=1;
c.insets = new Insets(5,10,5,5);
c.fill=GridBagConstraints.BOTH;
cp.add(g,c);

// Color Button
colorButton = new JButton("color...");
colorButton.addActionListener(this);
c.anchor = GridBagConstraints.CENTER;
c.gridx=0;c.gridy=4;c.gridwidth=1;c.gridheight=1;
c.fill=GridBagConstraints.NONE;
cp.add(colorButton,c);

// Shape Combo Box
String [] shapes = {"Ellipse","Square"};
shapeCombo = new JComboBox(shapes);
shapeCombo.addActionListener(this);
c.gridx=1;c.gridy=4;
cp.add(shapeCombo,c);

// List
pointList = new JList();
pointList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
pointList.addListSelectionListener(this);

JScrollPane listScroller = new JScrollPane(pointList);
listScroller.setPreferredSize(new Dimension(200, 80));
c.insets = new Insets(5,20,5,5);
c.gridx=4; c.gridy=3;c.gridheight=1;c.gridwidth=2;
c.fill=GridBagConstraints.BOTH;
cp.add(listScroller,c);

JLabel convLabel = new JLabel("Converges: ");

c.gridx=4;c.gridy=4;c.gridheight=1;c.gridwidth=1;
cp.add(convLabel,c);

convOut = new JLabel("x");
c.gridx=5;c.gridy=4;
cp.add(convOut,c);

// Update the list
updateList();
g.repaint();
}

//------------------------------------------------------------------------------
public void actionPerformed(ActionEvent e) {
if(e.getSource() == colorButton){
Color c = JColorChooser.showDialog(this,"Pick a Color",g.getBaseColor());
if(c != null) g.setBaseColor(c);
}else if(e.getSource() == shapeCombo){
g.setShape(shapeCombo.getSelectedIndex());
}
}

//------------------------------------------------------------------------------
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();

if (!source.getValueIsAdjusting()){
if(source == nSlider){
int n = (int)source.getValue();
nOut.setText(new Integer(n).toString());
g.setN(n);
}else if(source == aSlider){
double a = aTrans(source.getValue());
g.setA(a);
aOut.setText(new Double(a).toString());

}else if(source == rSlider){
double r = (source.getValue());
g.setR(r);
rOut.setText(new Double(r).toString());
}
updateList();
}
}

//------------------------------------------------------------------------------
public void valueChanged(ListSelectionEvent listSelectionEvent) {

boolean adjust = listSelectionEvent.getValueIsAdjusting();

if (!adjust) {
JList list = (JList) listSelectionEvent.getSource();
int s = list.getSelectedIndex();
g.setSelectedIndex(s);
}
}

//------------------------------------------------------------------------------
public void updateList(){

double [] p = g.getPoints();
Double [] d = new Double[p.length];

for(int i=0;i<p.length;i++) d[i] = new Double(p[i]);

pointList.setListData(d);

convOut.setText(new Double(g.getConv()).toString() );
}

//------------------------------------------------------------------------------
private double aTrans(double i){ return (i - 100)/50;}

//------------------------------------------------------------------------------
private int aInv(double a){ return (int)Math.floor((a* 50) + 100.0);}

//------------------------------------------------------------------------------
private int rInv(double r){ return (int)Math.floor(r);}


//------------------------------------------------------------------------------
private class GeoImage extends JPanel{
Image img;

//------------------------------------------------------------------------------
public GeoImage(){
Dimension d = new Dimension(100,85);
setPreferredSize(d);
setMinimumSize(d);

Border blackline = BorderFactory.createLineBorder(Color.black);
setBorder(blackline);

try{
img = getImage(getDocumentBase(), "geometric_form.gif");

}catch(Exception e){
System.out.println("[Error] Could not Load Image!\n" + e);
}

}

//------------------------------------------------------------------------------
public void paint(Graphics g){
//g.setColor(Color.white);
//g.fillRect(0,0,getSize().width,85);

if(img!=null){
g.drawImage(img,20,5,this);
g.dispose();
}
}
}

}
Show details Hide details

Change log

r7 by rgootee on Apr 30, 2008   Diff
Made naming more obvious.
Go to: 
Project members, sign in to write a code review

Older revisions

r5 by rgootee on Aug 10, 2007   Diff
Layout is approaching usability -- not
quite there... GridBagLayout is
killing me
r4 by rgootee on Aug 10, 2007   Diff
Separated the main classes, added a
selection list box, added an Icon to
represent a geometric series.

r3 by rgootee on Aug 10, 2007   Diff
Restructure
All revisions of this file

File info

Size: 7564 bytes, 281 lines
Hosted by Google Code