INEW 2338, Advanced Java Study Guide:  Java 2D

Published November 8, 2004
By Richard G. Baldwin

File: Inew2338Sg006.htm


Welcome

The purpose of this series of tutorial lessons is to help you learn the essential topics covered in INEW 2338, Advanced Java.

These lessons provide questions, answers, and explanations designed to help you to understand the essential Java features covered by the Advanced Java course.

The textbook for this course is Advanced Java Internet Applications, Second Edition by Art Gittleman, ISBN 1-57676-096-0.  This study guide is for Chapter 6 in the textbook.

In addition to the textbook, I recommend that you study my extensive collection of online Java tutorial lessons.  Those tutorial lessons are published at http://www.dickbaldwin.com.

For this particular study guide, you should study lessons 300 through 324 plus lesson 124 at the URL given above.

While there, you may also find it useful to study lessons 160 through 182.  These lessons provide a great deal of information about graphics in Java that you should understand before embarking on the Java 2D API.

You may also find it useful to open another copy of this document in a separate browser window.  That will make it possible for you to see the code and the associated image at the same time.


Questions

1.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Inew2338_100 extends JFrame{
  public static void main(String[] args){
    Inew2338_100 guiObj = new Inew2338_100();
  }//end main

  int res;//screen resolution
  final int ds = 72;//default scale,72 units/inch

  Inew2338_100(){//constructor
    //Get screen resolution
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();

    setSize(4*res,4*res);
    setTitle("Copyright 2004, R.G.Baldwin");

    MyPanel myPanel = new MyPanel();
    getContentPane().add(myPanel);

    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //-------------------------------------------//

  //Inner class
  class MyPanel extends JPanel{
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      //Downcast to a Graphics2D object
      Graphics2D g2 = (Graphics2D)g;

      //Draw five concentric squares
      double delta = 0.1;
      for(int cnt = 0; cnt < 5; cnt++){
        g2.draw(new Rectangle2D.Double(
                          (1.5+cnt*delta)*ds,
                          (1.5+cnt*delta)*ds,
                          (1.0-cnt*2*delta)*ds,
                          (1.0-cnt*2*delta)*ds));
      }//end for loop

      //Superimpose some text on the squares
      g2.drawString("Exit ->",2.0f*ds,2.0f*ds);
    }//end overridden paintComponent()
  }//end MyPanel

}//end class Inew2338_100

Answer and Explanation

2.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Inew2338_102 extends JFrame{
  public static void main(String[] args){
    Inew2338_102 guiObj = new Inew2338_102();
  }//end main

  int res;//screen resolution
  final int ds = 72;//default scale,72 units/inch

  Inew2338_102(){//constructor
    //Get screen resolution
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();

    setSize(4*res,4*res);
    setTitle("Copyright 2004, R.G.Baldwin");

    MyPanel myPanel = new MyPanel();
    getContentPane().add(myPanel);

    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //-------------------------------------------//

  //Inner class
  class MyPanel extends JPanel{
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      //Downcast to a Graphics2D object
      Graphics2D g2 = (Graphics2D)g;

      //Scale Transform")
      g2.scale((double)res/72,(double)res/72);

      //Translate Transform
      g2.translate(0.25*ds,0.25*ds);

      //Add Shear Transform")
      g2.shear(0.05,0.1);

      //Rotate Transform
      //11.25 degrees about center
      g2.rotate(Math.PI/16,2.0*ds, 2.0*ds);

      //Draw five concentric squares and apply
      // the transform that results from the
      // above transform updates.
      double delta = 0.1;
      for(int cnt = 0; cnt < 5; cnt++){
        g2.draw(new Rectangle2D.Double(
                          (1.5+cnt*delta)*ds,
                          (1.5+cnt*delta)*ds,
                          (1.0-cnt*2*delta)*ds,
                          (1.0-cnt*2*delta)*ds));
      }//end for loop

      //Superimpose some text on the squares
      g2.drawString("Exit ->",2.0f*ds,2.0f*ds);
    }//end overridden paintComponent()
  }//end MyPanel

}//end class Inew2338_102

Answer and Explanation

3.  True or false?  The code in the following box produces the graphics output shown in the second box below.  The two green buttons have a slight 3D appearance due to the shadows on the right side and the bottom.  When you press either of the green buttons with the mouse pointer, the shadow on the bottom and the right disappears.  A similar shadow appears on the left side and the top giving the appearance that the button has been pushed into the surface.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//=============================================//
//This class is used to create a fake button
// object.
class FakeButton extends Canvas{
  boolean pushed = false;//button pushed or not
  int width; //button width in pixels
  int height;//button height in pixels
  String caption;//caption for button

  public FakeButton(//constructor
            int width,int height,String caption){
    this.width = width;
    this.height = height;
    this.caption = caption;
    this.setBackground(Color.GREEN);
    this.setSize(this.width,this.height);
  }//end constructor

  public void paint(Graphics g){
    //Determine offset and draw rectangle on
    // Canvas object
    if(pushed) g.drawRect(1,1,width,height);
    else g.drawRect(-1,-1,width,height);

    //Draw the caption centered horizontally and
    // slightly above center vertically.
    int fontHeight =
                  g.getFontMetrics().getHeight();
    int stringWidth =
         g.getFontMetrics().stringWidth(caption);
    g.drawString(caption,(width-stringWidth)/2,
                      (height/2)+(fontHeight/4));
  }//end paint()
}//end class FakeButton
//=============================================//

class Inew2338_104 extends JFrame{
  public static void main(String[] args){
    new Inew2338_104();
  }//end main

  public Inew2338_104(){//constructor
    getContentPane().setLayout(new FlowLayout());
    setTitle("Copyright 2004, R.G.Baldwin");
    setSize(300,150);

    FakeButton firstFakeButton =
                   new FakeButton(40,20,"Small");
    FakeButton secondFakeButton =
                   new FakeButton(80,40,"Large");
    JButton myRealButton =
                          new JButton("JButton");

    getContentPane().add(firstFakeButton);
    getContentPane().add(myRealButton);
    getContentPane().add(secondFakeButton);

    this.setVisible(true);

    MouseListenerClass myMouseListener =
                        new MouseListenerClass();
    firstFakeButton.addMouseListener(
                                myMouseListener);
    secondFakeButton.addMouseListener(
                                myMouseListener);

    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
  }//end constructor
}//end class Inew2338_104
//=============================================//

//This class is used to instantiate a listener
// object that listens for mouse events on the
// fake buttons.
class MouseListenerClass extends MouseAdapter{
  public void mousePressed(MouseEvent e){
    ((FakeButton)e.getComponent()).pushed = true;
    e.getComponent().repaint();
  }//end mousePressed()

  public void mouseReleased(MouseEvent e){
    ((FakeButton)e.getComponent()).pushed =
                                           false;
    e.getComponent().repaint();
  }//end mouseReleased()
}//end class MouseListenerClass

Answer and Explanation

4.  True or false?  The code in the following box produces the graphics output shown in the second box below and produces the screen output shown in the third box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Inew2338_106 extends JFrame{
  int res;//store screen resolution here
  final int ds = 72;
  final int hSize = 4;
  final int vSize = 4;

  public static void main(String[] args){
    new Inew2338_106();
  }//end main

  Inew2338_106(){//constructor
    //Get screen resolution
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    setSize(hSize*res,vSize*res);
    setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());

    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);

    this.setVisible(true);
  }//end constructor

  //===========================================//
  //Inner class
  class GUI extends JPanel{

    public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;

      g2.scale((double)res/72,(double)res/72);

      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      //Draw x-axis
      g2.draw(new Line2D.Double(-1.5*ds,0.0,
                                    1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(0.0,-1.5*ds,
                                    0.0,1.5*ds));

      //Define a circle centered about the
      // origin.
      Ellipse2D.Double theCircle =
           new Ellipse2D.Double(-0.5*ds, -0.5*ds,
                                 1.0*ds, 1.0*ds);

      //Draw theCircle in the JFrame in the
      // default drawing color: black
      g2.draw(theCircle);

      //Get bounding box of the Circle
      Rectangle2D theBoundingBox =
                         theCircle.getBounds2D();
      g2.setColor(Color.RED);
      //Draw the bounding box in the new color
      g2.draw(theBoundingBox);

      //Create boxes to test for contains and
      // intersects
      Rectangle2D.Double theInsideBox =
                          new Rectangle2D.Double(
                              -0.25*ds, -0.25*ds,
                                 0.5*ds, 0.5*ds);
      Rectangle2D.Double theIntersectingBox =
                          new Rectangle2D.Double(
                                  0.3*ds, 0.3*ds,
                                 0.5*ds, 0.5*ds);
      Rectangle2D.Double theOutsideBox =
                          new Rectangle2D.Double(
                              -1.25*ds, -1.25*ds,
                                 0.5*ds, 0.5*ds);

      //Draw the test boxes in different colors
      g2.setColor(Color.GREEN);
      g2.draw(theInsideBox);
      g2.setColor(Color.BLUE);
      g2.draw(theIntersectingBox);
      g2.setColor(Color.MAGENTA);
      g2.draw(theOutsideBox);

      //Now perform the tests and display the
      // results on the command-line screen.
      System.out.println("The Circle:");
      System.out.println(
           "Contains the red BoundingBox: "
           + theCircle.contains(theBoundingBox));
      System.out.println(
             "Contains the green InsideBox: "
             + theCircle.contains(theInsideBox));
      System.out.println(
            "Contains the blue IntersectingBox: "
                            + theCircle.contains(
                            theIntersectingBox));
      System.out.println(
            "Contains the magenta OutsideBox: "
            + theCircle.contains(theOutsideBox));
      System.out.println("The Circle:");
      System.out.println(
         "Intersects the red BoundingBox: "
         + theCircle.intersects(theBoundingBox));
      System.out.println(
           "Intersects the green InsideBox: "
           + theCircle.intersects(theInsideBox));
      System.out.println(
          "Intersects the blue IntersectingBox: "
                          + theCircle.intersects(
                            theIntersectingBox));
      System.out.println(
          "Intersects the magenta OutsideBox: "
          + theCircle.intersects(theOutsideBox));

    }//end overridden paintComponent() method
  }//end inner class GUI
}//end Inew2338_106

The Circle:
Contains the red BoundingBox: false
Contains the green InsideBox: true
Contains the blue IntersectingBox: false
Contains the magenta OutsideBox: false
The Circle:
Intersects the red BoundingBox: true
Intersects the green InsideBox: false
Intersects the blue IntersectingBox: true
Intersects the magenta OutsideBox: false

Answer and Explanation

5.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Inew2338_108 extends JFrame{
  int res;//store screen resolution here
  final int ds = 72;//default scale
  final int hSize = 4;//horizonal size
  final int vSize = 4;//vertical size

  public static void main(String[] args){
    new Inew2338_108();
  }//end main

  Inew2338_108(){//constructor
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    setSize(hSize*res,vSize*res);
    setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //===========================================//

  //Inner class
  class GUI extends JPanel{

    public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;

      g2.scale((double)res/72,(double)res/72);

      //Translate origin to center of frame
      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      //Use the GeneralPath class to instantiate
      // a diamond object whose vertices are in
      // the N, S, E, and W positions, centered
      // about the origin.
      GeneralPath thePath = new GeneralPath();
      thePath.moveTo(0.5f*ds,0.0f*ds);
      thePath.lineTo(0.0f*ds,0.5f*ds);
      thePath.lineTo(-0.5f*ds,0.0f*ds);
      thePath.lineTo(0.0f*ds,-0.5f*ds);
      thePath.closePath();

      //Now draw the diamond on the screen
      g2.draw(thePath);

    }//end overridden paintComponent()
  }//end inner class GUI
}//end class Inew2338_108

Answer and Explanation

6.  True or false?  The code in the following box produces the graphics output shown in the second box below and produces the screen output shown in the third box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Inew2338_110a extends JFrame{
  int res;//store screen resolution here
  final int ds = 72;
  final int hSize = 4;
  final int vSize = 4;

  public static void main(String[] args){
    new Inew2338_110a();
  }//end main

  Inew2338_110a(){
    //Get screen resolution
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    setSize(hSize*res,vSize*res);
    setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());

    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }//constructor
  //===========================================//

  //Inner class
  class GUI extends JPanel{

    public void paintComponent(Graphics g){
      super.paintComponent(g);

      Graphics2D g2 = (Graphics2D)g;//downcast

      g2.scale((double)res/72,(double)res/72);

      //Translate the origin to the center
      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      //Use the GeneralPath class to instantiate
      // a diamond object whose vertices are the
      // N, S, E, and W positions, centered
      // about the origin.
      GeneralPath thePath = new GeneralPath();
      thePath.moveTo(0.5f*ds,0.0f*ds);
      thePath.lineTo(0.0f*ds,0.5f*ds);
      thePath.lineTo(-0.5f*ds,0.0f*ds);
      thePath.lineTo(0.0f*ds,-0.5f*ds);
      thePath.closePath();

      //Now draw the diamond on the screen in
      // black
      g2.draw(thePath);

      //Get a PathIterator object on the diamond
      PathIterator theIterator =
                   thePath.getPathIterator(null);
      //Use this array to store segment
      // coordinate data
      float[] theData = new float[6];
      int theType;//store type of segment here

      //Get a new GeneralPath object.  Populate
      // it based on coordinates and segment
      // types extracted from the original
      // GeneralPath object but offset the
      // coordinate values in both X and Y.
      GeneralPath newPath = new GeneralPath();

      //Iterate on the original GeneralPath
      // object and get information to populate
      // the new GeneralPath object.
      while(!theIterator.isDone()){
        //While not done, get type of segment and
        // coordinate values for the current
        // segment
        theType = theIterator.currentSegment(
                                        theData);

        //Process the current segment to populate
        // a new segment of the new GeneralPath
        // object.
        switch(theType){
          case PathIterator.SEG_MOVETO :
            System.out.println("SEG_MOVETO");
            newPath.moveTo(theData[0]+1.0f*ds,
                             theData[1]+1.0f*ds);
            break;
          case PathIterator.SEG_LINETO :
            System.out.println("SEG_LINETO");
            newPath.lineTo(theData[0]+1.0f*ds,
                             theData[1]+1.0f*ds);
            break;
          case PathIterator.SEG_QUADTO :
            System.out.println(
                     "SEG_QUADTO not supported");
            break;
          case PathIterator.SEG_CUBICTO :
            System.out.println(
                    "SEG_CUBICTO not supported");
            break;
          case PathIterator.SEG_CLOSE :
            System.out.println("SEG_CLOSE");
            newPath.closePath();
            break;
        }//end switch

        //Get the next segment
        theIterator.next();
      }//end while loop

      g2.setColor(Color.red);
      g2.draw(newPath);

    }//end overridden paintComponent()

  }//end inner class GUI
}//end controlling class Inew2338_110a

SEG_MOVETO
SEG_LINETO
SEG_LINETO
SEG_LINETO
SEG_CLOSE

Answer and Explanation

7.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;

class Inew2338_112a extends JFrame{
  int res;
  final int ds = 72;
  final int hSize = 4;//horizonal size
  final int vSize = 4;//vertical size

  public static void main(String[] args){
    new Inew2338_112a();
  }//end main

  Inew2338_112a(){
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    this.setSize(hSize*res,vSize*res);
    this.setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //===========================================//

  //Inner class
  class GUI extends JPanel{

    public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;//downcast

      g2.scale((double)res/72,(double)res/72);

      //Translate origin to center
      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      Ellipse2D.Double circle1 =
                new Ellipse2D.Double(
                  -2.0*ds,-2.0*ds,2.0*ds,2.0*ds);
      g2.setPaint(new Color(255,0,0));//red
      g2.fill(circle1);
      g2.draw(circle1);

      Ellipse2D.Double circle2 =
                new Ellipse2D.Double(
                   0.0*ds,-2.0*ds,2.0*ds,2.0*ds);
      g2.setPaint(new Color(0,255,0));//green
      g2.fill(circle2);
      g2.draw(circle2);

      Ellipse2D.Double circle3 =
                new Ellipse2D.Double(
                   -2.0*ds,0.0*ds,2.0*ds,2.0*ds);
      g2.setPaint(new Color(0,0,255));//blue
      g2.fill(circle3);
      g2.draw(circle3);

      Ellipse2D.Double circle4 =
                new Ellipse2D.Double(
                    0.0*ds,0.0*ds,2.0*ds,2.0*ds);
      g2.setPaint(new Color(255,255,0));//yellow
      g2.fill(circle4);
      g2.draw(circle4);

    }//end overridden paintComponent()

  }//end inner class GUI
}//end controlling class Inew2338_112a

Answer and Explanation

8.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;


class Inew2338_114 extends JFrame{
  int res;
  final int ds = 72;
  final int hSize = 4;//horizonal size
  final int vSize = 4;//vertical size

  public static void main(String[] args){
    new Inew2338_114();
  }//end main

  Inew2338_114(){//constructor
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    this.setSize(hSize*res,vSize*res);
    this.setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //===========================================//

  //Inner class
  class GUI extends JPanel{

    public void paintComponent(Graphics g){
      super.paintComponent(g);

      Graphics2D g2 = (Graphics2D)g;//downcase

      g2.scale((double)res/72,(double)res/72);

      //Translate origin to center
      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      Ellipse2D.Double circle1 =
                new Ellipse2D.Double(
                  -2.0*ds,-2.0*ds,2.0*ds,2.0*ds);
      g2.setPaint(new Color(255,0,0));//red
      g2.fill(circle1);
      g2.draw(circle1);

      Ellipse2D.Double circle2 =
                 new Ellipse2D.Double(
                   0.0*ds,-2.0*ds,2.0*ds,2.0*ds);
      g2.setPaint(new GradientPaint(0.5f*ds,
                                    -1.0f*ds,
                                    Color.RED,
                                    1.5f*ds,
                                    -1.0f*ds,
                                    Color.ORANGE,
                                    false));
      g2.fill(circle2);
      g2.draw(circle2);

      Ellipse2D.Double circle3 =
                 new Ellipse2D.Double(
                   -2.0*ds,0.0*ds,2.0*ds,2.0*ds);
      g2.setPaint(new GradientPaint(-1.15f*ds,
                                    1.0f*ds,
                                    Color.RED,
                                    -0.85f*ds,
                                    1.0f*ds,
                                    Color.ORANGE,
                                    true));
      g2.fill(circle3);
      g2.draw(circle3);

      Ellipse2D.Double circle4 =
                  new Ellipse2D.Double(
                    0.0*ds,0.0*ds,2.0*ds,2.0*ds);
      g2.setPaint(new GradientPaint(0.0f*ds,
                                    0.0f*ds,
                                    Color.RED,
                                    0.25f*ds,
                                    0.25f*ds,
                                    Color.ORANGE,
                                    true));
      g2.fill(circle4);
      g2.draw(circle4);

    }//end overridden paintComponent()

  }//end inner class GUI
}//end controlling class Inew2338_114

Answer and Explanation

9.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

class Inew2338_116a extends JFrame{
  int res;
  final int ds = 72;
  final int hSize = 4;//horizonal size
  final int vSize = 4;//vertical size

  public static void main(String[] args){
    new Inew2338_116a();
  }//end main

  Inew2338_116a(){
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    this.setSize(hSize*res,vSize*res);

    this.setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);

    this.setVisible(true);
  }//constructor
//=============================================//

  //Inner class
  class GUI extends JPanel{

    public void paintComponent(Graphics g){
      super.paintComponent(g);

      Graphics2D g2 = (Graphics2D)g;//downcast

      g2.scale((double)res/72,(double)res/72);

      //Translate origin to center
      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      Ellipse2D.Double theMainCircle =
         new Ellipse2D.Double(
                  -1.0*ds,-1.0*ds,2.0*ds,2.0*ds);

      double tileSize = 0.25;

      Rectangle2D.Double anchor =
                       new Rectangle2D.Double(
                          0,0,(int)(tileSize*ds),
                             (int)(tileSize*ds));
      TexturePaint theTexturePaintObj =
                    new TexturePaint(
                      getBufferedImage(),anchor);

      g2.setPaint(theTexturePaintObj);
      g2.fill(theMainCircle);
      g2.draw(theMainCircle);

    }//end overridden paintComponent()
    //-----------------------------------------//

    //Method to create and return a BufferedImage
    // object
    BufferedImage getBufferedImage(){
      double imageSize = 10.0;
      BufferedImage theBufferedImage =
                      (BufferedImage)createImage(
                             (int)(imageSize*ds),
                            (int)(imageSize*ds));

      Graphics2D g2dImage =
               theBufferedImage.createGraphics();

      Rectangle2D.Double theRectangle =
                  new Rectangle2D.Double(0.0,0.0,
                                   imageSize*ds,
                                   imageSize*ds);
      g2dImage.setPaint(new Color(0,0,0));//black
      g2dImage.fill(theRectangle);
      g2dImage.draw(theRectangle);

      Ellipse2D.Double circleOntheBufferedImage =
                    new Ellipse2D.Double(0.0,0.0,
                                   imageSize*ds,
                                   imageSize*ds);
      g2dImage.setPaint(
                  new Color(255,255,255));//white
      g2dImage.fill(circleOntheBufferedImage);
      g2dImage.draw(circleOntheBufferedImage);

      return theBufferedImage;

    }//end getBuffered Image

  }//end class GUI

}//end controlling class Inew2338_116a

Answer and Explanation

10.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

class Inew2338_118 extends JFrame{
  int res;
  final int ds = 72;
  final int hSize = 4;//horizonal size
  final int vSize = 4;//vertical size

  public static void main(String[] args){
    new Inew2338_118();
  }//end main

  Inew2338_118(){//constructor
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    this.setSize(hSize*res,vSize*res);
    this.setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //===========================================//

  //Inner class
  class GUI extends JPanel{

    public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;//downcast

      g2.scale((double)res/72,(double)res/72);

      //Translate origin to center
      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      //Display all three end cap types in
      // upper-left quadrant

      //Display red CAP_BUTT
      Stroke stroke = new BasicStroke(
                         0.2f*ds,
                         BasicStroke.CAP_BUTT,
                         BasicStroke.JOIN_BEVEL);
      g2.setStroke(stroke);
      g2.setPaint(Color.red);
      g2.draw(new Line2D.Double(
               -1.5*ds,-1.5*ds,-0.5*ds,-1.5*ds));

      //Display green CAP_ROUND
      stroke = new BasicStroke(
                         0.2f*ds,
                         BasicStroke.CAP_ROUND,
                         BasicStroke.JOIN_BEVEL);
      g2.setStroke(stroke);
      g2.setPaint(Color.green);
      g2.draw(new Line2D.Double(
               -1.5*ds,-1.0*ds,-0.5*ds,-1.0*ds));

      //Display blue CAP_SQUARE
      stroke = new BasicStroke(
                         0.2f*ds,
                         BasicStroke.CAP_SQUARE,
                         BasicStroke.JOIN_BEVEL);
      g2.setStroke(stroke);
      g2.setPaint(Color.blue);
      g2.draw(new Line2D.Double(
               -1.5*ds,-0.5*ds,-0.5*ds,-0.5*ds));

      //Display two lines that connect, but are
      // not segments of a Shape in the lower
      // left quadrant.  Illustrates the problems
      // of creating geometric figures with
      // connecting lines that have width.

      //This illustrates the problem with
      // CAP_SQUARE -- red
      stroke = new BasicStroke(
                         0.2f*ds,
                         BasicStroke.CAP_SQUARE,
                         BasicStroke.JOIN_BEVEL);
      g2.setStroke(stroke);
      g2.setPaint(Color.red);
      g2.draw(new Line2D.Double(
               -1.75*ds,1.5*ds,-1.50*ds,0.5*ds));
      g2.draw(new Line2D.Double(
               -1.50*ds,0.5*ds,-1.25*ds,1.5*ds));

      //This illustrates the problem with
      // CAP_BUTT -- green
      stroke = new BasicStroke(
                         0.2f*ds,
                         BasicStroke.CAP_BUTT,
                         BasicStroke.JOIN_BEVEL);
      g2.setStroke(stroke);
      g2.setPaint(Color.green);
      g2.draw(new Line2D.Double(
                -0.75*ds,1.5*ds,-0.5*ds,0.5*ds));
      g2.draw(new Line2D.Double(
                -0.5*ds,0.5*ds,-0.25*ds,1.5*ds));

      //Display all three join types in
      // upper-right quadrant

      //Display blue JOIN_BEVEL with CAP_SQUARE
      stroke = new BasicStroke(
                         0.2f*ds,
                         BasicStroke.CAP_SQUARE,
                         BasicStroke.JOIN_BEVEL);
      g2.setStroke(stroke);
      GeneralPath gp1 = new GeneralPath();
      gp1.moveTo(0.25f*ds,-1.25f*ds);
      gp1.lineTo(0.50f*ds,-0.25f*ds);
      gp1.lineTo(0.75f*ds,-1.25f*ds);
      g2.setPaint(Color.blue);
      g2.draw(gp1);

      //Display green JOIN_MITER with CAP_ROUND
      stroke = new BasicStroke(
                         0.2f*ds,
                         BasicStroke.CAP_ROUND,
                         BasicStroke.JOIN_MITER);
      g2.setStroke(stroke);
      GeneralPath gp2 = new GeneralPath();
      gp2.moveTo(0.75f*ds,-0.25f*ds);
      gp2.lineTo(1.00f*ds,-1.25f*ds);
      gp2.lineTo(1.25f*ds,-0.25f*ds);
      g2.setPaint(Color.green);
      g2.draw(gp2);

      //Display red JOIN_ROUND with CAP_BUTT
      stroke = new BasicStroke(
                         0.2f*ds,
                         BasicStroke.CAP_BUTT,
                         BasicStroke.JOIN_ROUND);
      g2.setStroke(stroke);
      GeneralPath gp3 = new GeneralPath();
      gp3.moveTo(1.25f*ds,-1.25f*ds);
      gp3.lineTo(1.50f*ds,-0.25f*ds);
      gp3.lineTo(1.75f*ds,-1.25f*ds);
      g2.setPaint(Color.red);
      g2.draw(gp3);

      //Display dash pattern and miterlimit in
      // bottom-right quadrant

      //Display blue JOIN_BEVEL with CAP_SQUARE
      //Dash pattern is one on, three off, but
      // this is not what it looks like with
      // CAP__SQUARE
      stroke = new BasicStroke(
                   0.2f*ds,
                   BasicStroke.CAP_SQUARE,
                   BasicStroke.JOIN_BEVEL,
                   0.0f,
                   new float[] {0.1f*ds,0.3f*ds},
                   0.0f);
      g2.setStroke(stroke);
      GeneralPath gp4 = new GeneralPath();
      gp4.moveTo(0.25f*ds,1.25f*ds);
      gp4.lineTo(0.50f*ds,0.25f*ds);
      gp4.lineTo(0.75f*ds,1.25f*ds);
      g2.setPaint(Color.blue);
      g2.draw(gp4);

      //Display green JOIN_MITER with CAP_ROUND
      // and miter limit. No dash pattern.
      stroke = new BasicStroke(
                          0.2f*ds,
                          BasicStroke.CAP_ROUND,
                          BasicStroke.JOIN_MITER,
                          .057f*ds);
      g2.setStroke(stroke);
      GeneralPath gp5 = new GeneralPath();
      gp5.moveTo(0.75f*ds,0.25f*ds);
      gp5.lineTo(1.00f*ds,1.25f*ds);
      gp5.lineTo(1.25f*ds,0.25f*ds);
      g2.setPaint(Color.green);
      g2.draw(gp5);

      //Display red JOIN_ROUND with CAP_BUTT
      //Dash pattern is one on, three off again.
      // Looks like it with CAP_BUTT
      stroke = new BasicStroke(
                   0.2f*ds,
                   BasicStroke.CAP_BUTT,
                   BasicStroke.JOIN_ROUND,
                   0.0f,
                   new float[] {0.1f*ds,0.3f*ds},
                   0.0f);
      g2.setStroke(stroke);
      GeneralPath gp6 = new GeneralPath();
      gp6.moveTo(1.25f*ds,1.25f*ds);
      gp6.lineTo(1.50f*ds,0.25f*ds);
      gp6.lineTo(1.75f*ds,1.25f*ds);
      g2.setPaint(Color.red);
      g2.draw(gp6);

      //Draw a circle with an orange outline
      // centered on the origin with a dash
      // pattern and CAP_BUTT end caps.
      stroke = new BasicStroke(
                   0.1f*ds,
                   BasicStroke.CAP_BUTT,
                   BasicStroke.JOIN_ROUND,
                   0.0f,
                   new float[] {0.2f*ds,0.1f*ds},
                   0.0f);
      g2.setStroke(stroke);
      Ellipse2D.Double theCircle =
                new Ellipse2D.Double(
                  -0.4*ds,-0.4*ds,0.8*ds,0.8*ds);
      g2.setPaint(Color.orange);
      g2.draw(theCircle);

    }//end overridden paintComponent()

  }//end class GUI
}//end controlling class Inew2338_118

Answer and Explanation

11.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

class Inew2338_120a extends JFrame{
  int res;
  final int ds = 72;
  final int hSize = 4;
  final int vSize = 4;

  public static void main(String[] args){
    new Inew2338_120a();
  }//end main

  Inew2338_120a(){
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    this.setSize(hSize*res,vSize*res);
    this.setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //===========================================//

  //Inner class
  class GUI extends JPanel{

    public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;//downcast

      g2.scale((double)res/72,(double)res/72);

      //Translate origin to center
      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      //Draw a big circle
      g2.setStroke(new BasicStroke(0.1f*ds));
      Ellipse2D.Double bigCircle =
                new Ellipse2D.Double(
                  -1.5*ds,-1.5*ds,3.0*ds,3.0*ds);
      g2.draw(bigCircle);

      Ellipse2D.Double theEllipse;

      //Translate origin to upper-left quadrant
      g2.translate(-1.0*ds,-1.0*ds);

      //Red horizontal ellipse
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.setPaint(Color.red);
      //Red is not transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,1.0f));
      g2.fill(theEllipse);

      //Green ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.green);
      //Green is not transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,1.0f));
      g2.fill(theEllipse);

      //Blue ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      //Rotate 60 more degrees
      g2.rotate((Math.PI/3.0));
      g2.setPaint(Color.blue);
      //Blue is not transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,1.0f));
      g2.fill(theEllipse);

      //Translate origin to upper-right quadrant
      //Undo previous rotation
      g2.rotate(-2*(Math.PI/3.0));
      g2.translate(2.0*ds,0.0*ds);

      //Red horizontal ellipse
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.setPaint(Color.red);
      //Red is not transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,1.0f));
      g2.fill(theEllipse);

      //Green ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.green);
      //Green is not transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,1.0f));
      g2.fill(theEllipse);

      //Blue ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      //Rotate 60 more degrees
      g2.rotate((Math.PI/3.0));
      g2.setPaint(Color.blue);
      //Blue is 50-percent transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,0.5f));
      g2.fill(theEllipse);

      //Translate origin to lower-left quadrant
      //Undo previous rotation
      g2.rotate(-2*(Math.PI/3.0));
      g2.translate(-2.0*ds,2.0*ds);

      //Red horizontal ellipse
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.setPaint(Color.red);
      //Red is not transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,1.0f));
      g2.fill(theEllipse);

      //Green ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.green);
      //Green is 50 percent transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,0.5f));
      g2.fill(theEllipse);

      //Blue ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      //Rotate 60 more degrees
      g2.rotate((Math.PI/3.0));
      g2.setPaint(Color.blue);
      //Blue is 90-percent transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,0.1f));
      g2.fill(theEllipse);

      //Translate origin to lower-right quadrant
      //Undo previous rotation
      g2.rotate(-2*(Math.PI/3.0));
      g2.translate(2.0*ds,0.0*ds);

      //Red horizontal ellipse
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.setPaint(Color.red);
      //Red is not transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,1.0f));
      g2.fill(theEllipse);

      //Green ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.green);
      //Green is 90-percent transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,0.1f));
      g2.fill(theEllipse);

      //Blue ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      //Rotate 60 more degrees
      g2.rotate((Math.PI/3.0));
      g2.setPaint(Color.blue);
      //Blue is 90-percent transparent
      g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER,0.1f));
      g2.fill(theEllipse);

    }//end overridden paintComponent()

  }//end class GUI
}//end controlling class Inew2338_120a

Answer and Explanation

12.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

class Inew2338_122 extends JFrame{
  int res;
  final int ds = 72;
  final int hSize = 4;
  final int vSize = 4;

  public static void main(String[] args){
    new Inew2338_122();
  }//end main

  Inew2338_122(){//constructor
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    this.setSize(hSize*res,vSize*res);
    this.setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());

    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //===========================================//

  //Inner class
  class GUI extends JPanel{
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;//downcast

      g2.scale((double)res/72,(double)res/72);
      g2.translate((hSize/2)*ds,(vSize/2)*ds);
      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      //Draw a big circle underneath all of the
      // ellipses.
      g2.setStroke(new BasicStroke(0.1f*ds));
      Ellipse2D.Double bigCircle =
                new Ellipse2D.Double(
                  -1.5*ds,-1.5*ds,3.0*ds,3.0*ds);
      g2.draw(bigCircle);

      g2.setStroke(new BasicStroke(0.05f*ds));

      //Translate origin to upper-left quadrant
      g2.translate(-1.0*ds,-1.0*ds);

      //Red horizontal ellipse
      Ellipse2D.Double theEllipse = 
               new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      //Draw nontransparent outline
      g2.setPaint(Color.red);
      g2.draw(theEllipse);
      //Use color constructor for
      // nontransparent red
      g2.setPaint(new Color(
                           1.0f,0.0f,0.0f,1.0f));
      g2.fill(theEllipse);

      //Green ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      //Draw nontransparent outline
      g2.setPaint(Color.green);
      g2.draw(theEllipse);
      //Construct nontransparent green
      g2.setPaint(new Color(
                           0.0f,1.0f,0.0f,1.0f));
      g2.fill(theEllipse);

      //Blue ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      //Rotate 60 more degrees
      g2.rotate((Math.PI/3.0));
      //Draw nontransparent outline
      g2.setPaint(Color.blue);
      g2.draw(theEllipse);
      //Construct nontransparent blue
      g2.setPaint(new Color(
                           0.0f,0.0f,1.0f,1.0f));
      g2.fill(theEllipse);

      //Translate origin to upper-right quadrant
      //Undo previous rotation
      g2.rotate(-2*(Math.PI/3.0));
      g2.translate(2.0*ds,0.0*ds);

      //Red horizontal ellipse
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.setPaint(Color.red);
      g2.draw(theEllipse);
      //Construct 30% transparent red
      g2.setPaint(new Color(
                           1.0f,0.0f,0.0f,0.7f));
      g2.fill(theEllipse);

      //Green ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.green);
      g2.draw(theEllipse);
      g2.setPaint(new Color(
                           0.0f,1.0f,0.0f,0.7f));
      g2.fill(theEllipse);

      //Blue ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.blue);
      g2.draw(theEllipse);
      g2.setPaint(new Color(
                           0.0f,0.0f,1.0f,0.7f));
      g2.fill(theEllipse);

      //Translate origin to lower-left quadrant
      g2.rotate(-2*(Math.PI/3.0));
      g2.translate(-2.0*ds,2.0*ds);

      //Red horizontal ellipse
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.setPaint(Color.red);
      g2.draw(theEllipse);
      //Construct 60% transparent red
      g2.setPaint(new Color(
                           1.0f,0.0f,0.0f,0.4f));
      g2.fill(theEllipse);

      //Green ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);
      g2.setPaint(Color.green);
      g2.draw(theEllipse);
      //Construct 60% transparent green
      g2.setPaint(new Color(
                           0.0f,1.0f,0.0f,0.4f));
      g2.fill(theEllipse);

      //Blue ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate((Math.PI/3.0));
      g2.setPaint(Color.blue);
      g2.draw(theEllipse);
      //Construct 60% transparent blue
      g2.setPaint(new Color(
                           0.0f,0.0f,1.0f,0.4f));
      g2.fill(theEllipse);

      //Translate origin to lower-right quadrant
      g2.rotate(-2*(Math.PI/3.0));
      g2.translate(2.0*ds,0.0*ds);

      //Red horizontal ellipse
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);

      g2.setPaint(Color.red);
      g2.draw(theEllipse);
      //Construct 90% transparent red
      g2.setPaint(new Color(
                           1.0f,0.0f,0.0f,0.1f));
      g2.fill(theEllipse);

      //Green ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.green);
      g2.draw(theEllipse);
      //Construct 90% transparent green
      g2.setPaint(new Color(
                           0.0f,1.0f,0.0f,0.1f));
      g2.fill(theEllipse);

      //Blue ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.blue);
      g2.draw(theEllipse);
      //Construct 90% transparent blue
      g2.setPaint(new Color(
                           0.0f,0.0f,1.0f,0.1f));
      g2.fill(theEllipse);

    }//end overridden paintComponent()

  }//end class GUI
}//end controlling class Inew2338_122

Answer and Explanation

13.  True or false?  The code in the following box produces the graphics output shown in the second box below.

import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

class Inew2338_124 extends JFrame{
  int res;
  final int ds = 72;
  final int hSize = 4;
  final int vSize = 4;

  public static void main(String[] args){
    new Inew2338_124();
  }//end main

  Inew2338_124(){//constructor
    res = Toolkit.getDefaultToolkit().
                           getScreenResolution();
    this.setSize(hSize*res,vSize*res);
    this.setTitle("Copyright 2004, R.G.Baldwin");
    getContentPane().add(new GUI());

    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
  }//end constructor
  //===========================================//

  //Inner class
  class GUI extends JPanel{
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;//downcast

      setBackground(Color.WHITE);

      g2.scale((double)res/72,(double)res/72);
      g2.translate((hSize/2)*ds,(vSize/2)*ds);

      g2.setClip(new Rectangle2D.Double(
                 -1.5*ds,-1.5*ds,2.5*ds,2.5*ds));
      //Draw x-axis
      g2.draw(new Line2D.Double(
                        -1.5*ds,0.0,1.5*ds,0.0));
      //Draw y-axis
      g2.draw(new Line2D.Double(
                        0.0,-1.5*ds,0.0,1.5*ds));

      //Draw a big circle
      g2.setStroke(new BasicStroke(0.1f*ds));
      Ellipse2D.Double bigCircle =
                new Ellipse2D.Double(
                  -1.5*ds,-1.5*ds,3.0*ds,3.0*ds);
      g2.draw(bigCircle);

      g2.setStroke(new BasicStroke(0.05f*ds));

      //Translate origin to upper-left quadrant
      g2.translate(-1.0*ds,-1.0*ds);

      //Horizontal ellipse
      Ellipse2D.Double theEllipse =
               new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      //Draw nontransparent outline
      g2.setPaint(Color.BLACK);
      g2.draw(theEllipse);
      //60% transparent black
      g2.setPaint(new Color(
                           0.0f,0.0f,0.0f,0.4f));
      g2.fill(theEllipse);

      //Translate origin to upper-right quadrant
      g2.translate(2.0*ds,0.0*ds);

      //Ellipse at 60 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(Math.PI/3.0);//rotate 60 degrees
      g2.setPaint(Color.BLACK);
      g2.draw(theEllipse);
      //70% transparent black
      g2.setPaint(new Color(
                           0.0f,0.0f,0.0f,0.3f));
      g2.fill(theEllipse);

      //Translate origin to lower-left quadrant
      g2.rotate(-Math.PI/3.0);
      g2.translate(-2.0*ds,2.0*ds);

      //Ellipse at 120 degrees
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);
      g2.rotate(2.0*(Math.PI/3.0));
      g2.setPaint(Color.BLACK);
      g2.draw(theEllipse);
      //80% transparent black
      g2.setPaint(new Color(
                           0.0f,0.0f,0.0f,0.2f));
      g2.fill(theEllipse);

      //Translate origin to lower-right quadrant
      g2.rotate(-2*(Math.PI/3.0));
      g2.translate(2.0*ds,0.0*ds);

      //Horizontal ellipse
      theEllipse = new Ellipse2D.Double(
                 -1.0*ds,-0.25*ds,2.0*ds,0.5*ds);

      g2.setPaint(Color.BLACK);
      g2.draw(theEllipse);
      //90% transparent black
      g2.setPaint(new Color(
                           0.0f,0.0f,0.0f,0.1f));
      g2.fill(theEllipse);

    }//end overridden paintComponent()

  }//end class GUI
}//end controlling class Inew2338_124

Answer and Explanation



Copyright 2004, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which have gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com


Answers and Explanations


Answer 13

True.

Explanation 13

This program illustrates the use of the constructors of the Color class to achieve transparency with solid-fill black. The program also illustrates the use of a clipping region.

The program translates the origin to the center of the frame.  Then it sets a clipping region that is smaller than that required to contain the entire drawing.

The program draws a pair of X and Y-axes centered on the new origin. It draws a big circle centered on the origin underneath all of the ellipses. It uses rotation and translation to fill an ellipse in each of the four quadrants. Some of the ellipses are rotated.

Each ellipse has a relatively wide non-transparent outline, which is filled with partially transparent black as follows:

You can learn more about these topics in lesson 324 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 13


Answer 12

True.

Explanation 12

This program illustrates the use of the constructors of the Color class to achieve transparency with solid-fill colors.

The program translates the origin to the center of the frame where it draws a pair of X and Y-axes centered on the new origin.  Then it draws a big circle centered on the origin underneath all of the ellipses.

Following this, the program uses rotation and translation to fill three ellipses in each of the four quadrants.  The ellipses intersect at their center.  Each ellipse is rotated by 60 degrees relative to the one below it.

The order of the ellipses is:

Each ellipse has a relatively wide non-transparent outline.

The transparency characteristics are as follows:

You can learn more about this topic in lesson 324 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 12


Answer 11

False.

Explanation 11

The program produces the graphic output shown below.  This output differs from the output shown in the question with respect to the amount of rotation of the ellipses.

This program Illustrates the use of the AlphaComposite class to achieve transparency with solid-fill colors.

The program draws a big circle centered on the origin underneath all of the ellipses.

It uses rotation and translation to fill three ellipses in each of the four quadrants.  The ellipses intersect at their center.  Each is rotated by 60 degrees relative to the one below it. The order is:

The upper-left quadrant shows no transparency

The upper-right quadrant shows:

The lower-left quadrant shows:

The lower-right quadrant shows:

The program also illustrates the use of the Stroke interface along with translation and rotation using affine transforms.

You can learn more about this topic in lesson 320 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 11


Answer 10

True.

Explanation 10

This program illustrates:

You can learn more about this topic in lesson 318 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 10


Answer 9

False.

Explanation 9

This program produces white circles on a black background as shown below instead of black circles on a white background.

This program illustrates the use of BufferedImage and TexturePaint.

You can learn more about this topic in lesson 316 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 9


Answer 8

True.

Explanation 8

This program illustrates use of a Paint object to fill a Shape
with a gradient and a solid color.  The gradient is from red to orange in all cases.  The fill characteristics are as follows:

You can learn more about this topic in lesson 314  at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 8


Answer 7

False.

Explanation 7

The fill color of the circle in the lower-right quadrant in the image referred to by the question is not the correct color.  The fill color should be yellow as shown below instead of turquoise as shown in the image referred to by the question.

This program illustrates use of a Paint object to fill a Shape
with a solid color.  You can learn more about this topic in lesson 312 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 7


Answer 6

False.

Explanation 6

The screen output shown for this program is correct, but the graphic is incorrect.  The correct graphic for this program is shown below.  (Note the difference in the location of the red box.)

This program illustrates the use of a PathIterator to iterate on a GeneralPath object, extracting information about that object, and using that information to replicate the shape of the object at a different location in the graphic.  The new object is drawn in a different color.

You can learn more about the use of a PathIterator in conjunction with a GeneralPath object in lesson 310 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 6


Answer 5

True.

Explanation 5

This program illustrates the use of the GeneralPath class to create and draw a shape based on straight lines connecting a series of points.

You can learn more about the use of the GeneralPath class in lesson 310 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 5


Answer 4

False.

Explanation 4

The graphic output shown in the question is correct, but the screen output shown in the question is not correct.  The correct screen output is shown below.  Note the difference in the boldface third line from the bottom.

The Circle:
Contains the red BoundingBox: false
Contains the green InsideBox: true
Contains the blue IntersectingBox: false
Contains the magenta OutsideBox: false
The Circle:
Intersects the red BoundingBox: true
Intersects the green InsideBox: true
Intersects the blue IntersectingBox: true
Intersects the magenta OutsideBox: false

This program illustrates the Shape interface.  You can learn more about this topic in lesson 308 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 4


Answer 3

True.

Explanation 3

This program illustrates the use of light and shadows to produce the illusion of a 3D button, which can protrude from the surface or be pushed into the surface.  (Note that this optical illusion may not have the same effect on all people.)

You can learn more about this topic in lesson 160 at http://www.dickbaldwin.com/tocadv.htm.  While there, you may also find it useful to study lessons 162 through 182.  Those lessons provide a great deal of information about graphics in Java that you should understand before embarking on the Java 2D API.  You will also find improved versions of the 3D optical illusion in some of those lessons.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 3


Answer 2

True.

Explanation 2

This program illustrates the use of the Java 2D API for the drawing and transforming of rectangular shapes.  In particular, this program illustrates the application of affine transforms of the following types in the following order to what would otherwise be a set of nested rectangles with some text superimposed on the rectangles:

You can learn more about the application of affine transforms in lesson 306 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 2


Answer 1

True.

Explanation 1

This is a rather straightforward application of the use of the Java 2D Graphics API to draw rectangular shapes.

You can learn more about this topic in lessons 300 through 306 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 1


Copyright 2004, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which have gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-