copyright © 2009 curt hill the picture object getting and displaying

23
Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Upload: norah-wade

Post on 31-Dec-2015

224 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Copyright © 2009 Curt Hill

The Picture Object

Getting and displaying

Page 2: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

How are pictures shown?• Any image displayed on a

computer is a series of pixels• Pixel is an abbreviation of picture

elements• It is a dot of a particular color and

intensity• Each pixel has at least three

components:– Red, Green, Blue– All of which are in the range 0-255

Copyright © 2009 Curt Hill

Page 3: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

How are pictures organized?

• Pictures are always lines of pixels– The lines are horizontal

• The pictures are always rectangular– Each line is same length

• Monitors are generally in the 1024 by 760 size– May be higher or lower depending on

age and expense

• Digital cameras usually make much larger images

Copyright © 2009 Curt Hill

Page 4: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

How are pictures stored?

• There are many formats• JPEG – Joint Photograph Expert

Group– Usually have a .JPG extension on the

file

• GIF – Graphic Interchange Format– Owned by CompuServe– PNG is the public domain alternative

• Bitmap – Native format for most PCs

Copyright © 2009 Curt Hill

Page 5: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Storage

• Most file formats compress the image in some way

• Bitmaps generally the least• JPGs generally the best• GIFs are best for line drawings• Compression may be lossless or

lossy– JPG is lossy but the amount of loss is a

parameter to the compression process

Copyright © 2009 Curt Hill

Page 6: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Objects

• We will use the Java object named Picture and a few others

• It will allows us to read from disk and write to disk JPGs

• We will not have to worry about compression and other details

• We will be able to access individual pixels as well as display the picture easily

Copyright © 2009 Curt Hill

Page 7: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Constructors

• Pictures like all other objects need a constructor

• Recall there were two Turtle constructors with different parameters

• One took just the World object• The other took an initial location and

the World object• We will have multiple constructors

with Picture as wellCopyright © 2009 Curt Hill

Page 8: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Picture Constructors

• There is a default constructor:Picture pix = new Picture();

• This creates a picture object but does not initialize it

• The more helpful is the String constructor:Picture(String fileName);– The string is the file name of the picture– This could be a constant string or we

could find itCopyright © 2009 Curt Hill

Page 9: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

View• The string constructor makes the

object• It then loads the actual picture into

memory• It does not display it• This is done with the show method:

p.show();• It creates a new window to display

the picture• See the example program

Copyright © 2009 Curt Hill

Page 10: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Example

Copyright © 2009 Curt Hill

public class PictureDemo1{ public static void main(String [] a){ Picture p = new Picture( “C:/intro-prog-java/mediasources/barbara.jpg"); p.show(); }}

Page 11: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

What’s wrong with this picture?

• The problem is the constant file name

• Every time we run the program we get the same file

• What we would really like is the ability to choose any picture file on the disk

• We do this with FileChooser object

Copyright © 2009 Curt Hill

Page 12: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

FileChooser• FileChooser is an existing object• Produces a common dialog box

screen that makes it easy to choose a file

• The static method of the object that you want is called pickAFile

• This returns a string• This can then be given to the

Picture constructor

Copyright © 2009 Curt Hill

Page 13: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

The Program

Copyright © 2009 Curt Hill

Page 14: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Started

Copyright © 2009 Curt Hill

Page 15: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Display

Copyright © 2009 Curt Hill

Page 16: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Commentary

• The String declaration and call to FileChooser was made two lines so PowerPoint would not wrap

• It could be:String fileName = FileChooser.pickAFile();

• Displaying pictures is that easy

Copyright © 2009 Curt Hill

Page 17: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Now What?

• In order to do picture manipulation we need flow of control

• This includes loops and conditionals

• This is our next major topic (chapter 4)

• However, a turtle can drop a picture onto its world

Copyright © 2009 Curt Hill

Page 18: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Turtles and Pictures

• A turtle can place a picture on a world with the drop method

• This method takes a Picture as a parameter

• Something like this:Turtle t = new Turtle …Picture p = new Picture ……t.drop(p);

Copyright © 2009 Curt Hill

Page 19: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Commentary

• The picture is not resized• If the picture is too large it will be

clipped by the world• The orientation of the picture

matches the turtle– It will be shown tilted at the same

angle as the turtle

• Consider the following program

Copyright © 2009 Curt Hill

Page 20: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Example program

Copyright © 2009 Curt Hill

public class PictureDemo1{ public static void main(String [] a){ String fileName; fileName = FileChooser.pickAFile(); Picture p = new Picture(fileName); World w = new World(); Turtle yertle = new Turtle(w); yertle.forward(50); yertle.drop(p); yertle.forward(50); yertle.turn(-50); yertle.drop(p);}}

Page 21: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

Display

Copyright © 2009 Curt Hill

Page 22: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

FileChooser Again

• FileChooser is a wrapper for the JFileChooser object– Part of the standard Java package– FileChooser is made to be easier to use

• One problem with FileChooser is that it starts at the same directory– Depends on the system– In Windows usually MyDocuments

• The directory to start in can be changed

Copyright © 2009 Curt Hill

Page 23: Copyright © 2009 Curt Hill The Picture Object Getting and displaying

setMediaPath

• A method that tells FileChooser where to start

• Example:FileChooser.setMediaPath(“D:/intro-prog-java”);

• Always use forward slashes rather than backslashes

Copyright © 2009 Curt Hill