This page is optimized for mobile devices, if you would prefer the desktop version just click here

3.2 Java3002: creating and manipulating turtles and pictures in a  (Page 9/12)

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.

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

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.