How do you put shapes in front of others?

edited March 2018 in How To...

I'm in 9th grade and we've started doing processing and I found it quite fun so I decided to make my own little things at home. I tried making faces but for some reason I type in a line of code to draw a line before any of the other shapes and it still goes behind all of the other shapes. Could anyone explain why this happens and how I could fix it please, thanks :)

Answers

  • in 2D:

    it's about the order in that you draw things.

    It's like on a real canvas: When you draw it, it's on the canvas. When you draw something over it, the latter is above (in front) the previous. So you draw each item on top of the other.

    Example:

    float angle1=.4;
    
    void setup()
    {
      // runs once
      // init
      size(800, 600);
      background(255);
    }
    
    void draw() 
    { 
      // runs on and on in a loop
      // 
      background(255);
    
      // the big gray rect
      pushMatrix();
      noStroke();
      translate(100, 110);
      rotate(angle1);
      fill(111);  // gray 
      rect (-50, -60, 100, 120);  
      popMatrix();
    
      // the smaller red rect        // it's in front
      fill(255, 0, 0);  // red 
      rect (100, 100, 50, 42);
    }
    
  • edited March 2018

    (they was also a thing that did some optimisation by drawing shapes and lines in groups so it might be this. There's a hint to turn it off iirc. But try the above first)

    hint(DISABLE_OPTIMIZED_STROKE);
    
  • @neutronstar: re:

    or some reason I type in a line of code to draw a line before any of the other shapes and it still goes behind all of the other shapes.

    If this is simple 2D, then before = behind.

    Imagine a similar question:

    I have a sheet of paper. When a draw a line on it in red crayon before I draw a line on it in black crayon, for some reason the red line is behind the black line. Why?

    Because things you draw first may end up underneath things you draw later -- in the real world, and in simple Processing 2D!

Sign In or Register to comment.