Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 1bed101

Browse files
committed
added new examples
1 parent 3fc2e29 commit 1bed101

File tree

5 files changed

+321
-0
lines changed

5 files changed

+321
-0
lines changed

Diff for: getting_started/ex1_touchscreen/ex1_touchscreen.pde

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
void setup() {
2+
fullScreen();
3+
noStroke();
4+
fill(0);
5+
}
6+
7+
void draw() {
8+
background(204);
9+
if (mousePressed) {
10+
if (mouseX < width/2) {
11+
rect(0, 0, width/2, height); // Left
12+
} else {
13+
rect(width/2, 0, width/2, height); // Right
14+
}
15+
}
16+
}

Diff for: location_permissions/gps_example/gps_example.pde

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*****************************************************************************************
2+
3+
Android Processing GPS example
4+
5+
Query the phone's GPS and display the data on the screen
6+
7+
Rolf van Gelder - v 22/02/2011 - http://cage.nl :: http://cagewebdev.com :: info@cage.nl
8+
9+
Check the ACCESS_FINE_LOCATION permission in Sketch Permissions!
10+
11+
*****************************************************************************************/
12+
13+
// Import needed Android libs
14+
import android.content.Context;
15+
import android.location.Location;
16+
import android.location.LocationListener;
17+
import android.location.LocationManager;
18+
import android.os.Bundle;
19+
20+
// Set up the variables for the LocationManager and LocationListener
21+
LocationManager locationManager;
22+
MyLocationListener locationListener;
23+
24+
// Variables to hold the current GPS data
25+
float currentLatitude = 0;
26+
float currentLongitude = 0;
27+
float currentAccuracy = 0;
28+
String currentProvider = "";
29+
30+
// Android fonts
31+
String[] fontList;
32+
PFont androidFont;
33+
Context context;
34+
35+
void setup () {
36+
size(480, 640, P2D);
37+
// Sketch stays in portrait mode, even when the phone is rotated
38+
orientation(PORTRAIT);
39+
// Select a font to use
40+
fontList = PFont.list();
41+
androidFont = createFont(fontList[0], 26, true);
42+
textFont(androidFont);
43+
}
44+
45+
void draw()
46+
{
47+
background(0);
48+
// Display current GPS data
49+
text("Latitude: "+currentLatitude, 20, 40);
50+
text("Longitude: "+currentLongitude, 20, 75);
51+
text("Accuracy: "+currentAccuracy, 20, 110);
52+
text("Provider: "+currentProvider, 20, 145);
53+
}
54+
55+
// Override the activity class methods
56+
void onResume() {
57+
super.onResume();
58+
59+
context = getActivity();
60+
//context = surface.getActivity();
61+
locationListener = new MyLocationListener();
62+
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
63+
64+
// Register the listener with the Location Manager to receive location updates
65+
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
66+
}
67+
68+
void onPause() {
69+
super.onPause();
70+
//locationManager.removeGpsStatusListener(locationListener);
71+
}
72+
73+
/*****************************************************************/
74+
75+
// Class for capturing the GPS data
76+
class MyLocationListener implements LocationListener {
77+
// Define all LocationListener methods
78+
public void onLocationChanged(Location location) {
79+
// Save new GPS data
80+
currentLatitude = (float)location.getLatitude();
81+
currentLongitude = (float)location.getLongitude();
82+
currentAccuracy = (float)location.getAccuracy();
83+
currentProvider = location.getProvider();
84+
}
85+
86+
public void onProviderDisabled (String provider) {
87+
currentProvider = "";
88+
}
89+
90+
public void onProviderEnabled (String provider) {
91+
currentProvider = provider;
92+
}
93+
94+
public void onStatusChanged (String provider, int status, Bundle extras) {
95+
// Nothing yet...
96+
}
97+
}

Diff for: multitouch/mt_example/mt_example.pde

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// http://www.akeric.com/blog/?p=1435
2+
// multiTouchCore.pde
3+
// Eric Pavey - 2011-01-02
4+
// Code used to get multitouch working in Processing.
5+
// Add to what's in the draw() function to implement the magic.
6+
7+
// It's all about the MotionEvent:
8+
// http://developer.android.com/reference/android/view/MotionEvent.html
9+
10+
//------------------------------
11+
// Setup globals:
12+
13+
import android.view.MotionEvent;
14+
15+
// Define max touch events. My phone (seems to) support 5.
16+
int maxTouchEvents = 5;
17+
// This array will hold all of our queryable MultiTouch data:
18+
MultiTouch[] mt;
19+
20+
//------------------------------
21+
void setup() {
22+
fullScreen();
23+
// Populate our MultiTouch array that will track all of our touch-points:
24+
mt = new MultiTouch[maxTouchEvents];
25+
for(int i=0; i < maxTouchEvents; i++) {
26+
mt[i] = new MultiTouch();
27+
}
28+
noFill();
29+
stroke(255, 0, 0);
30+
strokeWeight(3);
31+
}
32+
33+
//------------------------------
34+
void draw() {
35+
background(0);
36+
// If the screen is touched, start querying for MultiTouch events:
37+
if (mousePressed == true) {
38+
// ...for each possible touch event...
39+
for(int i=0; i < maxTouchEvents; i++) {
40+
// If that event been touched...
41+
if(mt[i].touched == true) {
42+
// DO SOMETHING WITH IT HERE:
43+
// Amazing mult-touch graphics implemeneted....
44+
ellipse(mt[i].motionX, mt[i].motionY, mt[i].size, mt[i].size);
45+
}
46+
}
47+
}
48+
}
49+
50+
//------------------------------
51+
// Override parent class's surfaceTouchEvent() method to enable multi-touch.
52+
// This is what grabs the Android multitouch data, and feeds our MultiTouch
53+
// classes. Only executes on touch change (movement across screen, or initial
54+
// touch).
55+
56+
public boolean surfaceTouchEvent(MotionEvent me) {
57+
// Find number of touch points:
58+
int pointers = me.getPointerCount();
59+
// Set all MultiTouch data to "not touched":
60+
for(int i=0; i < maxTouchEvents; i++) {
61+
mt[i].touched = false;
62+
}
63+
// Update MultiTouch that 'is touched':
64+
for(int i=0; i < maxTouchEvents; i++) {
65+
if(i < pointers) {
66+
mt[i].update(me, i);
67+
}
68+
// Update MultiTouch that 'isn't touched':
69+
else {
70+
mt[i].update();
71+
}
72+
}
73+
74+
// If you want the variables for motionX/motionY, mouseX/mouseY etc.
75+
// to work properly, you'll need to call super.surfaceTouchEvent().
76+
return super.surfaceTouchEvent(me);
77+
}
78+
79+
//------------------------------
80+
// Class to store our multitouch data per touch event.
81+
82+
class MultiTouch {
83+
// Public attrs that can be queried for each touch point:
84+
float motionX, motionY;
85+
float pmotionX, pmotionY;
86+
float size, psize;
87+
int id;
88+
boolean touched = false;
89+
90+
// Executed when this index has been touched:
91+
//void update(MotionEvent me, int index, int newId){
92+
void update(MotionEvent me, int index) {
93+
// me : The passed in MotionEvent being queried
94+
// index : the index of the item being queried
95+
// newId : The id of the pressed item.
96+
97+
// Tried querying these via' the 'historical' methods,
98+
// but couldn't get consistent results.
99+
pmotionX = motionX;
100+
pmotionY = motionY;
101+
psize = size;
102+
103+
motionX = me.getX(index);
104+
motionY = me.getY(index);
105+
size = me.getSize(index);
106+
107+
id = me.getPointerId(index);
108+
touched = true;
109+
}
110+
111+
// Executed if this index hasn't been touched:
112+
void update() {
113+
pmotionX = motionX;
114+
pmotionY = motionY;
115+
psize = size;
116+
touched = false;
117+
}
118+
}

Diff for: sensors/ex1_accelerator/ex1_accelerator.pde

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import android.content.Context;
2+
import android.hardware.Sensor;
3+
import android.hardware.SensorManager;
4+
import android.hardware.SensorEvent;
5+
import android.hardware.SensorEventListener;
6+
7+
Context context;
8+
SensorManager manager;
9+
Sensor sensor;
10+
AccelerometerListener listener;
11+
float ax, ay, az;
12+
13+
void setup() {
14+
fullScreen();
15+
16+
// Version 3 of the mode:
17+
context = getActivity();
18+
19+
// In the version 4 of the mode, getActivity() is accessed through the surface object:
20+
//context = surface.getActivity();
21+
22+
manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
23+
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
24+
listener = new AccelerometerListener();
25+
manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
26+
27+
textFont(createFont("Arial", 60));
28+
}
29+
30+
void draw() {
31+
background(0);
32+
text("X: " + ax, 10, 60);
33+
text("Y: " + ay, 10, 120);
34+
text("Z: " + az, 10, 180);
35+
}
36+
37+
class AccelerometerListener implements SensorEventListener {
38+
public void onSensorChanged(SensorEvent event) {
39+
ax = event.values[0];
40+
ay = event.values[1];
41+
az = event.values[2];
42+
}
43+
public void onAccuracyChanged(Sensor sensor, int accuracy) {
44+
}
45+
}
46+
47+
public void onResume() {
48+
super.onResume();
49+
if (manager != null) {
50+
manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
51+
}
52+
}
53+
54+
public void onPause() {
55+
super.onPause();
56+
if (manager != null) {
57+
manager.unregisterListener(listener);
58+
}
59+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
float currH, currB;
2+
float nextH, nextB;
3+
float easing = 0.001;
4+
int lastChange = 0;
5+
6+
void setup() {
7+
fullScreen();
8+
colorMode(HSB, 100);
9+
currH = nextH = 100;
10+
currB = nextH = 100;
11+
}
12+
13+
void draw() {
14+
background(currH, currB, 100);
15+
updateCurrColor();
16+
if (5000 < millis() - lastChange) {
17+
pickNextColor();
18+
lastChange = millis();
19+
}
20+
}
21+
22+
void pickNextColor() {
23+
nextH = random(100);
24+
nextB = random(100);
25+
}
26+
27+
void updateCurrColor() {
28+
// Easing between current and next colors
29+
currH += easing * (nextH - currH);
30+
currB += easing * (nextB - currB);
31+
}

0 commit comments

Comments
 (0)