<< Chapter < Page Chapter >> Page >

The driver class named Prob03

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

Listing 1 - The driver class named Prob03.
public class Prob03{ public static void main(String[]args){ new Prob03Runner().run();}//end main method }//end class Prob03

If you have been studying the earlier modules in this series, no explanation of Listing 1 should be required.

Beginning of the class named Prob03Runner

The class named Prob03Runner begins in Listing 2 .

Listing 2 - Beginning of the class named Prob03Runner.
class Prob03Runner{ public Prob03Runner(){System.out.println("Display your name here."); }//end constructor//----------------------------------------------------// public void run(){Picture pic = new Picture("Prob03.jpg"); // Picture pic = new Picture(300,300);pic.setAllPixelsToAColor(Color.RED); process(pic);//Add your name and display the output picture. pic.addMessage("Display your name here.",10,20);pic.explore(); System.out.println(pic);}//end run

Avoiding use of a blank input image file

This program was originally written using an early version of Ericson's class library that didn't support the second statement in the run method. (That statement was disabled by turning it into a comment in Listing 2 ) As a result, with that library, it was necessary to read an image file containing a blank white image to create a Picture object with a blank white image.

No longer a problem

That problem was rectified with an update to her library and the disabled statement can be substituted for the statement immediately above it. If you dothat, you won't need the input image file.

Except for that, and the call to the method named process , you should already understand all of the code in Listing 2 .

Beginning of the method named process

The method named process begins in Listing 3 .

Listing 3 - Beginning of the method named process.
private void process(Picture pic){ Graphics2D g2 = (Graphics2D)(pic.getGraphics());int width = pic.getWidth(); int height = pic.getHeight();

Java2D graphics

This is basically a module on the use of classes from the Java2D section of the standard class library. (See my lessons 300 through 324 on Java2D graphics here .) Classes from Ericson's library are used mainly to support the display aspects of theprogram.

What are Java2D graphics?

Although the capabilities provided by Java2D graphics are wide and varied, in one way or another, they generally have to do with the creation of images bydrawing.

A graphics context is required

In order to use the methods that will be using, it is necessary to gain access to the graphics context of an object as type Graphics2D . The first statement in Listing 3 calls Ericson's getGraphics method to gain access to the graphics context of a Picture object.

A cast to type Graphics2D is required

However, the getGraphics method returns a reference to the graphics context as type Graphics . In order for us to use it to do what we want to do, we must cast it to type Graphics2D . This gives us access to many more methods that would be the case without the cast.

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