cs026_6_Part2

cs026_6_Part2 - Topic 5 Part 2 Modifying Pictures Using...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Topic 5 Part 2 Modifying Pictures Using Loops (While Loops) Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared by B. Ericson.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Learning Goals • To understand at a conceptual and practical level: • How to manipulate digital images using various methods • What is a loop • While loops
Background image of page 2
3 Pictures as a Grid of Pixels • Recall that pictures are organized as a grid (matrix) of pixels • The grid has columns and rows • Each pixel has an (x, y) position in the grid • x specifies the column • y specifies the row • This is called column-major order X Y
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Pictures as a Grid of Pixels • Note: the array of pixels we have been working with: Pixel[] pixelArray = pictureObj.getPixels(); does not capture this structure, because it is a 1-Dimensional array • Later we will be considering pictures as 2-Dimensional arrays of pixels X Y
Background image of page 4
5 Pictures as a Grid of Pixels • We can get the pixel’s location in the grid of pixels that make up the Picture object: getX() • Returns its x position (the column ) getY() • Returns its y position (the row )
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Pictures as a Grid of Pixels • To get a pixel at a particular x and y location: Pixel pixelObj = picture.getPixel(x,y); • The columns start with index 0 and end with index (height – 1) • The rows start with index 0 and end with index (width – 1) X Y
Background image of page 6
7 Changing a Picture Exercise import java.awt.Color; String fileName = FileChooser.pickAFile(); Picture pictureObj = new Picture(fileName); pictureObj.show(); pictureObj.getPixel(10,100).setColor(Color.black); pictureObj.getPixel(11,100).setColor(Color.black); pictureObj.getPixel(12,100).setColor(Color.black); pictureObj.getPixel(13,100).setColor(Color.black); pictureObj.getPixel(14,100).setColor(Color.black); pictureObj.getPixel(15,100).setColor(Color.black); pictureObj.getPixel(16,100).setColor(Color.black); pictureObj.getPixel(17,100).setColor(Color.black); pictureObj.getPixel(18,100).setColor(Color.black); pictureObj.getPixel(19,100).setColor(Color.black); pictureObj.repaint();
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Saving Changes to Pictures • After manipulating a picture, we can save our results to a file: pictureObj.write("newPicture.jpg"); • You can specify a full path so you know exactly where it is saved, for example: pictureObj.write(“Z:/janeST/MyPictures/newPicture.jpg”); • Or, you can use the FileChooser here too: String fileName = FileChooser.pickAFile(); pictureObj.write(fileName);
Background image of page 8
9 How Do We Know if it Worked? A very important part of programming is testing the results ! • Just because code compiles and runs without error doesn’t mean it is correct • There could be an error in the logic • It could fail under certain conditions • It could even return the correct answer but for the wrong reason • So, how can we easily test our work with pictures?
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 The Picture Explorer • This is a tool that creates a copy of the current picture and lets you
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/20/2009 for the course COMP SCI 026 taught by Professor Unknown during the Spring '07 term at UWO.

Page1 / 50

cs026_6_Part2 - Topic 5 Part 2 Modifying Pictures Using...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online