Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.

Similar presentations


Presentation on theme: "Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class."— Presentation transcript:

1 Week 13 - Wednesday

2  What did we talk about last time?  Color representation  Color class  Picture class

3

4

5

6  Straightforward idea  Flip an image around the y-axis  Maybe you want to decipher some of Leonardo’s writings  No, the other one

7  Given an image with width w and height h:  Moving from left to right in the original image, copy each column, storing each column from right to left in the new image

8 012 0 A 1 B 2 C 3 D 012 0 A 1 B 2 C 3 D OriginalMirrored

9  What would the code for mirroring look like? Picture picture = new Picture( file ); //the picture to be mirrored Picture mirrored = new Picture( picture.width(), picture.height() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) mirrored.set( picture.width() - i - 1, j, picture.get( i, j ) ); Picture picture = new Picture( file ); //the picture to be mirrored Picture mirrored = new Picture( picture.width(), picture.height() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) mirrored.set( picture.width() - i - 1, j, picture.get( i, j ) );

10  Pretty much the same thing  Instead of copying each column in reverse order, copy each row in reverse order

11

12  Another straightforward idea  Necessary if you are writing software for a digital camera (the user might turn the camera for portrait instead of landscape)

13  Given an image with width w and height h:  Create a new image with width h and height w  Copy each column in the original image into a row in the new image, remembering to move back along the row

14 012 0 A 1 B 2 C 3 D 0123 0 DCBA 1 2 Original Rotated

15  What would the code for a rotation look like? Picture picture = new Picture( file ); //the picture to be rotated Picture rotated = new Picture( picture.height(), picture.width() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) rotated.set( picture.height() - j - 1, i, picture.get( i, j ) ); Picture picture = new Picture( file ); //the picture to be rotated Picture rotated = new Picture( picture.height(), picture.width() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) rotated.set( picture.height() - j - 1, i, picture.get( i, j ) );

16  Rotating 180°, 270°, -90°, -180°, or -270° can be done using similar techniques or simply performing right rotations multiple times  Rotations that are not multiples of 90° are much trickier, result in non-rectangular final images, and need some trigonometry  Don’t worry about rotations that are not multiples of 90°

17

18  You’ve seen this many times before  Sometimes an image won’t fit nicely in on a PowerPoint slide

19  Let's just focus on growing, because that's what you need to do in your project  If we are just doubling the image, we create a new image whose width and height are twice the original  Then, we go through the new image, copying the pixels from the original using pixels whose column and row are half as big as the ones we are putting into the new image

20  Doubling an image  Each old pixels maps to four new ones 0123 0 AABB 1 AABB 2 CCDD 3 CCDD 01 0 AB 1 CD

21  This kind of resize works, but is crude  To shrinking an image, a clever resize might average together pixels  When growing an image, different techniques can be done to fill in “guesses” for pixels that sit between pixels from the original image, instead of just duplicating them  People who program Photoshop have thought long and hard about how to do these tasks better

22

23  The idea of inheritance is to take one class and generate a child class  This child class has everything that the parent class has (members and methods)  But, you can also add more functionality to the child  The child can be considered to be a specialized version of the parent

24  The key idea behind inheritance is safe code reuse  You can use old code that was designed to, say, sort lists of Vehicle s, and apply that code to lists of Car s  All that you have to do is make sure that Car is a subclass (or child class) of Vehicle

25  Java respects the subclass relationship  If you have a Vehicle reference, you can store a Car object in that reference  A subclass (in this case a Car ) is a more specific version of the superclass ( Vehicle )  For this reason, you can use a Car anywhere you can use a Vehicle  You cannot use a Vehicle anywhere you would use a Car

26  As long as Car is a subclass of Vehicle, we can store a Car in a Vehicle reference  Even in an array is fine  Storing a Vehicle into a Car doesn’t work Vehicle v = new Car("Lancer Evolution"); // perfectly okay Vehicle v = new Car("Lancer Evolution"); // perfectly okay Vehicle[] vehicles = new Vehicle[100]; for( int i = 0; i < vehicles.length; i++ ) vehicles[i] = new RocketShip(); // cool Vehicle[] vehicles = new Vehicle[100]; for( int i = 0; i < vehicles.length; i++ ) vehicles[i] = new RocketShip(); // cool Car c = new Vehicle(); // gives error

27

28  All this is well and good, but how do you actually create a subclass?  Let’s start by writing the Vehicle class public class Vehicle { public void travel(String destination) { System.out.println("Traveling to " + destination); } public class Vehicle { public void travel(String destination) { System.out.println("Traveling to " + destination); }

29  We use the extends keyword to create a subclass from a superclass  A Car can do everything that a Vehicle can, plus more public class Car extends Vehicle { private String model; public Car(String s) { model = s; } public String getModel() { return model; } public void startEngine() { System.out.println("Vrooooom!"); } public class Car extends Vehicle { private String model; public Car(String s) { model = s; } public String getModel() { return model; } public void startEngine() { System.out.println("Vrooooom!"); }

30  There is a part of the Car class that knows all the Vehicle members and methods Car car = new Car("Camry"); System.out.println( car.getModel() ); //prints "Camry" car.startEngine(); //prints "Vrooooom!" car.travel( "New York City" ); //prints "Traveling to New York City" Car car = new Car("Camry"); System.out.println( car.getModel() ); //prints "Camry" car.startEngine(); //prints "Vrooooom!" car.travel( "New York City" ); //prints "Traveling to New York City"

31  Each Car object actually has a Vehicle object buried inside of it  If code tries to call a method that isn’t found in the Car class, it will look deeper and see if it is in the Vehicle class  The outermost method will always be called Car model getModel() startEngine() Car model getModel() startEngine() Vehicle travel() Vehicle travel()

32

33  Sometimes you want to do more than add  You want to change a method to do something different  You can write a method in a child class that has the same name as a method in a parent class  The child version of the method will always get called  This is called overriding a method

34  We can define the Mammal class as follows: public class Mammal { public void makeNoise() { System.out.println("Grunt!"); } public class Mammal { public void makeNoise() { System.out.println("Grunt!"); }

35  From there, we can define the Dog, Cat, and Human subclasses, overriding the makeNoise() method appropriately public class Dog extends Mammal { public void makeNoise() { System.out.println("Woof"); } } public class Dog extends Mammal { public void makeNoise() { System.out.println("Woof"); } } public class Cat extends Mammal { public void makeNoise() { System.out.println("Meow"); } } public class Cat extends Mammal { public void makeNoise() { System.out.println("Meow"); } } public class Human extends Mammal { public void makeNoise() { System.out.println("Hello"); } } public class Human extends Mammal { public void makeNoise() { System.out.println("Hello"); } }

36

37

38  Inheritance examples  Lab 13

39  Start working on Project 5


Download ppt "Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class."

Similar presentations


Ads by Google