Problem 77

Graphics Demo 1

We’re going to learn how to draw some stuff on the screen using Processing. You can easily download processing at home.

If you want to use pure Java, go to the Graphics section on Mr. Mitchell’s site.

Type this code into a Processing window

void setup() {
  size(800, 600);
  noLoop();
}

void draw() {
  stroke(0, 255, 0);
  noFill();
  rect(50, 20, 100, 200);        // draw rectangle
  fill(0, 255, 0);
  ellipse(220, 120, 100, 200);   // draw ellipse
  
  stroke(0, 0, 255);
  fill(0, 0, 255);
  rect(200, 400, 200, 20);
  
  noFill();
  ellipse(300, 480, 200, 100);
  
  fill(0);
  text("Graphics are pretty neat.", 500, 100);
  int x = width / 2;
  int y = height / 2;
  textSize(15);
  text("The first letter of this string is at (" + x + "," + y + ")", x, y);
}

What You Should Do on Your Own

Assignments turned in without these things will receive half credit or less. Answer any questions in comments at the top of GraphicsDemo1.java.

  1. How big is the window that appears? How many pixels wide? How many pixels tall?
  2. In the call to the function rect(), there are four numbers. What do they mean? Try changing them to figure it out.
  3. What about the call to ellipse()? What do the four numbers mean here?
  4. What are the two numbers in the call to text()?
  5. What happens when two objects overlap? Which one is drawn on top?
  6. Add a red square somewhere in the lower-right of the canvas.

◄ 76: Project: Blackjack 97: Picture Menu ►



Adapted from ProgrammingByDoing.com
©2013 Graham Mitchell

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.