mirror of
https://github.com/processing/processing4.git
synced 2026-01-30 11:51:54 +01:00
This commit is contained in:
39
java/examples/Basics/Arrays/ArrayObjects/ArrayObjects.pde
Normal file
39
java/examples/Basics/Arrays/ArrayObjects/ArrayObjects.pde
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Array Objects.
|
||||
*
|
||||
* Demonstrates the syntax for creating an array of custom objects.
|
||||
*
|
||||
* Updated 26 February 2010.
|
||||
*/
|
||||
|
||||
int unit = 40;
|
||||
int count;
|
||||
Module[] mods;
|
||||
|
||||
|
||||
void setup() {
|
||||
size(320, 240);
|
||||
background(176);
|
||||
noStroke();
|
||||
|
||||
int wideCount = width / unit;
|
||||
int highCount = height / unit;
|
||||
count = wideCount * highCount;
|
||||
mods = new Module[count];
|
||||
|
||||
int index = 0;
|
||||
for (int y = 0; y < highCount; y++) {
|
||||
for (int x = 0; x < wideCount; x++) {
|
||||
mods[index++] = new Module(x*unit, y*unit, unit/2, unit/2, random(0.05, 0.8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
for (int i = 0; i < count; i++) {
|
||||
mods[i].update();
|
||||
mods[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
38
java/examples/Basics/Arrays/ArrayObjects/Module.pde
Normal file
38
java/examples/Basics/Arrays/ArrayObjects/Module.pde
Normal file
@@ -0,0 +1,38 @@
|
||||
class Module {
|
||||
int mx, my;
|
||||
int big;
|
||||
float x, y;
|
||||
int xdir = 1;
|
||||
int ydir = 1;
|
||||
float speed;
|
||||
|
||||
// Contructor (required)
|
||||
Module(int imx, int imy, int ix, int iy, float ispeed) {
|
||||
mx = imx;
|
||||
my = imy;
|
||||
x = ix;
|
||||
y = iy;
|
||||
speed = ispeed;
|
||||
big = unit;
|
||||
}
|
||||
|
||||
// Custom method for updating the variables
|
||||
void update() {
|
||||
x = x + (speed * xdir);
|
||||
if (x >= big || x <= 0) {
|
||||
xdir *= -1;
|
||||
x = x + (1 * xdir);
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
if (y >= big || y <= 0) {
|
||||
ydir *= -1;
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
}
|
||||
|
||||
// Custom method for drawing the object
|
||||
void draw() {
|
||||
stroke(second() * 4);
|
||||
point(mx+x-1, my+y-1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class ArrayObjects extends PApplet {
|
||||
|
||||
/**
|
||||
* Array Objects.
|
||||
*
|
||||
* Demonstrates the syntax for creating an array of custom objects.
|
||||
*/
|
||||
|
||||
int unit = 40;
|
||||
int num;
|
||||
Module[] mods;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
background(176);
|
||||
noStroke();
|
||||
|
||||
num = width/unit * width/unit;
|
||||
mods = new Module[num];
|
||||
|
||||
for (int i=0; i<height/unit; i++) {
|
||||
for(int j=0; j<height/unit; j++) {
|
||||
int index = i*height/unit + j;
|
||||
mods[index] = new Module(j*unit, i*unit, unit/2, unit/2, random(0.05f, 0.8f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
for(int i=0; i<num; i++) {
|
||||
mods[i].update();
|
||||
mods[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
class Module {
|
||||
float mx, my;
|
||||
int size = unit;
|
||||
float x, y = 0;
|
||||
int xdir = 1;
|
||||
int ydir = 1;
|
||||
float speed;
|
||||
|
||||
// Contructor (required)
|
||||
Module(float imx, float imy, float ix, float iy, float ispeed) {
|
||||
mx = imy;
|
||||
my = imx;
|
||||
x = PApplet.parseInt(ix);
|
||||
y = PApplet.parseInt(iy);
|
||||
speed = ispeed;
|
||||
}
|
||||
|
||||
// Custom method for updating the variables
|
||||
public void update() {
|
||||
x = x + (speed * xdir);
|
||||
if (x >= size || x <= 0) {
|
||||
xdir *= -1;
|
||||
x = x + (1 * xdir);
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
if (y >= size || y <= 0) {
|
||||
ydir *= -1;
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
}
|
||||
|
||||
// Custom method for drawing the object
|
||||
public void draw() {
|
||||
stroke(second()*4);
|
||||
point(mx+x-1, my+y-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "ArrayObjects" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Array Objects.
|
||||
*
|
||||
* Demonstrates the syntax for creating an array of custom objects.
|
||||
*/
|
||||
|
||||
int unit = 40;
|
||||
int num;
|
||||
Module[] mods;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
background(176);
|
||||
noStroke();
|
||||
|
||||
num = width/unit * width/unit;
|
||||
mods = new Module[num];
|
||||
|
||||
for (int i=0; i<height/unit; i++) {
|
||||
for(int j=0; j<height/unit; j++) {
|
||||
int index = i*height/unit + j;
|
||||
mods[index] = new Module(j*unit, i*unit, unit/2, unit/2, random(0.05, 0.8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
for(int i=0; i<num; i++) {
|
||||
mods[i].update();
|
||||
mods[i].draw();
|
||||
}
|
||||
}
|
||||
|
||||
class Module {
|
||||
float mx, my;
|
||||
int size = unit;
|
||||
float x, y = 0;
|
||||
int xdir = 1;
|
||||
int ydir = 1;
|
||||
float speed;
|
||||
|
||||
// Contructor (required)
|
||||
Module(float imx, float imy, float ix, float iy, float ispeed) {
|
||||
mx = imy;
|
||||
my = imx;
|
||||
x = int(ix);
|
||||
y = int(iy);
|
||||
speed = ispeed;
|
||||
}
|
||||
|
||||
// Custom method for updating the variables
|
||||
void update() {
|
||||
x = x + (speed * xdir);
|
||||
if (x >= size || x <= 0) {
|
||||
xdir *= -1;
|
||||
x = x + (1 * xdir);
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
if (y >= size || y <= 0) {
|
||||
ydir *= -1;
|
||||
y = y + (1 * ydir);
|
||||
}
|
||||
}
|
||||
|
||||
// Custom method for drawing the object
|
||||
void draw() {
|
||||
stroke(second()*4);
|
||||
point(mx+x-1, my+y-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BIN
java/examples/Basics/Arrays/ArrayObjects/applet/loading.gif
Normal file
BIN
java/examples/Basics/Arrays/ArrayObjects/applet/loading.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
Reference in New Issue
Block a user