Topic 5 Introduction to Pictures and Arrays - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Topic 5 Introduction to Pictures and Arrays

Description:

a Color object lighter or darker: colorObj.brighter(); colorObj.darker ... Color brightColor = testColor.brighter(); System.out.println(brightColor) ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 41
Provided by: barbaraja
Category:

less

Transcript and Presenter's Notes

Title: Topic 5 Introduction to Pictures and Arrays


1
Topic 5 Introduction to Picturesand Arrays
Notes adapted from Introduction to Computing and
Programming with Java A Multimedia Approach by
M. Guzdial and B. Ericson, andinstructor
materials prepared by B. Ericson.
1
2
Learning Goals
  • To understand at a conceptual and practical
    level
  • How to manipulate pictures by changing pixels
  • What is an array
  • How to use an array

2
3
Digital Pictures
  • Recall that we can represent a digital picture
    as a grid of pixels
  • With a red, green, and blue value stored for
    each pixel

3
4
Picture as a Grid of Pixels
X
  • A picture isorganized as a grid (matrix) of
    pixels
  • The grid has columnsand rows
  • Each pixel has an (x, y)position in the grid
  • x specifies the column, starting at 0
  • y specifies the row, starting at 0
  • This is called column-major order

Y
5
Picture Objects
  • Recall we can create a Picture object by
    String fileName FileChooser.pickAFile()
    Picture pictureObj new Picture (fileName)
  • A Picture object has properties width and height
  • We can get the picture width (in pixels) using
  • pictureObj.getWidth()
  • We can get the picture height (in pixels) using
  • pictureObj.getHeight()

5
6
Picture Exercise
  • Create a picture in DrJava and report the
    pictures width, height, and number of pixels
  • String fileName FileChooser.pickAFile()
  • Picture pictureObj new Picture(fileName)
  • pictureObj.show()
  • int width pictureObj.getWidth()
  • System.out.println("The picture width is "
    width)
  • int height pictureObj.getHeight()
  • System.out.println("The picture height is "
    height)
  • System.out.println("The picture contains"
    _________________ " pixels")

6
7
Manipulating Pictures
  • A picture is made up of pixels
  • So, to manipulate a picture, we manipulate the
    pixels that make up the picture
  • We can change the red, green, blue values of a
    pixel

7
8
The Pixel Class
  • We have a class called Pixel that models
    individual pixels
  • (also created at Georgia Tech Pixel.java is in
    the bookClasses folder)
  • Each object of the Pixel class has
  • An (x,y) position in a picture
  • x specifies the column, starting at 0
  • y specifies the row, starting at 0
  • A red, green, and blue value
  • Each is an integer between 0 and 255

8
9
The Picture Explorer
  • The Picture class has a method that lets us
    explore a picture by moving the cursor around
    in the picture
  • We can see the x and y values and the color of
    the pixel at the cursor
  • To use this on a Picture object
  • pictureObj.explore()

10
Creating Pixel Objects
  • We can get a pixel at a specific location in a
    picture by using the getPixel method of the
    Picture class
  • ExamplePixel pixel1 pictureObj.getPixel(0,0)
  • This will create a Pixel object from the pixel at
    the top left-hand corner of the picture
  • And store a reference to this Pixel object in the
    variable pixel1

11
Manipulating Pixel Objects
  • We can get and set the red, green, blue values of
    a Pixel object individually, using methods of the
    Pixel class
  • Example of getting a pixels color values
    int red pixel1.getRed()
  • int green pixel1.getGreen()
  • int blue pixel1.getBlue()
  • Example of setting a pixels color values
  • pixel1.setRed(red10)
  • pixel1.setGreen(0)
  • pixel1.setBlue(blue-10)

11
12
Pixel Location in a Picture
  • We can get the pixels 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)
  • Example what will be printed here?System.out.pri
    ntln(pixel1.getX() ,
    pixel1.getY())

13
Pictures Can Have Lots of Pixels
  • How could we refer to each of the Pixel objects
    from a picture?
  • pixel1, pixel2, pixel3, pixel4, pixel5,
  • But, in a 640 x 480 image, there are 307,200
    pixels!
  • Do we really want to name each one?
  • Computer languages have a way of efficiently
    dealing with lots of data of the same type
  • We use a data structure called an array

13
14
What is an Array?
  • An array is storage for a collection of items of
    the same type
  • You can access the individual 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)

0
1
2
3
4
5
3
7
9
2
1
5
Exampleint array of length 6 What value is
stored at index 0? at index 4?
14
15
What is an Array?
  • In Java, an array is an object
  • As with any other object
  • We need to declare a reference variable
  • We need to create the array object
  • We declare array reference variables by
  • type name
  • Example int numbers
  • This creates a reference variable called numbers
    that can be used to refer to an array of integers
  • But this does not actually create the array

15
16
Creating Arrays
  • Creating an array is much like creating other
    objects in Java we use the Java keyword new
  • Example numbers new int6
  • This creates an array of 6 integers, and has our
    reference variable numbers refer to it
  • The reference variable numbers refers to the
    entire collection of integers
  • We can declare and create in one statementint
    numbers new int6
  • Once we create an array, its size is fixed and we
    cannot change it

16
17
Creating Arrays
  • int numbers new int6
  • numbers
  • numbers is a reference variable
  • What object does it refer to?
  • What is stored in each element of the array now?
  • Why?

18
Array Indexing
  • Each item of an array can be accessed
    individually, using indexing
  • Examples
  • numbers0 refers to the first element of the
    array (the element at position 0)
  • numbers1 refers to the second element
    (the element at
    position 1)
  • numbersi refers to the (i1)th element
    (the element at
    position i)

18
19
Storing in Arrays
  • numbers0 3
  • numbers1 7
  • numbers2 9
  • numbers3 2
  • numbers4 1
  • numbers5 5
  • index 0 1 2 3
    4 5
  • numbers

3
7
9
2
1
5
20
Array Indexing
  • index
  • numbers 0 1 2 3 4 5

3
7
9
2
1
5
What would be printed by System.out.println(numb
ers0) How would we print the last item in
the array?
21
Initializing Arrays
  • We can declare, create, and initialize an array
    to specific values, in one statement
  • Example
  • int numbers 3, 7, 9, 2, 1, 5
  • This creates an array of 6 integers, and sets the
    initial values of the integers in the array
    according to the values specified
  • The length of the array is determined by the
    number of items listed

21
22
Initializing Arrays
  • int numbers 3, 7, 9, 2, 1, 5
  • numbers

3
7
9
2
1
5
23
Array Size
  • Java remembers the size of arrays for you
  • An array object has a special public property
    (attribute) called length, which stores the size
    of the array
  • Public attributes are accessed using the dot
    notation, just as with method calls
  • Note length is a variable, not a method!
  • So there are no parentheses after length
  • Example int arraySize numbers.length
  • Useful to get the last item in an array, for
    example
  • int lastNumber numbersnumbers.length 1

23
24
Array of Pixels
  • A Picture object has an array of pixels, i.e an
    array of Pixel objects
  • This is the color data it read from the image
    file when the Picture object was created
  • This array has
  • the pixels from the first row of the grid
  • followed by the pixels from the second row
  • etc.
  • To get the array of pixels to use to manipulate
    the picture
  • Pixel pixelArray pictureObj.getPixels()

24
25
Array of Pixels
  • Pixel pixelArray pictureObj.getPixels()
  • pixelArray 1st row
    2nd row etc.


pixel at (0,0)
pixel at (1,0)
  • This creates an array of Pixel objects
  • Note that each element of the array is
    actually a reference to a Pixel object

26
Picture Exercise
  • Create a picture in DrJava and report the number
    of pixels
  • String fileName FileChooser.pickAFile()
  • Picture pictureObj new Picture(fileName)
  • pictureObj.show()
  • Pixel pixelArray pictureObj.getPixels()
  • System.out.println("The picture contains"
    pixelArray.length " pixels")

26
27
Pixel Objects from a Pixel Array
  • How do you get an individual Pixel object from
    the pixel array?
  • How do you access an individual elementfrom an
    array normally?
  • You use indexing!
  • So, we use indexing to get Pixel objectsfrom the
    pixel array
  • For example, to get the first pixel
  • Pixel pixelObj pixelArray0

27
28
Pixel Objects from a Pixel Array
  • Pixel pixelObj pixelArray0
  • pixelArray 1st row
    2nd row etc.


pixel at (0,0)
pixel at (1,0)
pixelObj

29
More on Manipulating Pixel Objects
  • We have already seen how to get and set the red,
    green and blue values of a pixel individually
  • Or, we can get and set the color of the pixel as
    a whole using the methods
  • getColor()
  • setColor(colorObj) - where colorObj refers to
    an object from the class Color

30
The Color class
  • Recall the class defined in Java that represents
    color
  • The Color class in the package java.awt
  • Recall that a package is a group of related
    classes
  • To use the class, you must either use
  • The full name java.awt.Color
  • Or, much easier, use the import statement import
    java.awt.Color
  • Then you can just use the class name Color
    (without needing the name of the package as well)
  • In a Java program, import statements go at the
    beginning of the source file

30
31
Predefined Colors
  • The Color class has defined class constants for
    many different 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,
  • (We saw these with our turtles)

31
32
Color Objects
  • You can create a Color object by giving the red,
    green, and blue values
  • Example
  • Color colorObj new Color(255,10,125)

32
33
Making Colors Lighter or Darker
  • The Color class has methods for making a Color
    object lighter or darker colorObj.brighter()co
    lorObj.darker()
  • Example in Interactions pane
  • gt import java.awt.Color
  • gt Color testColor new Color(168,131,105)
  • gt System.out.println(testColor)
  • java.awt.Colorr168,g131,b105
  • gt Color darkColor testColor.darker()
  • gt System.out.println(darkColor)
  • java.awt.Colorr117,g91,b73
  • gt Color brightColor testColor.brighter()
  • gt System.out.println(brightColor)
  • java.awt.Colorr240,g187,b150

33
34
Getting and Setting Pixel Colors
  • To get a Pixels color as a Color object
  • Color color1 pixelObj.getColor()
  • int red color1.getRed()
  • int green color1.getGreen()
  • int blue color1.getBlue()
  • To set a Pixels color using a new Color object
  • Color color2 new Color(red10, 0, blue-10)
  • pixelObj.setColor(color2)

34
35
Choosing a Color
  • You can also get a color by using thefollowing
    methodColorChooser.pickAColor()
  • You can use this anywhere you would have used a
    Color object
  • Example
  • Color pickedColor ColorChooser.pickAColor()
  • pixelObj.setColor(pickedColor)

35
36
Pixel Color Review
  • What is the difference between this code segment
    and the one on the next slide?
  • int red pixelObj.getRed()
  • int green pixelObj.getGreen()
  • int blue pixelObj.getBlue()
  • System.out.println("r " red ", g "
    green

  • ", b " blue)

36
37
Pixel Color Review
  • Color colorObj pixelObj.getColor()
  • int red colorObj.getRed()
  • int green colorObj.getGreen()
  • int blue colorObj.getBlue()
  • System.out.println("r " red ", g "
    green

  • ", b " blue)
  • In what class are these methods getRed, getGreen,
    getBlue defined?
  • In what class are the methods getRed, getGreen,
    getBlue on the previous slide defined?
  • Will both code segments print the same thing?

37
38
Changing Colors in a Picture
  • We have seen how to change the color of a pixel
    in a picture
  • But, you wont see any change in the picture
    until you repaint the picture by
    pictureObj.repaint()
  • Another way to do this is by
    pictureObj.show()

38
39
Saving Changes to Pictures
  • After manipulating a picture, we can save our
    results to a file pictureObj.write("newPicture.j
    pg")
  • You can specify a full path so you know exactly
    where it is saved, for example
    pictureObj.write(Z/jane/MyPictures/newPicture.jp
    g)
  • Or, you can use the FileChooser here too
  • String fileName FileChooser.pickAFile()
  • pictureObj.write(fileName)

40
New Concepts in this Section
  • Java concepts
  • Arrays
  • Picture concepts
  • Picture as an array of Pixel objects
  • Pixel concepts
  • Getting/setting red, green, blue values of pixel
  • Getting/setting color of pixel as a Color object

40
Write a Comment
User Comments (0)
About PowerShow.com