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

3.28 Java3028: clipping images  (Page 2/4)

Beginning of the class named Prob04Runner

The class named Prob04Runner begins in Listing 2 .

Listing 2 - Beginning of the class named Prob04Runner.
class Prob04Runner{ public Prob04Runner(){System.out.println("Display your name here."); }//end constructor//----------------------------------------------------// public void run(){Picture pix = new Picture("Prob04a.jpg"); //Rotate and mirror the picture.pix = rotatePicture(pix,35); pix = mirrorUpperQuads(pix);pix = mirrorHoriz(pix); pix.explore();

Nothing new here

There is nothing new in Listing 2 .

After instantiating a new Picture object from the given image file, Listing 2 calls three methods to rotate, mirror, and display the picture, producing the graphic output shown in Figure 2 .

All of the code to accomplish this is essentially the same as code that I have explained in earlier modules.

Clip the picture and display your name

Then Listing 3 calls the clipToEllipse method to clip the picture to an ellipse on a red background as shown in Figure 3 . The clipToEllipse method is new to this module, so I will explain it shortly.

Listing 3 - Clip the picture and display your name.
pix = clipToEllipse(pix); //Add your name and display the output picture.pix.addMessage("Display your name here.",10,20); pix.explore();System.out.println(pix); }//end run

The remaining code in Listing 3 is a repeat of code that I have explained in earlier modules, so I won't have anythingfurther to say about it.

The method named clipToEllipse

The method named clipToEllipse is shown in its entirety in Listing 4 .

Listing 4 - The method named clipToEllipse.
private Picture clipToEllipse(Picture pix){ Picture result =new Picture(pix.getWidth(),pix.getHeight()); result.setAllPixelsToAColor(Color.RED);//Get the graphics2D object Graphics2D g2 = (Graphics2D)(result.getGraphics());//Create an ellipse for clipping Ellipse2D.Double ellipse =new Ellipse2D.Double(28,64,366,275); //Use the ellipse for clippingg2.setClip(ellipse); //Draw the imageg2.drawImage(pix.getImage(),0,0,pix.getWidth(), pix.getHeight(),null); return result;}//end clipToEllipse

Behavior of the clipToEllipse method

The clipToEllipse method receives an incoming parameter that is a reference to an object of the Picture class. Basically, here is what the method does:

  • Instantiate a Picture object with an all white background that is the same size as the incoming Picture object.
  • Call Ericson's setAllPixelsToAColor method to convert the white background into a red background.
  • Call Ericson's getGraphics method to get the Graphics object encapsulated in the red Picture object.
  • Cast the Graphics object's reference to type Graphics2D .
  • Construct a new Ellipse2D.Double object with the position, width, and height specified by the constructor parameters.
  • Call Sun's setClip method to set the clipping area on the red Picture object to match the position and shape of the ellipse.
  • Call Ericson's getImage method to get the Image object encapsulated in the incoming Picture object.
  • Call Sun's drawImage method to draw that portion of the incoming picture that fits inside the ellipse on the red Picture object.
<< 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.