Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2009 Curt Hill The Picture Object Getting and displaying."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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 well Copyright © 2009 Curt Hill

8 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 it Copyright © 2009 Curt Hill

9 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

10 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(); }

11 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

12 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

13 The Program Copyright © 2009 Curt Hill

14 Started Copyright © 2009 Curt Hill

15 Display Copyright © 2009 Curt Hill

16 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

17 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

18 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

19 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

20 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); }

21 Display Copyright © 2009 Curt Hill

22 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

23 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


Download ppt "Copyright © 2009 Curt Hill The Picture Object Getting and displaying."

Similar presentations


Ads by Google