Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 119 lines (101 sloc) 2.894 kB
a1dd7aa @camillol initial commit
authored
1 import java.awt.Graphics2D;
2 import java.awt.Shape;
3 import java.util.jar.*;
4
5 /* timing utilities */
6 float lastTime;
7
8 float stopWatch()
9 {
10 float x = lastTime;
11 lastTime = millis();
12 return lastTime - x;
13 }
14
15 /* graphics utilities */
16 Graphics2D g2;
17 Shape[] clipStack;
18 int clipIdx;
19
20 void setupG2D()
21 {
22 g2 = ((PGraphicsJava2D)g).g2;
23 clipStack = new Shape[32];
24 clipIdx = 0;
25 }
26
27 /* I can't believe this is not part of the Processing API! */
28 void clipRect(int x, int y, int w, int h)
29 {
30 g2.clipRect(x, y, w, h);
31 }
32
33 void clipRect(float x, float y, float w, float h)
34 {
35 g2.clipRect((int)x, (int)y, (int)w, (int)h);
36 }
37
38 void noClip()
39 {
40 g2.setClip(null);
41 }
42
43 void pushClip()
44 {
45 clipStack[clipIdx++] = g2.getClip();
46 }
47
48 void popClip()
49 {
50 g2.setClip(clipStack[--clipIdx]);
51 clipStack[clipIdx] = null;
52 }
53
54
55 /* about paths:
56 inside IDE: sketchPath: /Users/camillo/UIC/CS424 visualization/p3/cs424p3
57 dataPath(""): /Users/camillo/UIC/CS424 visualization/p3/cs424p3/data/
58
59 application: sketchPath: /Users/camillo/UIC/CS424 visualization/p3/cs424p3/application.macosx
60 dataPath(""): /Users/camillo/UIC/CS424 visualization/p3/cs424p3/application.macosx/data/
61
62 applet: sketchPath: null
63 dataPath(""): null
64 */
65
66 /* data utilities */
67 String[] listDataSubdir(String subdir)
68 {
69 String[] items = null;
70 /* dataPath does not work for application, either. it's only useful in the IDE */
71 /* if (sketchPath != null) {
72 println(dataPath(subdir));
73 File dir = new File(dataPath(subdir));
74 items = dir.list();
75 println(items);
76 } else */
77
78 ClassLoader cl = getClass().getClassLoader();
79 URL url = cl.getResource("META-INF/MANIFEST.MF"); /* just a random file that's known to exist */
80 if (url == null) { /* running in IDE */
81 File dir = new File(dataPath(subdir));
82 items = dir.list();
83 } else try { /* applet OR application */
84 JarURLConnection conn = (JarURLConnection)url.openConnection();
85 JarFile jar = conn.getJarFile();
86 Enumeration e = jar.entries();
87 String re = "data/" + subdir + "/(.*)";
88 /* note that jars don't have directory entries, or at least Processing's don't */
89 Set<String> itemSet = new LinkedHashSet<String>();
90 while (e.hasMoreElements()) {
91 JarEntry entry = (JarEntry)e.nextElement();
92 String[] groups = match(entry.getName(), re);
93 if (groups == null) continue;
94 String[] comps = split(groups[1], "/");
95 itemSet.add(comps[0]);
96 }
97 /*Iterator it = itemSet.iterator();
98 while (it.hasNext()) {
99 println(it.next());
100 }*/
101 items = (String[])itemSet.toArray(new String[0]);
102 } catch (IOException e) {
103 println(e);
104 }
105 return items;
106 }
107
108 boolean dataFileExists(String path)
109 {
110 InputStream is = createInput(path);
111 if (is == null) return false;
112 else {
113 try { is.close(); }
114 catch (IOException e) {}
115 return true;
116 }
117 }
118
Something went wrong with that request. Please try again.