I would like to load and draw multiple/all images from a directory in Processing.
I cant find a way to extend the one image example:
PImage a;
void setup() {
size(800,800);
background(127);
a = loadImage("a/1.jpg");
noLoop();
}
void draw(){
image(a,random(300),random(300),a.width/2, a.height/2);
}
to multiple images.
Is there a simple way to achieve this?
Thank you very much.
I'm sure there are more elegant ways to do it, but wouldn't something as simple as this work?
PImage a;
Pimage b;
void setup() {
size(800,800);
background(127);
a = loadImage("a/1.jpg");
b = loadImage("b/1.jpg");
noLoop();
}
void draw(){
image(a,random(300),random(300),a.width/2, a.height/2);
image(b,random(300),random(300),b.width/2, b.height/2);
}
You can find an example of listing a directories here: http://processing.org/learning/topics/directorylist.html. The reference section for loops is here: http://processing.org/reference/loop_.html.
Imagine u have a known number of images (n) called 0.jpg, 1.jpg, 2.jpg..., then u can do sth like this:
PImage[] fragment;
int n=3;
void setup() {
size(400, 400);
fragment=new PImage[n];
for(int i=0;i<fragment.length;i++){
fragment[i]=loadImage(str(i) + ".jpg");
}
}
void draw(){
for(int i=0;i<fragment.length;i++){
image(fragment[i],20*i,20*i);
}
}
Related
In the processing language I am trying to create a translation similar to the image below:
Output Goal
In my code, the image is moving but it isn't showing the original picture in addition to the translation and it isn't being shown across the screen as i'd like.
I have included the code I have so far below:
PImage img;
int reps=10;
void setup()
{
size(600,120);
triangle(30,5,50,30,15,20);
save("image.png");
img=loadImage("image.png");
}
void draw()
{
for (int i=0; i<reps; i++);
{
pushMatrix();
image(img,0,0);
translate(img.height,0);
scale(-1,1);
image(img,0,0);
popMatrix();
}
}
This is what it produces so far:
current_output
Im happy it's translating, I am just trying to figure out how to see the original in addition to the translation and have it shown multiple times.
Thanks in advance!!
The images have to be translated differently depending on the index. The 2nd image has to be translated more than the 1st one and the 3rd more than the 2nd:
translate(img.height * i, 0);
function draw:
void draw()
{
for (int i=0; i<reps; i++);
{
pushMatrix();
translate(img.height * i, 0);
scale(-1,1);
image(img,0,0);
popMatrix();
}
}
I want to create points and then store them in an array. I'm doing this to put a linear regression through my data points afterwards. So I need to be able to cycle through all my points.
I could not find anything like that on the web for processing and as I was not really able to do it, I need your help. Here is my approach, but it doesn't seem to work:
ArrayList<dataPoint> dataPoints = new ArrayList<dataPoint>();
void setup(){
size(1000, 1000);
background(255);
}
void draw(){
for (int i = 1; i == dataPoints.size(); i++) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
dataPoint Point = dataPoints.get(i);
Point.display();
}
}
void mousePressed() {
dataPoints.add(new dataPoint(mouseX, mouseY));
}
class dataPoint {
float x;
float y;
dataPoint(int tempX, int tempY) {
x = tempX;
y = tempY;
}
void display() {
strokeWeight(10);
stroke(255,0,0);
point(x,y);
}
}
I would like to have a program to create points and store them in an array (or something similar, that you can cycle through).
Most of your code makes sense, there are only two gotchas I could spot that may prevent you from cycling through all your points and visualising them:
your condition is will go to an array index out of bounds: try for (int i = 0; i < dataPoints.size(); i++)
remember to clear the frame, otherwise you're drawing on top of the same dots over and over again
Remember array indices start at 0 in Processing/Java (and likewise the last index will not be the size() of your array, but the 1 less, hence the < in the for condition)
Here is your code with the above tweaks:
ArrayList<dataPoint> dataPoints = new ArrayList<dataPoint>();
void setup(){
size(1000, 1000);
}
void draw(){
background(255);
for (int i = 0; i < dataPoints.size(); i++) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
dataPoint Point = dataPoints.get(i);
Point.display();
}
}
void mousePressed() {
dataPoints.add(new dataPoint(mouseX, mouseY));
}
class dataPoint {
float x;
float y;
dataPoint(int tempX, int tempY) {
x = tempX;
y = tempY;
}
void display() {
strokeWeight(10);
stroke(255,0,0);
point(x,y);
}
}
Note that Processing has a handy PVector class (which has x,y properties) so you could do something like this:
ArrayList<PVector> dataPoints = new ArrayList<PVector>();
void setup(){
size(1000, 1000);
strokeWeight(10);
stroke(255,0,0);
noFill();
}
void draw(){
background(255);
beginShape();
for (int i = 0; i < dataPoints.size(); i++) {
PVector point = dataPoints.get(i);
vertex(point.x,point.y);
}
endShape();
}
void mousePressed() {
dataPoints.add(new PVector(mouseX, mouseY));
}
This a bit of a detail, but I recommend to following Java Naming Convention to keep the code consistent. (For example: renaming the dataPoint class to DataPoint and renaming the Point instance to point)
I was remaking a platformer after I closed it without saving my code and came across the error. I don't know how the error occurred and am completely lost.
Here's the code below(sorry for any incomplete code):
PImage saw = loadImage("saw.png");
game run = new game();
void setup(){
size(1000, 1000);
}
void draw(){
run.saw(100, 100);
}
class game{
boolean dead;
int ballX;
int ballY;
int sawRotate;
game(){
dead = false;
ballX = 100;
ballY = 50;
sawRotate = 1;
}
void ball(){
}
void saw(int x, int y){
pushMatrix();
rotate(sawRotate);
translate(x, y);
image(saw, 100, -100, 500, 500);
popMatrix();
}
void platform(){
}
}
You can't use Processing functions until after the setup() function has been called. This line happens before that:
PImage saw = loadImage("saw.png");
You need to move the call to loadImage() to be inside the setup() function:
PImage saw;
void setup(){
size(1000, 1000);
saw = loadImage("saw.png");
}
I am creating a small game in processing and I am trying to print a 2D array of square objects. I have this NullPointerException and I cannot seem to find anything like it on the web.
int edge = 10;
public int sizeOfRect = 50;
public int numberOfRects = 10;
Rectangle[][] player = new Rectangle[numberOfRects][numberOfRects];
public int k;
public int l;
public int kcount=0;
public int lcount=0;
void setup(){
background(200);
size(565, 565);
}
void draw(){
for(k=edge; k<width-edge; k+=55){
for(l=edge; l<height-edge; l+=55){
player[kcount][lcount].display();
lcount++;
}
lcount=0;
kcount++;
}
kcount=0;
}
and the Rectangle Class
class Rectangle{
int i;
int j;
Rectangle(){
i=k;//xcoor
j=l;//ycoor
}
void display(){
fill(0);
rect(i,j,sizeOfRect,sizeOfRect);
}
}
And finally the exception
Plain.pde:17:0:17:0: NullPointerException Finished. Could not run the
sketch (Target VM failed to initialize). For more information, read
revisions.txt and Help? Troubleshooting. Could not run the sketch.
Thank you in advance
You're creating a 2D array, but you're never filling that array with any objects. In other words, your 2D array is full of null values. That's why you're getting a NullPointerException.
You need to fill your array with values. Here's an example:
player[1][2] = new Rectangle();
You probably want to use a nested for loop to fill your array.
I have used Cinder about few weeks and i have some problem.
I am using method "drag-and-drop" in my program :
void TutorialApp::fileDrop(FileDropEvent drop){ /// drop are images
for(size_t i=0; i<drop.getNumFiles();++i){
Vertex imageVertex = Vertex((Vec2i(drop.getPos().x, drop.getPos().y+200)));
imageVertex.path = drop.getFiles()[i];
and next my step is draw Vertex with associeted image. So that is question: how to add resources in this case, or maybe there is more easy solution? Thank
Straight to the point:
First of all,you want to keep images (i.e gl::Texture) in your objects, that you want to draw. So in your class Vertex, add this gl::Texture as member. And then I suggest to use this function to load image and edit constructor, to take gl::Texture as parameter.
So you end up with something like this:
class testVertex
{
public:
testVertex(Vec2i _pos, gl::Texture image);
void draw(){
gl::draw(texture, pos);
}
private:
gl::Texture texture;
Vec2i pos;
};
///constructor
testVertex::testVertex(Vec2i _pos, gl::Texture image)
{
pos = _pos;
texture = image;
}
class BasicApp : public AppNative {
public:
void setup();
void mouseMove( MouseEvent event );
void mouseUp( MouseEvent event );
void keyUp(KeyEvent event);
void fileDrop(FileDropEvent event);
void update();
void draw();
//// create container for testVertex
vector <testVertex> mVertices;
}
/// To load images via drop...
void BasicApp::fileDrop(FileDropEvent event){
gl::Texture fileTexture = loadImage(event.getFile(0));
mVertices.push_back(testVertex(Vec2i(event.getX(), event.getY()), fileTexture));
}
/// To draw images...
void BasicApp::draw(){
for (int i = 0; i < mVertices.size(); i++)
{
mVertices[i].draw();
}
}
///