lec 08 after class
TRANSCRIPT
-
8/12/2019 Lec 08 After Class
1/24
-
8/12/2019 Lec 08 After Class
2/24
1. If this is the header to a function defined inPicture.java:
public Picture scaleUp(int numTimes){....
}
What should the method call look like if I call it on aPicture object named pictObjand I wanted to usethe Picture object that it returns?
READING QUIZ
NO TALKING
CORRECTNESS MATTERS
A. Picture pictReturned = scaleUp(2);B. Picture pictReturned = pictObj.scaleUp(2);
C. String pictReturned = pictObj.scaleUp(2);
D. Picture = pictObj.scaleUp(2);
-
8/12/2019 Lec 08 After Class
3/24
2.To scale a picture smaller by 2 (to make a copy of the picture that is half thesize of the source) what should go in the blanks
public void copySmaller(){
Picture pictObj = new Picture(FileChooser.pickAFile());
Pixel sourcePixel = null;Pixel targetPixel = null;
//loop through the columnsfor(int sourceX=0, targetX=0;
sourceX < pictObj.getWidth();____1_____, targetX++)
{//loop through the rows
for(int sourceY=0; targetY=0;soruceY < pictObj.getHeight();____2____, targetY++)
{sourcePixel = pictObj.getPixel(sourceX, sourceY);targetPixel = this.getPixel(targetX,targetY);
targetPixel.setColor(sourcePixel.getColor());}}
}A. 1. sourceX++ 2. sourceY+=2
B. 1. sourceX+=2 2. sourceY+=2
C. 1. sourceX+=2 2. sourceY++
D. 1. sourceX++ 2. sourceY++READING QUIZNO TALKINGCORRECTNESS MATTERS
-
8/12/2019 Lec 08 After Class
4/24
3. How do I blend two picture objectswith 50% of one picture and 50% ofanother picture?
A. By multiplying their colors together and by 0.5
B. By adding them togetherC. By multiplying their colors by 0.5 and then adding
them
READING QUIZ
NO TALKING
CORRECTNESS MATTERS
-
8/12/2019 Lec 08 After Class
5/24
4. Imagine that you have a method copyPictureTo, whose header is below, defined inPicture.java. This method copies the source Picture (sourcePic) and places its top leftcorner at (xStart, yStart) of the Picture that called the copyPictureTomethod.
public void copyPictureTo(Picture sourcePic, int xStart, int yStart)
Assume you have already created two Picture objects: sourcePic and pictObj, andyou make the following call:
pictObj.copyPictureTo(sourcePict, pictObj.getWidth()/2, 0);
Which position below best represents where the sourcePicwill be located in picObjafter the call? Assume that sourcePicis much smaller than pictObj.
READING QUIZNO TALKINGCORRECTNESS MATTERS
picObj
A B
C D
Arrows indicate
half the target
picture width and height
-
8/12/2019 Lec 08 After Class
6/24
Mirroring Even Width versus Odd Width
int mirrorPt = getWidth() / 2;
...
for (int x = 0; x < mirrorPt; x++)
-
8/12/2019 Lec 08 After Class
7/24
Mirroring Odd-width Pictures
What happens when this code attempts to mirror a Picture around the vertical
axis when the Pictures width is odd (e.g. 101)?
1) Solo: (30 sec)
2) Discuss: (2min)
3) Group: (30 sec)
int mirrorPt = getWidth()/2;
Pixel leftP, rightP;
for (int y = 0; y < getHeight(); y++)
{for (int x = 0; x < mirrorPt; x++)
{
leftP = getPixel(x,y);
rightP = getPixel(getWidth()-1-x,y);
rightP.setColor(leftP.getColor());
}}
A. It will work fine
B. It will run, but it wont
mirror correctly
C. I wont run, there will
be an index out ofbounds exception
D. It wont even compile
if getWidth() is odd
-
8/12/2019 Lec 08 After Class
8/24
Mirroring Odd-width Pictures
What happens when this code attempts to mirror a Picture around the vertical
axis when the Pictures width is odd (e.g. 101)?
1) Solo: (30 sec)
2) Discuss: (2min)
3) Group: (30 sec)
int mirrorPt = getWidth()/2;
Pixel leftP, rightP;
for (int y = 0; y < getHeight(); y++)
{for (int x = 0; x < mirrorPt; x++)
{
leftP = getPixel(x,y);
rightP = getPixel(getWidth()-1-x,y);
rightP.setColor(leftP.getColor());
}}
A. It will work fine
B. It will run, but it wont
mirror correctly
C. I wont run, there will
be an index out ofbounds exception
D. It wont even compile
if getWidth() is odd
-
8/12/2019 Lec 08 After Class
9/24
What are the first (x,y) coords fortopP and bottomP
to mirror around horizontalaxis?
topP bottomP
A. (0,0) (0,3)
(0,1) (0,2)
(1,0) (1,3)
B. (0,0) (0,3)
(1,0) (1,3)
(2,0) (2,3)
1) Solo: (30 sec)
2) Discuss (1 min)
3) Group
C. either A or B will work
D. none of the above
-
8/12/2019 Lec 08 After Class
10/24
Challenge: Complete the code that mirrors inthe order specified by answer B
topP bottomPB. (0,0) (0,3)
(1,0) (1,3)
(2,0) (2,3)
int height = getHeight();
int width = getWidth();int mid = height/2;
Pixel topP, botP;
for ( ){
for( ) {topP = getPixel( );
botP = getPixel( );
botP.setColor(topP.getColor());
}
}
-
8/12/2019 Lec 08 After Class
11/24
-
8/12/2019 Lec 08 After Class
12/24
Mirroring around horizontal axis
topP botPA. (0,0) (0,3)
(0,1) (0,2)
(1,0) (1,3)
int height = getHeight();
int width = getWidth();
int mid = height/2;
Pixel topP, botP;
for(int y=0; y
-
8/12/2019 Lec 08 After Class
13/24
Challenge: What doesthis code do?
Hint: trace some of the getPixel index values.
int magic = getWidth()/2;
Pixel foo, bar;
for(int y = 0; y < getHeight(); y++)
{
int countingDown = getWidth()-1;
for(int x = 0; x < magic; x++)
{
foo = getPixel(x,y);
bar = getPixel(countingDown,y);
bar.setColor(foo.getColor());
countingDown--;}
}
A. Copies top half intobottom half notmirrored.
B. Copies left half intoright half notmirrored.
C. Mirrors aroundvertical axis, left intoright
D. Mirrors aroundhorizontal axis, topinto bottom
E. Some other bizarre
transformation
1) Solo: (1 min)
2) Discuss: (2 min)
3) Group: (30 sec)
-
8/12/2019 Lec 08 After Class
14/24
Challenge: What doesthis code do?
Hint: trace some of the getPixel index values.
int magic = getWidth()/2;
Pixel foo, bar;
for(int y = 0; y < getHeight(); y++)
{
int countingDown = getWidth()-1;
for(int x = 0; x < magic; x++)
{
foo = getPixel(x,y);
bar = getPixel(countingDown,y);
bar.setColor(foo.getColor());
countingDown--;}
}
1) Solo: (1 min)
2) Discuss: (2 min)
3) Group: (30 sec)
x
y
countingDown
foo bar
-
8/12/2019 Lec 08 After Class
15/24
By what variable name do we refer tocollage inside makeC in Picture.java?
A. collage
B. callingObjec
C. Object
D. Picture
E. this
public class Lab4
{public static void main(String[] args)
{
Picture collage = new Picture("blank.jpg");
Picture p = new Picture("bird1.jpg");
Picture q = new Picture("bird2.jpg");
collage.makeC(p,q);}
}
public class Picture
{
public void makeC(Picture p1, Picture p2){
Pixel[] targetPixels = _______.getPixels();
// more code
}
}
-
8/12/2019 Lec 08 After Class
16/24
Match the scenarioto the constructor call
(well vote for each scenario)
Scenario
1) Create a picture from a
specific file
2) Create a picture that is a
copy of another picture
3) Create a picture of a given
width and height4) Create a picture of the
same width and height as
another picture
Call
A. Picture p =
new Picture();
B. Picture p =
new Picture("filename.jpg");
C. Picture p =
new Picture(other);
D. Picture p =
new Picture(aNum,bNum);
1) S l (1 i )
-
8/12/2019 Lec 08 After Class
17/24
What doesthis code do?
Makes red box of
width height
1) Solo: (1 min)
2) Discuss: (2 min)
3) Group: (30 sec)
Pixel foo;for(int y = 40; y < 50; y++)
{
for(int x = 1 ; x < 5; x++)
{
foo = getPixel(x,y);
foo.setColor(Color.RED);}
}
1) S l (1 i )
-
8/12/2019 Lec 08 After Class
18/24
What doesthis code do?
Makes red box of
width height
1) Solo: (1 min)
2) Discuss: (2 min)
3) Group: (30 sec)
Pixel foo;for(int y = 40; y < 50; y++)
{
for(int x = 1 ; x
-
8/12/2019 Lec 08 After Class
19/24
What are correct loopsto make a black box of
width x and height y?public void foo(int x, int y) {
Pixel foo;
{
{
foo = getPixel(w,h);
foo.setColor(Color.BLACK);
}
}
}
1) Solo: (1 min)
2) Discuss: (2 min)
3) Group: (30 sec)
A) for (int w = 0; w
-
8/12/2019 Lec 08 After Class
20/24
From the book: Cropping A Picture(page 147-148)well change a bit
Example of:
Working with both the calling object and a parameter
object in a method
Extra information is passed to methods through
parameters. The calling object is something like an extraparameter, named this
Doing something to a subset of the possible pixels in a
picture
1) Solo: (1 min)
-
8/12/2019 Lec 08 After Class
21/24
What part of Katie are we copying?(slightmod from the book)
public void copyKatiesXXX(Picture sourcePic)
{
Pixel sPixel = null, tPixel = null;for (int sX = 40, tX = 100; sX < 110; sX++, tX++)
{
for (int sY = 350, tY = 100; sY < 400; sy++, tY++)
{
sPixel = sourcePic.getPixel(sX,sY);
tPixel = this.getPixel(tX,tY);tPixel.setColor(sPixel.getColor();
}
}
}
1) Solo: (1 min)
2) Discuss: (2 min)
3) Group: (30 sec)
A. Feet
B. Part of dress
C. Hands
D. Part of Couch
E. Face
-
8/12/2019 Lec 08 After Class
22/24
Parameters: getting information in tomethods
Its nice to have code that is user controllable
We have been hard-coding constants (40, 3, 100,
for example) a lot, but we can write more flexible
code using PARAMETERS This lets us write code to do things like cropping
and pasting into a blank canvas, but letting the
user specify what part of the source picture to crop,and where to place it in the canvas.
-
8/12/2019 Lec 08 After Class
23/24
Underline the values you would change intoparameters and write a new method header
public void copyKatiesXXX(
){
Pixel sPixel, tPixel = null;
for (int sX = 40, tX = 100; sX < 110; sX++, tX++)
{
for (int sY = 350, tY = 100; sY < 400; sy++, tY++)
{sPixel = sourcePic.getPixel(sX,sY);
tPixel = this.getPixel(tX,tY);
tPixel.setColor(sPixel.getColor();
}
}
}
-
8/12/2019 Lec 08 After Class
24/24
TODO
Study for In-term Exam 2
Start PSA4: Collage and Picture Flip
Dont forget your PSA3 interview!