<< Chapter < Page Chapter >> Page >

What if it doesn't fit?

If the new Picture object isn't large enough to completely fill the area inside the borders of the World object, it will be placed in the upper-left corner of the World object and the remainder of the World object will be a light gray color.

If the Picture object is too large, an upper-left rectangular portion of the Picture object, sufficient to fill the World object, will be displayed. The remainder of the Picture object will not be visible even if you manually resize the World object to make it larger.

Constructors for the Picture class

Figure 6 shows the javadocs for the constructors for Ericson's Picture class.

Figure 6 - Constructors for the Picture class.

Screen shot of constructor documentation.

Replace the default picture object

The right-hand portion of the last statement in Listing 5 uses the last constructor in Figure 6 to instantiate a new Picture object that encapsulates the image contained in the image file named Prob01.jpg .

(Click here to download a copy of the file named Prob01.jpg.)

What about the size of the Picture object?

I was careful to use an image that was a little wider than and exactly as tall as the dimensions of my World object (300 x 274) . Therefore, the image completely filled the world as shown in Figure 1 .

Pass the reference to a setter method

The reference belonging to the new Picture object was passed to the setPicture method of the World object (a setter method) in Listing 5. This caused the new picture containing the penguin to replace the default all-white picture thatforms the background for the World object. (See Figure 1 .)

A subclass of the SimplePicture class

Ericson's Picture class is a subclass of (extends) the class named SimplePicture . Therefore, an object of the Picture class encapsulates all of the methods defined in the Picture class in addition to all of the methods defined in the SimplePicture class.

A subclass of the Object class

Further, the SimplePicture class is a subclass of (extends) the Object class. Therefore, an object of the Picture class also encapsulates all of the methods defined in the Object class.

The AddMessage method

One of the methods defined in the SimplePicture class and inherited into the Picture class is named AddMessage .

The addMessage method requires three parameters:

  • a string and
  • two coordinate values of type int .

The addMessage method will draw the string as text characters onto the image at the location specified by the two coordinate values.

(The origin of the coordinate system is the upper-left corner of the image with positive horizontal values going to the right and positive verticalvalues going down.)

Add text to the image

The code in Listing 6 uses two levels of indirection to add my name as a message to the picture that forms the background of the world as shown in Figure 1 .

Listing 6 . Add text to the image.
mars.getPicture().addMessage( "Dick Baldwin",10,20);

Get and access the World object

To begin with, Listing 6 goes to the variable named mars to get a reference to the World object. This reference is used to access the World object.

Access the Picture object via a getter method

Questions & Answers

Explain the following terms . (1) Abiotic factors in an ecosystem
Nomai Reply
Abiotic factors are non living components of ecosystem.These include physical and chemical elements like temperature,light,water,soil,air quality and oxygen etc
Qasim
what is biology
daniel Reply
what is diffusion
Emmanuel Reply
passive process of transport of low-molecular weight material according to its concentration gradient
AI-Robot
what is production?
Catherine
Pathogens and diseases
how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
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
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
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 respiration
Deborah
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
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