Adding images is a great way to add to a program. But there are some things to keep in mind:
- Your project must be saved!
- Your image must be saved in the same directory as your project
- Your image must be saved as one of the following formats: GIF, JPG, TGA, or PNG

If we wanted to use an image called turtle.jpg in a program called TurtleImage, we would save it into the same directory as the program.
Make sure your images are saved to the right place! You can quickly find the directory your project is saved in by going to Sketch > Show sketch folder.
When we use an image in a program, we must first load the image into a variable with the datatype of PImage. We load the image inside the setup() function because loading the image elsewhere will reduce the speed of the program.

Bouncing Image
To add an image to your project you must first save your project somewhere on your computer. Then you must save the images you want to use to the same directory as the project the image is being used in

int xPos = 100; int yPos = 100; int xSpeed = 2; int ySpeed = 3; PImage turtle; int turtleImageHeight = 80; int turtleImageWidth = 125; void setup() { size(500, 300); turtle = loadImage(“turtle.jpg”); } void draw() { background(255, 255, 255); // Check if turtle hit right wall if (xPos > width - (turtleImageWidth / 2)) { xSpeed *= -1; } // Check if turtle hit left wall if (xPos < 0 + (turtleImageWidth / 2)) { xSpeed *= -1; } // Check if turtle hit bottom wall if (yPos > height - (turtleImageHeight / 2)) { ySpeed *= -1; } // Check if turtle hit top wall if (yPos < 0 + (turtleImageHeight / 2)) { ySpeed *= -1; } /* Adjust the x and y-coordinates of the center of the circle */ xPos += xSpeed; yPos += ySpeed; imageMode(CENTER); image(turtle, xPos, yPos); }
If your image is not showing up in your project, check to make sure that you spelled the name of the image exactly as it appears in the file directory (including
the format of the image, like .jpg or .png)
Leave a Reply