Image Assignment

Please see the images page for more details.
There is a more complete indepth discussion of images on the grade 12 notes (scroll down to unit 5, then down to point 8 - the green highlight).

Task 1

  1. Load a background image (example "underwater.jpg") that fills the graphics console (window).
  2. Load a smaller image (example "fish.png").
    This must have a transparent background (thus either png or gif file)
  3. Resize this image to a NEW image that is somewhere around 120 pixels wide and an appropriate height for the aspect ratio
  4. Draw this image on top of the background
  5. Now draw the image (fish) flipped horizontally, vertically, and both. You do this using drawImage() with a whole bunch of parameters.
    Java Graphics Class documentation.
  6. For a challenge, draw the left half of the fish and the right half of the fish separated.
    This page might be useful. Or watch this video, but make sure to turn CC on.

Code to resize an image

BufferedImage img = loadImage(filename);

//scale the image to be 100x80. This does not output BufferedImage, only Image.
However, drawImage() will draw both BufferedImage as well as Image
Image scaledImage = img.getScaledInstance(100, 80, Image.SCALE_DEFAULT);

Code to load an image (as a method)

static BufferedImage loadImage(String filename) {
	BufferedImage img = null;			
	try {
		img = ImageIO.read(new File(filename));
	} catch (IOException e) {
		System.out.println(e.toString());
		JOptionPane.showMessageDialog(null, "An image failed to load: " + filename , "ERROR", JOptionPane.ERROR_MESSAGE);
	}
	//DEBUG
	//if (img == null) System.out.println("null");
	//else System.out.printf("w=%d, h=%d%n",img.getWidth(),img.getHeight());
	
	return img;
}

Images to start with


fish.png (has a transparent background)

transparent background

underwater.jpg

underwater

...

The next thing that we'll do is make it move around with keystrokes.
Then draw a hitbox and check for collisions
and then learn how to use arraylists