Load an array of images and display them randomly in the canvas

edited November 2014 in How To...

Hello,

I'm having a problem in processing, I want load 20 elements (images and svgs), place them randomly in the canvas and then save it as a pdf or svg.. I'm new in processing, but I found this code underneath which place elements (not images or svgs) random in the canvas.. I don't know if I'm totally wrong but isn't possible to loadImage instead of rect, triangles and ellipse?

I hope someone can help me..

void setup () {
  size(800, 1200);
  noLoop();

  smooth(4);
  noStroke();
}

void mousePressed() {
  redraw();
}

void keyPressed() {
  redraw();
}

void draw() {
  clear();

  for (int i=0; i!=10; ++i)
    skull((int) random(width), (int) random(height));
}

void skull(int x, int y) {
  translate(x, y);

  fill(-1);
  ellipse(35, 35, 70, 70);
  rect(17, 65, 36, 15);

  fill(0);
  ellipse(18, 35, 17, 25);
  ellipse(52, 35, 17, 25);  

  triangle(27, 51, 32, 51, 32, 61);
  triangle(38, 51, 43, 51, 38, 61);

  rect(23, 65, 3, 15);
  rect(33, 65, 3, 15);
  rect(43, 65, 3, 15);

  resetMatrix();
}
Tagged:

Answers

  • Answer ✓

    ... isn't possible to loadImage instead of rect, triangle and ellipse?

    We can draw them in a PGraphics and get() a PImage from them:

  • Thank you for you reply.. I'm not quiet sure what the examples has to do with what I want to archieve.. Sorry..

    Is it possible in this example to add an array with PImage and then call it in void skull()..

    Sorry I'm new in this..

  • edited October 2014 Answer ✓

    I'm not quite sure what the examples have to do with what I want to achieve...

    I thought you wanted the skull() turned into a PImage? :-/
    Anyways, I've made it. Now you got a PImage skull: :-bd

    http://studio.processingtogether.com/sp/pad/export/ro.9D3bN1ux8U67I/latest

    /**
     * Skulls (v2.0)
     * by  Kristian_Johansen_87 (2014/Oct)
     * mod GoToLoop
     *
     * forum.processing.org/two/discussion/7760/
     * load-an-array-of-images-and-display-them-randomly-in-the-canvas
     *
     * studio.processingtogether.com/sp/pad/export/ro.9D3bN1ux8U67I/latest
     */
    
    static final int NUM = 10, FPS = 10;
    PImage skull;
    
    void setup() {
      size(800, 800, JAVA2D);
      noLoop();
      frameRate(FPS);
    
      noSmooth();
      imageMode(CORNER);
    
      skull = createSkull();
    }
    
    void draw() {
      background(0);
    
      for (int i = 0; i++ != NUM; image(skull
        , random(width - skull.width), random(height - skull.height)));
    }
    
    void mousePressed() {
      redraw();
    }
    
    void keyPressed() {
      redraw();
    }
    
    PImage createSkull() {
      PGraphics pg = createGraphics(70, 80, JAVA2D);
      pg.beginDraw();
    
      pg.smooth(4);
      pg.rectMode(CORNER);
      pg.ellipseMode(CENTER);
    
      pg.fill(-1);
      pg.noStroke();
    
      pg.ellipse(35, 35, 70, 70);
      pg.rect(17, 65, 36, 15);
    
      pg.fill(0);
    
      pg.ellipse(18, 35, 17, 25);
      pg.ellipse(52, 35, 17, 25);
    
      pg.triangle(27, 51, 32, 51, 32, 61);
      pg.triangle(38, 51, 43, 51, 38, 61);
    
      pg.rect(23, 65, 3, 15);
      pg.rect(33, 65, 3, 15);
      pg.rect(43, 65, 3, 15);
    
      pg.endDraw(); 
      return pg.get();
    }
    
  • Thank you so much for your help.. That was really helpful..

Sign In or Register to comment.