Manipulating Pictures, Arrays, and Loops part 1

Slides:



Advertisements
Similar presentations
Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
Advertisements

TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology August 2005.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
TOPIC 4 INTRODUCTION TO MEDIA COMPUTATION: DIGITAL PICTURES Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
How to use the Java class libraries Brief documentation of how to do this all with Java.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Copyright © 2009 Curt Hill The Picture Object Getting and displaying.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Jeopardy Heading1Heading2Heading3Heading4 Heading5 Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Two –Dimensional Arrays Mrs. C. Furman Java Programming November 19, 2008.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2005.
CSC1401. Learning Goals Understand at a conceptual level What is media computation? How does color vision work? How can you make colors with red, green,
Digital Pictures Represented by pixels –With a red, green, and blue value stored for each pixel (each has a range from 0 to 255) Stored in.jpg (JPEG) files.
ManipulatingPictures-Mod6-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
CSC1401 Manipulating Pictures. What we have done to date We have modified pictures by writing on top of them Using Turtles and using Graphics Drawing.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 4 INTRODUCTION TO MEDIA COMPUTATION: DIGITAL PICTURES Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
Chapter 02 Data and Expressions.
Getting and displaying
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Introduction to Java part 2
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops part 3
Georgia Institute of Technology
Digital Pictures Represented by pixels Stored in .jpg (JPEG) files
Week 13 - Monday CS 121.
Barb Ericson Georgia Institute of Technology Dec 2009
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 1
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops part 5
Introduction to Java part 2
Two-Dimensional Arrays and Nested Loops – part 1
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Media Computation
Teaching Java using Turtles part 3
Manipulating Pictures, Arrays, and Loops part 1
Other displays Saving Arrays Using fors to process
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Digital Pictures Represented by pixels Stored in .jpg (JPEG) files
Manipulating Pictures, Arrays, and Loops part 6
CS 177 Week 3 Recitation Slides
Digital Pictures Represented by pixels Stored in .jpg (JPEG) files
Introduction to Java part 2
Manipulating Pictures, Arrays, and Loops
CSC1401 Manipulating Pictures 2
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

Manipulating Pictures, Arrays, and Loops part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology  North Jeddah Branch King Abdulaziz University Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level How to manipulate digital pictures? What is an array? How to get information from a picture object? How to get and set information in a pixel object? How to create and work with a color object? How to import a class in a package? Georgia Institute of Technology

Georgia Institute of Technology Digital Pictures Represented by pixels With a red, green, and blue value stored for each pixel Stored in .jpg (JPEG) files International standard With lossy compression Lossy means not all data is stored But what is lost isn’t that important Compression means made smaller Other formats for storing digital pictures are GIFF and BMP For more about JPEG see http://en.wikipedia.org/wiki/Jpeg. See http://en.wikipedia.org/wiki/GIF for more information about GIFF. See http://en.wikipedia.org/wiki/Windows_bitmap for more information about BMP. This windows format is not compressed. Georgia Institute of Technology

Pictures have lots of Pixels How can we refer to each pixel? pixel1, pixel2, pixel3, pixel4, pixel5, … Do we really want to name each one? There are 640 x 480 = 307,200 pixels How do we deal with lots of data of the same type? Use an array Georgia Institute of Technology

Georgia Institute of Technology What is an Array? Storage for a sequence of items Of the same type You can access items by using an index The index starts at 0 The first item is at index 0 The last item is at index (length – 1) Arrays know their length (have a public length field) arrayObj.length 1 2 3 4 5 3 7 9 2 1 5 1 2 3 8 3 2 6 Think of seats in a row of a movie theater. They have an index too. They often start at 1. Java uses a 0-based index. This is a holdover from C when the index was really the distance from the beginning of the array. The distance from the beginning of the array to the first element is 0. Georgia Institute of Technology

Manipulating a Picture To manipulate a picture we need to manipulate the pixels that make up the picture Change the red, green, or blue values at the pixel Pixel is a class that we created at Georgia Tech Each pixel object has a red, green, and blue value Georgia Institute of Technology

What Data does a Picture Object Have? A picture object has an array of pixel objects That it read from the JPEG file It knows the picture width pictureObj.getWidth() It knows the picture height pictureObj.getHeight() It knows how to return an array of pixels Pixel[] pixelArray = pictureObj.getPixels() Georgia Institute of Technology

Georgia Institute of Technology Picture Exercise Create a picture in DrJava get the pictures width, height, and number of pixels String fileName = FileChooser.pickAFile(); Picture pictureObj = new Picture(fileName); int width = pictureObj.getWidth(); System.out.println(“The picture width is “ + width); int height = pictureObj.getHeight(); System.out.println(“The picture height is “ + height); Pixel[] pixelArray = pictureObj.getPixels(); System.out.println(pixelArray.length + “ pixels”); Georgia Institute of Technology

Georgia Institute of Technology Pixel Objects Each pixel has a red, green, and blue value getRed(), getGreen(), getBlue() setRed(v), setGreen(v), setBlue(v) Each pixel knows the location it was in the picture object getX(), getY() You can also get and set the color at the pixel getColor(), setColor(color) Georgia Institute of Technology

Georgia Institute of Technology Color Objects There is a class defined in Java that represents color The Color class in the package java.awt To use the class you must either import java.awt.Color; Use the full name java.awt.Color You can create a color object by giving the red, green, and blue values for it Color colorObj = new Color(255,10,125); Georgia Institute of Technology

Georgia Institute of Technology Predefined Colors The Color class has defined class constants for many colors Color.red, Color.green, Color.blue, Color.black, Color.white, Color.yellow, Color.gray, Color.orange, Color.pink, Color.cyan, Color.magenta Or you can use all uppercase names Color.RED, Color.BLUE, Color.BLACK, … Originally the predefined colors where are lowercase names. However, the Java convention for constants is to use all uppercase letters. So the uppercase names for the predefined colors were added. Georgia Institute of Technology

Getting and Setting Pixel Colors To get a pixel’s color as a color object Color color1 = pixelObj.getColor(); int red = color1.getRed(); int green = color1.getGreen(); int blue = color1.getBlue(); To set a pixel’s color using a new color object red = 20; green = 30; blue = 100; Color color2 = new Color(red,green,blue); pixelObj.setColor(color2); For pixelObj use whatever the name is of the current pixel object. Georgia Institute of Technology

Using Classes in Packages All classes in the Java language are in packages You can use any class in java.lang System, Math, Object For classes in other packages you need to import them import java.awt.Color; Import java.awt.*; //import all classes in this package To use the short name: Color Or use the fully qualified name packageName.ClassName java.awt.Color Some people prefer to import each class by naming each one in an import statement. This is helpful for other people who may not know what package each class is in. However, if you are using several classes from the same package you can use the ‘*’ to import all classes in the same package. This will not extend to sub-packages. Georgia Institute of Technology

Georgia Institute of Technology Undefined Class Error If you forget to import a class Yet, you use the short name for the class It won’t compile Undefined class error Undefined class errors mean You need to import the class Or you misspelled the class Or used the wrong case Georgia Institute of Technology

Georgia Institute of Technology Pixel Exercise In DrJava Pick a file and create a picture object Get the array of pixels from the picture object Get the 1st pixel from the array of pixels Pixel pixelObj = pixelArray[0]; // 0 is first one Get the red, green, and blue value for this pixel Get the x and y location of this pixel Get the color of this pixel Get the red, green, and blue values of the color Remember to import java.awt.Color. Georgia Institute of Technology

Georgia Institute of Technology Changing Pixel Colors There are two ways to change the color of a pixel in a picture Set the red, green, and blue values individually pixelObj.setRed(value), pixelObj.setGreen(value), pixelObj.setBlue(value), Or set the color pixelObj.setColor(colorObj) But, you won’t see any change in the picture Until you ask it to repaint: pictureObj.repaint(); Show will also repaint the picture. Georgia Institute of Technology

Georgia Institute of Technology Changing a Color The Color class has methods for making a color object Lighter colorObj.brighter(); Darker colorObj.darker(); Example > import java.awt.Color; > Color testColor = new Color(168,131,105); > System.out.println(testColor); java.awt.Color[r=168,g=131,b=105] > testColor = testColor.darker(); java.awt.Color[r=117,g=91,b=73] > testColor = testColor.brighter(); java.awt.Color[r=167,g=130,b=104] Georgia Institute of Technology

Georgia Institute of Technology Rounding Errors Notice that when you made the color darker and then lighter the resulting color was slightly off of the original The change is calculated in floating point The result is stored in integer form The decimal part is lost Rounding errors also occur because of the limited storage for floating point numbers We can’t store all the digits in some numbers Georgia Institute of Technology

Georgia Institute of Technology Summary Pictures have pixels You can change the picture by changing the color of the pixels Arrays let you store and retrieve values of the same type using an index You can ask a picture for it’s width, height, and an array of pixels You can get and set the color of a pixel You will need to import classes that you wish to use that aren’t in java.lang Georgia Institute of Technology