-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessingTricks.txt
495 lines (392 loc) · 14.7 KB
/
ProcessingTricks.txt
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
### INIT
void init()
{
// Do stuff
// ...
super.init();
}
Called before setup()!
### STOP
void stop()
{
// Do stuff
// ...
super.stop();
}
Called when closing the sketch.
### Titleless sketch window
[url=http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1228323183;start=1#1]Re: hiding windows controls[/url]
// Avoids java.awt.IllegalComponentStateException: The frame is displayable.
// Can put this code in init()
frame.removeNotify();
frame.setUndecorated(true);
frame.addNotify();
frame.isUndecorated();
### Window always on top
frame.setAlwaysOnTop(true);
### Resize sketch window
// Any time
frame.setSize(w, h);
setSize(w, h); // Optional? Or not?
Or, to allow users to resize:
frame.setResizable(true); // in setup()
### Resize OpenGL canvas
[url=http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1227629232;start=4#4]Re: OpenGL canvas does not resize when embeded[/url]
pgl = (PGraphicsOpenGL) g;
gl = pgl.beginGL();
gl.glViewport(0, 0, width, height);
pgl.endGL();
### Resize canvas to given size
[url=http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1217592683]Re: problem resizing a window on the fly[/url]
int w = 200;
final static int INCR = 50;
Insets insets;
void setup()
{
size(200, 400);
background(100);
frame.pack(); // Get insets. Get more!
insets = frame.getInsets();
}
void draw()
{
background(100);
line(0, 0, width, height);
line(0, height, width, 0);
fill(#55AAFF);
for (int i = 0; i < width; i += INCR)
{
ellipse(i + INCR/2, height/2, INCR, INCR);
}
}
void mousePressed()
{
w = w + INCR;
// Change internal canvas size
setSize(w, height);
// width variable isn't updated yet, will be on draw()
println("Press: " + w + " => " + width);
int windowW = Math.max(w, MIN_WINDOW_WIDTH) +
insets.left + insets.right;
int windowH = Math.max(height, MIN_WINDOW_HEIGHT) +
insets.top + insets.bottom;
// Change frame size, taking in account the insets (borders, title bar)
frame.setSize(windowW, windowH);
}
### Sketch (frame) location
frame.getLocation();
frame.getLocationOnScreen(); // Identical result!
// In OpenGL mode put in the setup(), in normal mode put in the draw(). (?)
frame.setLocation(0, 0);
### Where to put libraries
[url=http://processing.org/discourse/yabb2/YaBB.pl?num=1269728211]Libraries -still confused about where to put...[/url]
### Rotate a shape around a given point
// Where we want the shape drawn, depending on its mode: that's where the 0, 0 drawing will occur
translate(600, 400);
// Move the center by offset
translate(-OFFSET, 0);
// Do the rotation
rotate(angle);
// Restore the center where it was
translate(OFFSET, 0);
// Draw the shape
fill(202);
ellipse(0, 0, ELLIPSE_WIDTH, 180);
### Handling Ctrl+Char
boolean ctrlPressed;
void keyPressed()
{
// special key
if (key == CODED)
{
if (keyCode == CONTROL)
{
ctrlPressed = true;
}
}
// regular key
else
{
if (ctrlPressed)
{
// CTRL + KEY
}
else
{
// OTHER
}
}
}
void keyReleased()
{
if (key == CODED)
{
if (keyCode == CONTROL)
{
ctrlPressed = false;
}
}
}
### Java syntax
If the code is in a .pde file, you must use untyped Collections:
Processing uses Java 1.6 but is currently (1.0) stuck on 1.4 syntax. The 1.6 syntax is only usable within .java files in the sketch.
### How to Increase the Java Applet Memory Limit
[url=http://www.duckware.com/pmvr/howtoincreaseappletmemory.html]Sun Java Plugin - How to Increase the Java Applet Memory Limit[/url]
### Maximum memory usable by Java
The [url=http://processing.org/discourse/yabb2/YaBB.pl?num=1263007499]memory increase[/url] thread might be relevant. Basically, you need a 64bit Java on a 64bit OS (on a 64bit processor, of course).
See also [url=http://forums.sun.com/thread.jspa?threadID=5433568]The truth about JVM max memory usage. Does it use only 2gb?[/url] and [url=http://rsbweb.nih.gov/ij/docs/install/windows.html]ImageJ: Windows Installation[/url].
### Get a BI from a PImage:
BufferedImage bufimg = new BufferedImage(
smallImage.width, smallImage.height,
smallImage.format == ARGB ?
BufferedImage.TYPE_INT_ARGB :
BufferedImage.TYPE_INT_RGB);
bufimg.setRGB(0, 0, smallImage.width, smallImage.height,
smallImage.pixels, 0, smallImage.width);
Or just use PImage.getImage()...
### Create image from byte array
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1215762729;start=3#3
import java.awt.Toolkit;
Image GetFromJPEG(byte[] jpegBytes)
{
Image jpegImage = null;
println("Jpeg data length: " + jpegBytes.length);
try
{
jpegImage = Toolkit.getDefaultToolkit().createImage(jpegBytes);
}
catch (Exception e)
{
println("Problem creating image: " + e.toString() + ": " + e.getMessage());
}
// Image isn't loaded yet, we have to wait for end of processing of the data
// Stupid way, I suppose I should use an ImageObserver...
float waitTime = 0;
while (jpegImage.getHeight(null) == -1)
{
delay(25); // Don't hog CPU!
waitTime += 0.025;
}
println("After " + int(waitTime) + "s, I get an image of width " +
jpegImage.getWidth(null) + " and height " + jpegImage.getHeight(null));
return jpegImage;
}
byte[] bytes = null;
Image img = null;
void setup()
{
noLoop();
try
{
InputStream in = new FileInputStream("D:/_PhiLhoSoft/Processing/Johnson.jpg");
int l = in.available();
println("L: " + l);
bytes = new byte[l];
in.read(bytes);
}
catch (Exception e)
{
println("Problem reading image: " + e.toString() + ": " + e.getMessage());
}
// Initial load
img = GetFromJPEG(bytes);
println(img.getClass().toString());
// sun.awt.image.ToolkitImage tki = (sun.awt.image.ToolkitImage) img;
// BufferedImage bi = tki.getBufferedImage();
// println(bi.getClass().toString());
size(img.getWidth(null), img.getHeight(null));
}
void draw() // In noLoop mode!
{
PImage pimg = new PImage(img);
image(pimg, 0, 0);
}
### Drag'n'drop from a file explorer to an applet window
[url=http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1147684168;start=5#5]Re: tomc filechooser in a class[/url]
import java.awt.dnd.*;
import java.awt.datatransfer.*;
DropTarget dt = new DropTarget(this, new DnDListener());
class DnDListener implements DropTargetListener
{
public void dragEnter(DropTargetDragEvent event)
{
//System.out.println("dragEnter " + event);
event.acceptDrag(DnDConstants.ACTION_COPY);
}
public void dragExit(DropTargetEvent event)
{
//System.out.println("dragExit " + event);
}
public void dragOver(DropTargetDragEvent event)
{
//System.out.println("dragOver " + event);
event.acceptDrag(DnDConstants.ACTION_COPY);
}
public void dropActionChanged(DropTargetDragEvent event)
{
//System.out.println("dropActionChanged " + event);
}
public void drop(DropTargetDropEvent event)
{
//System.out.println("drop " + event);
event.acceptDrop(DnDConstants.ACTION_COPY);
Transferable transferable = event.getTransferable();
DataFlavor flavors[] = transferable.getTransferDataFlavors();
int successful = 0;
for (int i = 0; i < flavors.length; i++)
{
try
{
Object stuff = transferable.getTransferData(flavors[i]);
if (!(stuff instanceof java.util.List)) continue;
java.util.List list = (java.util.List) stuff;
for (int j = 0; j < list.size(); j++)
{
Object item = list.get(j);
if (item instanceof File)
{
File file = (File) item;
String filename = file.getPath();
if (filename.lastIndexOf(".jp") != -1)
{
img = loadImage(filename);
}
else
{
println("Wrong filetype! Only jpg supported");
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
### Image upload
[url=http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1212658813;start=15#15]Re: save to web --- image export[/url]
You can find the code in three parts:
http://philho.pastebin.com/f68e779ad -- Upload.php
http://philho.pastebin.com/f26b7f366 -- ImageUpload.pde
http://philho.pastebin.com/f6783c437 -- DataUpload.java
Obviously the Upload.php must be on the server, and you have to adjust some parameters, mostly the $destDir path (should be dedicated to uploads, because of the auto-deletion feature) and the $maxFiles parameter.
ImageUpload is a sample / example Processing script using my image upload class, DataUpload.
### Code in Zoho
<ol class="code" style="background-color: rgb(224, 248, 238); font-family: 'andale mono',monospace; font-size: 0.9em;">
### Compact Discourse Page (Stylish)
@namespace url(http://www.w3.org/1999/xhtml);
@-moz-document url("http://processing.org/discourse/yabb2/YaBB.pl") {
/* I reduce headers height and widen the section list table
to try and display the state of as much sections as possible
without scrolling.
*/
/* Widen the display */
#container, #maincontainer { width: 1250px !important; }
/* Don't skip space before the list of board links */
#mainnav_noSub { margin-bottom: 5px !important; }
/* Give less space around the message nb line */
#container .content td[height] { height: 15px !important; }
/* Hide "Processing 1.0" above the section list */
#board_nav { display: none !important; }
/* Reduce the left and right margins of the section list */
#container .content { margin: 0 10px !important; }
/* Second (windowbg) column of the table: the section name and description */
td.windowbg:first-child + td.windowbg { width: 64% !important; }
/* The Last Post column */
td.windowbg2 { width: 18% !important; }
/* Hide moderator info (these are always the same... :-P) */
/*td.windowbg div.small { display: none !important; }*/
/* Or float them to the right, so they take less room */
td.windowbg div.small { float: right !important; margin-top: -21px !important; background-color: #EEFFEE !important; font-size: 0.9em !important; }
}
@-moz-document url-prefix("http://processing.org/discourse/yabb2/YaBB.pl?board=")
{
/* Widen the display */
#container, #maincontainer { width: 1250px !important; }
td.titlebg:first-child, td.titlebg:nth-child(3)
{ width: 50px !important; }
/* Second (titlebg) column of the table: the subject */
td.titlebg:first-child + td.titlebg { width: 30% !important; }
}
### Export to applet from command line (Windows)
From Processing install dir
java -cp lib/pde.jar;lib/jna.jar;lib/ecj.jar;lib/core.jar;lib/antlr.jar;C:/Java/jdk1.6.0_13/lib/tools.jar processing.app.Commander --sketch=E:/Dev/PhiLhoSoft/Processing/Bezier --export-applet --output=E:/Dev/PhiLhoSoft/Processing/Bezier/applet
### Sign jar
> keytool -genkey -keystore plKeyStore -alias PhiLho
Tapez le mot de passe du Keystore : Tagada!
Ressaisissez le nouveau mot de passe :
Quels sont vos prÚnom et nom ?
[Unknown] : Philippe Lhoste
Quel est le nom de votre unitÚ organisationnelle ?
[Unknown] :
Quelle est le nom de votre organisation ?
[Unknown] : PhiLhoSoft
Quel est le nom de votre ville de rÚsidence ?
[Unknown] : Sevran
Quel est le nom de votre Útat ou province ?
[Unknown] : 93270
Quel est le code de pays Ó deux lettres pour cette unitÚ ?
[Unknown] : fr
Est-ce CN=Philippe Lhoste, OU=Unknown, O=PhiLhoSoft, L=Sevran, ST=93270, C=fr ?
[non] : oui
SpÚcifiez le mot de passe de la clÚ pour <PhiLho>
(appuyez sur EntrÚe s'il s'agit du mot de passe du Keystore) :
> keytool -selfcert -keystore plKeyStore -alias PhiLho
Tapez le mot de passe du Keystore :
> jarsigner -keystore ..\..\..\plKeyStore DisplayText.jar PhiLho
Enter Passphrase for keystore:
Warning:
The signer certificate will expire within six months.
### Perspective transform
http://download.java.net/media/jai/javadoc/1.1.2_01/jai-apidocs/javax/media/jai/PerspectiveTransform.html
A perspective transformation is capable of mapping an arbitrary quadrilateral into another arbitrary quadrilateral, while preserving the straightness of lines.
Such a coordinate transformation can be represented by a 3x3 matrix which transforms homogenous source coordinates (x, y, 1) into destination coordinates (x', y', w). To convert back into non-homogenous coordinates (X, Y), x' and y' are divided by w.
[ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ]
[ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ]
[ w ] [ m20 m21 m22 ] [ 1 ] [ m20x + m21y + m22 ]
x' = (m00x + m01y + m02)
y' = (m10x + m11y + m12)
w = (m20x + m21y + m22)
X = x' / w
Y = y' / w
http://knol.google.com/k/koen-samyn/perspective-transformation/2lijysgth48w1/19#
aspectRatio: width / height
fov: field of view (in radians) -- eg. 45° or 0.785398163 radians
far: the distance to the far clip plane -- eg. 1
near: the distance to the near clip plane -- eg. 1000
a = aspectRatio ; f = far ; n = near ; t = tan(fov/2)
[ 1/a/t 0 0 0 ]
[ 0 1/t 0 0 ]
[ 0 0 f/(f - n) -1 ]
[ 0 0 -n*f/(f - n) 0 ]
http://answers.google.com/answers/threadview/id/515829.html
I recently wrote a function that you pass:
x0,y0, x1,y1, x2,y2, x3,y3: Coordinates of four corners of a box, in
clockwise order.
x,y: coordinates of two points in a flat image from 0,0 to SX-1,SY-1.
It writes the result on *px, *py. The result is the point which
corresponds to x,y inside projection box. I used int because of
performance. Floating point would probably give better results. The
code is:
void corPix(int x0, int y0, int x1, int y1, int x2, int y2, int x3,
int y3, int x, int y, int *px, int *py) {
intersectLines(px, py,
((SY-y)*x0 + (y)*x3)/SY, ((SY-y)*y0 + y*y3)/SY,
((SY-y)*x1 + (y)*x2)/SY, ((SY-y)*y1 + y*y2)/SY,
((SX-x)*x0 + (x)*x1)/SX, ((SX-x)*y0 + x*y1)/SX,
((SX-x)*x3 + (x)*x2)/SX, ((SX-x)*y3 + x*y2)/SX);
}
It uses two functions I defined:
int det(int a, int b, int c, int d) {
return a*d-b*c;
}
void intersectLines(int *px, int *py, int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4) {
int d = det(x1-x2,y1-y2,x3-x4,y3-y4);
if (d==0)
d = 1;
*px = det(det(x1,y1,x2,y2),x1-x2,det(x3,y3,x4,y4),x3-x4)/d;
*py = det(det(x1,y1,x2,y2),y1-y2,det(x3,y3,x4,y4),y3-y4)/d;
}