<< 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 .

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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