<< Chapter < Page Chapter >> Page >

General background information

This program copies a rectangular portion of a picture of a butterfly into a specific location in a picture of a beach.

The program also crops the butterfly picture to the same size as the portion that was copied into the beach picture and flips the cropped version to causethe butterfly to face left instead of facing right.

Then it copies the cropped and flipped image to a location two pixels to the right of the original copy of the butterfly in the beach image.

The two resulting images of the butterfly within the beach image are separated by two pixels, face one another, and are centered in the picture ofthe beach as shown in Figure 3 .

Major evaluation areas

In order to successfully write this program, the student must, as a minimum be able to:

  • Work directly with individual pixels and keep track of coordinate values.
  • Copy a portion of one picture into a specific location in another picture.
  • Crop and flip a picture.

Discussion and sample code

Will discuss in fragments

I will discuss this program in fragments. A complete listing of the program is provided in Listing 10 near the end of the module.

The driver class named Prob02

The driver class containing the main method is shown in Listing 1 .

Listing 1 . The driver class named Prob02.
import java.awt.Color; public class Prob02{public static void main(String[] args){Picture[] pictures = new Prob02Runner().run();System.out.println(pictures[0]);System.out.println(pictures[1]);System.out.println(pictures[2]);}//end main method }//end class Prob02

A reference to an array object

The call to the run method in Listing 1 may be new to you. This call expects to receive a reference to an array objectof type Picture[] as a return value.

Save return value in variable named pictures

The return value from the run method is stored in the local reference variable named pictures .

Extract and print references to Picture objects

Then the reference variable is used to extract references to the individual Picture objects encapsulated in the array. Those references are passed to the println method causing the last three lines of text shown in Figure 4 to be displayed on the command line screen.

Beginning of the Prob02Runner class

The class named Prob02Runner begins in Listing 2 , which shows the constructor for the class.

Listing 2 . Beginning of the Prob02Runner class.
class Prob02Runner{ public Prob02Runner(){//constructorSystem.out.println("Display your name here."); }//end constructor

The constructor simply causes the student's name to be displayed on the command line screen, producing the first line of output text shown in Figure 4 .

Beginning of the run method

The run method, that was called in Listing 1 begins in Listing 3 .

Listing 3 . Beginning of the run method.
public Picture[] run(){Picture picA = new Picture("Prob02a.jpg"); picA.explore();Picture picB = new Picture("Prob02b.jpg");picB.addMessage("Display your name here.",10,20); picB.explore();Picture picC = cropAndFlip(picA,4,5,80,105);

Listing 3 instantiates two Picture objects from image files and displays them by calling the explore method on each Picture object. In addition, the student's name is added near the upper-left corner of the beach image. This coderesults in the images shown in Figure 1 and Figure 2 .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask