Skip to content

Commit 349b460

Browse files
committed
more source files for test
1 parent e08083b commit 349b460

201 files changed

Lines changed: 28257 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package com.polytopemedia;
2+
import java.awt.Color;
3+
import java.awt.Dimension;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
import java.io.BufferedReader;
7+
import java.io.File;
8+
import java.io.FileNotFoundException;
9+
import java.io.FileReader;
10+
import java.io.IOException;
11+
12+
import javax.swing.JApplet;
13+
import javax.swing.JButton;
14+
import javax.swing.JFrame;
15+
import javax.swing.JOptionPane;
16+
import javax.swing.JPanel;
17+
18+
import com.polytopemedia.polyhedra.nets.NameAndNet;
19+
import com.polytopemedia.polyhedra.nets.Net;
20+
import com.polytopemedia.polyhedra.nets.NetBuilder;
21+
import com.polytopemedia.polyhedra.ui.ImagePanel;
22+
import com.polytopemedia.polyhedra.ui.MainPanel;
23+
import com.polytopemedia.polyhedra.ui.MarkColors;
24+
import com.polytopemedia.polyhedra.ui.io.FileUtils;
25+
import com.polytopemedia.polyhedra.ui.io.JSCallback;
26+
import com.polytopemedia.polyhedra.ui.io.PolyhedronFileFilter;
27+
import com.polytopemedia.polyhedra.utils.Utils;
28+
29+
30+
public class PolyhedronApplet extends JApplet implements ActionListener{
31+
private String encodedNet;
32+
private String name;
33+
34+
public static boolean isJS = false;
35+
public static String myURL = "http://www.dr-mikes-math-games-for-kids.com/polyhedral-nets.html?";
36+
37+
static {
38+
boolean isJS = false;
39+
String url = myURL;
40+
/**
41+
* -- // BH getting the URL directly
42+
*
43+
* @j2sNative
44+
*
45+
* isJS = true;
46+
* url = document.location.href.split("#")[0];
47+
*/
48+
{}
49+
PolyhedronApplet.isJS = isJS;
50+
if (url != null)
51+
PolyhedronApplet.myURL = url;
52+
}
53+
54+
public void start() {
55+
56+
JPanel panel = ImagePanel.defaultImage();
57+
this.setContentPane(panel);
58+
NetBuilder netBuilder = new NetBuilder();
59+
60+
encodedNet = getParameterOrQuery("net"); // BH
61+
name = getParameterOrQuery("name"); // BH
62+
63+
// BH trim down the url now, not necessarily removing other parameters
64+
// that come prior to net= and name=
65+
myURL = myURL.split("#")[0].split("net=")[0].split("name=")[0];
66+
if (myURL.endsWith("?"))
67+
myURL = myURL.substring(0, myURL.length() - 1);
68+
69+
if (name != null && name.length() == 0) {
70+
name = null;
71+
}
72+
if (Utils.containsRudeWord(name)) {
73+
name = null;
74+
}
75+
try {
76+
netBuilder.decode(encodedNet);
77+
} catch (NullPointerException e) {
78+
encodedNet = null;
79+
name = null;
80+
}
81+
82+
addButton(panel, "New Net", "Create a new polyhedral net");
83+
if (FileUtils.canAccessFileSystem()) {
84+
addButton(panel, "Load Net", "Load a saved polyhedral net from your hard disk");
85+
}
86+
87+
88+
if (encodedNet != null) {
89+
addButton(panel, "Linked Net", "Show the linked polyhedral net"+(name == null ? "" : ", \""+name+"\""));
90+
showMainPanel(netBuilder.decode(encodedNet), name);
91+
}
92+
}
93+
94+
/**
95+
* Check query first, then parameters, for a key=value pair
96+
*
97+
* @param key
98+
* @return value or null
99+
* @author Bob Hanson
100+
*/
101+
private String getParameterOrQuery(String key) {
102+
// split out the key/value pairs in the query
103+
String query = getDocumentBase().getQuery();
104+
if (query != null) { // BH URL.getQuery() is supposed to return null.
105+
String[] fields = query.split("[\\=\\&#]");
106+
for (int i = 0; i < fields.length; i += 2)
107+
if (fields[i].equals(key))
108+
return fields[i + 1];
109+
}
110+
return getParameter(key);
111+
}
112+
113+
public void actionPerformed(ActionEvent e) {
114+
String cmd = e.getActionCommand();
115+
if (cmd.equalsIgnoreCase("linked net")) {
116+
try {
117+
showMainPanel(new NetBuilder().decode(encodedNet), name);
118+
} catch (NullPointerException ex) {
119+
// BH NetBuilder.decode can throw NullPointerException() for
120+
// invalid encodedNet
121+
ex.printStackTrace();
122+
}
123+
} else if (cmd.equalsIgnoreCase("new net")) {
124+
showMainPanel(null, null);
125+
} else if (cmd.equalsIgnoreCase("load net")) {
126+
PolyhedronFileFilter filter = new PolyhedronFileFilter();
127+
128+
final JSCallback cb = new JSCallback();
129+
cb.setRunnable(new Runnable() {
130+
public void run() {
131+
if (cb.getData() instanceof File) {
132+
//first time, dialog returns the File object with __bytes attached
133+
try {
134+
FileUtils.loadNameAndNet((File) cb.getData(), cb);
135+
} catch (Exception e) {
136+
JOptionPane.showMessageDialog(PolyhedronApplet.this, "I couldn't read a polyhedron from your file!", "Error!",
137+
JOptionPane.ERROR_MESSAGE);
138+
}
139+
} else {
140+
// second time, returns the NameAndNet
141+
showMainPanel((NameAndNet) cb.getData());
142+
}
143+
}
144+
});
145+
File file = FileUtils.getFile(filter, cb, false); // BH added asynchronous file load option
146+
if (file != null) {
147+
try {
148+
// Java only
149+
FileUtils.loadNameAndNet(file, cb); // adding callback
150+
} catch (Exception e1) {
151+
e1.printStackTrace();
152+
}
153+
if (!cb.getSuccessful()) {
154+
JOptionPane.showMessageDialog(this, "I couldn't read a polyhedron from your file!", "Error!",
155+
JOptionPane.ERROR_MESSAGE);
156+
}
157+
}
158+
}
159+
}
160+
161+
private void addButton(JPanel panel, String text, String tooltip) {
162+
JButton jb = new JButton(text);
163+
jb.setToolTipText(tooltip);
164+
jb.setActionCommand(text);
165+
panel.add(jb);
166+
jb.addActionListener(this);
167+
}
168+
169+
private void showMainPanel(NameAndNet nn) {
170+
showMainPanel(nn.getNet(), nn.getName());
171+
}
172+
private void showMainPanel(Net net, String name) {
173+
Color bgColor = Color.black;
174+
Color unjoinedEdgeColor = Color.red;
175+
Color joinedEdgeColor = Color.cyan;
176+
MarkColors markColors = new MarkColors(Color.green, Color.yellow, Color.blue);
177+
show(new MainPanel(joinedEdgeColor, bgColor, unjoinedEdgeColor, markColors,net, name), name);
178+
}
179+
180+
private static void show(JPanel panel, String title) {
181+
if (title == null) title = "Polyhedral Nets";
182+
JFrame jf = new JFrame(title);
183+
panel.setPreferredSize(new Dimension(800, 600));
184+
jf.setContentPane(panel);
185+
jf.pack();
186+
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
187+
jf.setVisible(true);
188+
}
189+
190+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.polytopemedia.colorfactory;
2+
3+
import java.awt.Color;
4+
5+
public class Bee implements ColorFactory {
6+
7+
public Color getColor(double x, double y, double z) {
8+
double r = (10*Math.sqrt(x*x+y*y+z*z))%1;
9+
return r < 0.5 ? Color.yellow : Color.black;
10+
}
11+
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.polytopemedia.colorfactory;
2+
3+
import java.awt.Color;
4+
5+
public interface ColorFactory {
6+
public Color getColor(double x, double y, double z);
7+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.polytopemedia.colorfactory;
2+
3+
import java.awt.Color;
4+
import java.awt.image.BufferedImage;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.net.URL;
9+
10+
import javax.imageio.ImageIO;
11+
12+
public class ImageColorFactory implements PlanarColorFactory{
13+
14+
private BufferedImage im;
15+
private final boolean repeat;
16+
private final Color bg;
17+
18+
public ImageColorFactory (URL imageURL, Color bg) throws IOException{
19+
this.bg = bg;
20+
this.repeat = bg == null;
21+
im = ImageIO.read(imageURL);
22+
}
23+
24+
public ImageColorFactory (InputStream stream, Color bg) throws IOException{
25+
this.bg = bg;
26+
this.repeat = bg == null;
27+
im = ImageIO.read(stream);
28+
}
29+
30+
public Color getColor(double x, double y) {
31+
if (repeat) {
32+
x = x%1;
33+
y = y%1;
34+
while (x < 0) x++;
35+
while (x >= 1) x--;
36+
while (y < 0) y++;
37+
while (y >= 1) y--;
38+
}
39+
if (x < 0 || x >= 1 || y < 0 || y >= 1) {
40+
return bg;
41+
}
42+
x *= (im.getWidth()-1);
43+
y *= (im.getHeight()-1);
44+
int ix = (int)(Math.floor(x));
45+
int iy = (int)(Math.floor(y));
46+
int jx = ix+1;
47+
int jy = iy+1;
48+
double tx = x-ix;
49+
double ty = y-iy;
50+
float[] f00 = extractColor(ix, iy);
51+
float[] f01 = extractColor(ix, jy);
52+
float[] f10 = extractColor(jx, iy);
53+
float[] f11 = extractColor(jx, jy);
54+
float[] f = new float[f00.length];
55+
for (int i=0; i<f.length; i++) {
56+
f[i] = (float) (tx*ty*f11[i]+tx*(1-ty)*f10[i]+(1-tx)*ty*f01[i]+(1-tx)*(1-ty)*f00[i]);
57+
}
58+
return new Color(f[0],f[1],f[2]);
59+
}
60+
61+
private float[] extractColor(int ix, int iy) {
62+
return new Color(im.getRGB(ix, iy)).getColorComponents(null);
63+
}
64+
65+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.polytopemedia.colorfactory;
2+
3+
import java.awt.Color;
4+
import java.util.Collection;
5+
import java.util.LinkedHashMap;
6+
7+
import com.polytopemedia.colorfactory.ColorFactory;
8+
import com.polytopemedia.polyhedra.PlacedPolygon;
9+
import com.polytopemedia.polyhedra.nets.Face;
10+
import com.polytopemedia.polyhedra.nets.FoldedNet;
11+
import com.polytopemedia.polyhedra.nets.Net;
12+
import com.polytopemedia.polyhedra.vec.Vector;
13+
14+
public class NetScaledColorFactory implements ColorFactory{
15+
16+
private final ColorFactory delegate;
17+
private Vector centre;
18+
private double scale;
19+
20+
public NetScaledColorFactory(Net net, ColorFactory delegate) {
21+
this.delegate = delegate;
22+
Collection<PlacedPolygon> polygons = new FoldedNet(net,net.findOptimalAngles(false)).getFacePositions().values();
23+
Vector sum = new Vector(0,0,0);
24+
int n = 0;
25+
for (PlacedPolygon poly : polygons) {
26+
Vector[] cornerPositions = poly.cornerPositions();
27+
for (Vector corner : cornerPositions) {
28+
sum = sum.plus(corner);
29+
n++;
30+
}
31+
}
32+
centre = sum.times(1.0/n);
33+
scale = 0;
34+
for (PlacedPolygon poly : polygons) {
35+
Vector[] cornerPositions = poly.cornerPositions();
36+
for (Vector corner : cornerPositions) {
37+
scale = scale + corner.minus(centre).length();
38+
}
39+
}
40+
scale = n/scale;
41+
}
42+
43+
public Color getColor(double x, double y, double z) {
44+
Vector v = new Vector(x,y,z);
45+
v = v.minus(centre).times(scale);
46+
return delegate.getColor(v.getX(), v.getY(), v.getZ());
47+
}
48+
49+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.polytopemedia.colorfactory;
2+
3+
import java.awt.Color;
4+
import java.util.Collection;
5+
6+
import org.omg.CORBA.portable.Delegate;
7+
8+
import com.polytopemedia.polyhedra.PlacedPolygon;
9+
import com.polytopemedia.polyhedra.nets.FoldedNet;
10+
import com.polytopemedia.polyhedra.nets.Net;
11+
import com.polytopemedia.polyhedra.vec.Vector;
12+
13+
public class NetScaledPlanarColorFactory implements ColorFactory{
14+
15+
private final Vector left;
16+
private final Vector up;
17+
private double minU;
18+
private double minL;
19+
private double uScale;
20+
private double lScale;
21+
private final PlanarColorFactory delegate;
22+
23+
public NetScaledPlanarColorFactory(PlanarColorFactory delegate, Net net, Vector left, Vector up) {
24+
this.delegate = delegate;
25+
this.left = left;
26+
this.up = up;
27+
Collection<PlacedPolygon> polygons = new FoldedNet(net,net.findOptimalAngles(false)).getFacePositions().values();
28+
minL = Double.POSITIVE_INFINITY;
29+
double maxL = Double.NEGATIVE_INFINITY;
30+
minU = Double.POSITIVE_INFINITY;
31+
double maxU = Double.NEGATIVE_INFINITY;
32+
for (PlacedPolygon poly : polygons) {
33+
Vector[] cornerPositions = poly.cornerPositions();
34+
for (Vector corner : cornerPositions) {
35+
double l = left.dot(corner);
36+
double u = up.dot(corner);
37+
minU = Math.min(minU, u);
38+
maxU = Math.max(maxU, u);
39+
minL = Math.min(minL, l);
40+
maxL = Math.max(maxL, l);
41+
}
42+
}
43+
lScale = 1.0/(maxL-minL);
44+
uScale = 1.0/(maxU-minU);
45+
}
46+
47+
public Color getColor(double x, double y, double z) {
48+
Vector v = new Vector(x,y,z);
49+
double l = left.dot(v)-minL;
50+
double u = up.dot(v)-minU;
51+
return delegate.getColor(l*lScale, u*uScale);
52+
}
53+
54+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.polytopemedia.colorfactory;
2+
3+
import java.awt.Color;
4+
5+
public interface PlanarColorFactory {
6+
public Color getColor(double x, double y);
7+
}

0 commit comments

Comments
 (0)