Upload
horace-tyler
View
237
Download
2
Embed Size (px)
Citation preview
Mirroring an image horizontally Straightforward idea Flip an image around the y-axis Maybe you want to decipher some of
Leonardo’s writings No, the other one
How to perform a horizontal mirror
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
Horizontal mirror in code
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 ) );
Mirroring vertically
Pretty much the same thing Instead of copying each column in
reverse order, copy each row in reverse order
Rotating an image
Another straightforward idea Necessary if you are writing software for
a digital camera (the user might turn the camera for portrait instead of landscape)
How to perform a 90° right rotation
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
Rotation in code
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 ) );
Rotating other amounts
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°
Resizing an image
You’ve seen this many times before Sometimes an image won’t fit nicely
in on a PowerPoint slide
How to perform a resize
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
Resizing example
Doubling an image Each old pixels maps
to four new ones
0 1 2 3
0 A A B B
1 A A B B
2 C C D D
3 C C D D
0 1
0 A B
1 C D
Resizing issues
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
Inheritance
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
Code reuse
The key idea behind inheritance is safe code reuse
You can use old code that was designed to, say, sort lists of Vehicles, and apply that code to lists of Cars
All that you have to do is make sure that Car is a subclass (or child class) of Vehicle
Subclass relationship
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
Subclass example
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[] vehicles = new Vehicle[100];for( int i = 0; i < vehicles.length; i++ )vehicles[i] = new RocketShip(); // cool
Car c = new Vehicle(); // gives error
Creating a subclass
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);
}}
Extending a superclass
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!");
}}
Power of inheritance
There is a part of the Car class that knows all the Vehicle members and methodsCar 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"
A look at a Car
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()
Vehicle
travel()
Adding to existing classes is nice…
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
Mammal example
We can define the Mammal class as follows:public class Mammal {
public void makeNoise(){
System.out.println("Grunt!");}
}
Mammal subclasses
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 Cat extends Mammal {public void makeNoise() { System.out.println("Meow"); }
}
public class Human extends Mammal {public void makeNoise() { System.out.println("Hello"); }
}