Introduction to Java Programming, Seventh Edition, Y. Daniel Liang

Chapter 14 Graphics


Section 14.2 Graphical Coordinate Systems
1  The coordinate of the upper-left corner of a frame is __________.

A. (0, 0)
B. (25, 25)
C. (100, 100)
D. (10, 10)

2  Suppose a button jbt is placed in a frame, the coordinate of the button within the content pane of the frame is _______.

A. (jbt.getX(), jbt.getY())
B. (jbt.x, jbt.y)
C. cannot be obtained
D. (0, 0)

Section 14.3 The Graphics Class
3  Which of the following statements are true?

A. Each GUI component contains a Graphics object that can be obtained using getGraphics() method.
B. Once a GUI component is visible, getGraphics() returns the object.
C. If a GUI component is not visible, getGraphics() returns null.
D. The Graphics object is automatically created for each visible GUI component.

Section 14.4 The paintComponent Method
4  The header for the paintComponent method is ________________.

A. private void paintComponent(Graphics g)
B. protected void paintComponent(Graphics g)
C. public void paintComponent(Graphics g)
D. protected void paintComponent()

5  You should override the __________ method to draw things on a Swing component.

A. repaint()
B. update()
C. paintComponent()
D. init()

6  You can draw graphics on any GUI components.

A. true
B. false

7  Which of the following statements are true?

A. You may create a Graphics object using new Graphics().
B. Whenever a GUI component is displayed, its Graphics object is automatically created.
C. The paintComponent method is automatically invoked by the JVM. You should never invoke it directly.
D. Invoking repaint() causes paintComponent to be invoked by the JVM.

Section 14.5 Drawing Graphics 0n Panels
8  To draw graphics, it is better to declare a class that extends ________ and override the paintComponent method.

A. JLabel
B. JButton
C. JPanel
D. JComponent

9  Analyze the following code.

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

public class Test {
   public static void main(String[] args) {
     JFrame frame = new JFrame("My Frame");
     frame.add(new MyDrawing("Welcome to Java!"));
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
     frame.setVisible(true);
   }
}

class MyDrawing extends JPanel {
   String message;

   public MyDrawing(String message) {
     this.message = message;
   }

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

     g.drawString(message, 20 ,20);
   }
}

A. The program runs fine and displays Welcome to Java!
B. The program has a compile error because the paintcomponent should be spelled as paintComponent.
C. The program has a runtime error because the paintcomponent should be spelled as paintComponent.
D. The program runs, but it does not display the message.
E. It is a runtime error to invoke the setVisible(true) twice.

10  Analyze the following code.

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

public class Test extends JFrame {
   public Test() {
     add(new MyDrawing("Welcome to Java!"));
   }

   public static void main(String[] args) {
     JFrame frame = new JFrame();
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}

class MyDrawing extends JPanel {
   String message;

   public MyDrawing(String message) {
     this.message = message;
   }

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

     g.drawString(message, 20 ,20);
   }
}

A. The program runs fine and displays Welcome to Java!
B. The program would display Welcome to Java! if new JFrame() is replaced by Test().
C. The program would display Welcome to Java! if new JFrame() is replaced by new Test().
D. The program would display Welcome to Java! if new JFrame() is replaced by new Test("My Frame").

11  Analyze the following code.

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

public class Test1 extends JFrame {
   public Test1() {
     add(new MyCanvas());
   }

   public static void main(String[] args) {
     JFrame frame = new Test1();
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}

class MyCanvas extends JPanel {
   private String message;

   public void setMessage(String message) {
     this.message = message;
   }

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

     g.drawString(message, 20, 20);
   }
}

A. The program runs fine and displays nothing since you have not set a string value.
B. The program would display Welcome to Java! if you replace new MyCanvas() by new MyCanvas("Welcome to Java!").
C. The program has a compile error because new Test1() is assigned to frame.
D. The program has a NullPointerException since message is null when g.drawString(message, 20, 20) is executed.

Section 14.6 Drawing Strings, Lines, rectangles, and Ovals
12  Given a Graphics object g, to draw a line from the upper left corner to the bottom right corner, you use __________.

A. g.drawLine(0, 0, 100, 100)
B. g.drawLine(0, 0, getWidth(), getHeight())
C. g.drawLine(0, 0, getHeight(), getHeight())
D. g.drawLine(0, 0, getWidth(), getWidth())

13  Given a Graphics object g, to draw an outline of a rectangle of width 20 and height 50 with the upper-left corner at (20, 20), you use __________.

A. g.drawRect(20, 50, 20, 20)
B. g.drawRectFill(20, 20, 20, 50)
C. g.drawRect(20, 20, 20, 50)
D. g.drawRectFill(20, 50, 20, 20)

14  Given a Graphics object g, to draw an circle with radius 20 centered at (50, 50), you use __________.

A. g.drawOval(50, 50, 20, 20)
B. g.drawOval(50, 50, 40, 40)
C. g.drawOval(30, 30, 20, 20)
D. g.drawOval(30, 30, 40, 40)

15  Given a Graphics object g, to draw a filled oval with width 20 and height 30 centered at (50, 50), you use __________.

A. g.fillOval(50, 50, 20, 30)
B. g.fillOval(50, 50, 40, 30)
C. g.fillOval(30, 30, 20, 30)
D. g.fillOval(30, 30, 40, 30)
E. g.fillOval(40, 35, 20, 30)

16  Which of the following methods draws a filled 3D rectangle.

A. g.fill3DRect(50, 50, 20, 30)
B. g.fill3DRect(50, 50, 20, 30, 1)
C. g.fill3DRect(50, 50, 20, 30, true)
D. g.fill3DRect(50, 50, 20, 30, false)

Section 14.7 Case Study: The FigurePanel Class
17  To repaint graphics, invoke __________ on a Swing component.

A. repaint()
B. update()
C. paintComponent()
D. init()

Section 14.8 Drawing Arcs
18  Given a Graphics object g, to draw a filled arc with radius 20 centered at (50, 50) and start angle 0 and spanning angle 90, you use __________.

A. g.fillArc(50, 50, 40, 40, 0, Math.toRadian(90))
B. g.fillArc(50, 50, 40, 40, 0, 90)
C. g.fillArc(30, 30, 40, 40, 0, Math.toRadian(90))
D. g.fillArc(30, 30, 40, 40, 0, 90)
E. g.fillArc(50, 50, 20, 20, 0, 90)

Section 14.9 The Polygon class and Drawing Polygons and Polylines
19  Given a Graphics object g, to draw a polygon to connect points (3, 3), (4, 10), (10, 20), (2, 100), you use __________.

A. g.drawPolyline(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4)
B. g.drawPolyline({3, 4, 10, 2}, {3, 10, 20, 100}, 4)
C. g.drawPolygon(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4)
D. g.drawPolygon({3, 4, 10, 2}, {3, 10, 20, 100}, 4)

20  Given a Graphics object g, to draw a polyline to connect points (3, 3), (4, 10), (10, 20), (2, 100), you use __________.

A. g.drawPolyline(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4)
B. g.drawPolyline({3, 4, 10, 2}, {3, 10, 20, 100}, 4)
C. g.drawPolygon(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4)
D. g.drawPolygon({3, 4, 10, 2}, {3, 10, 20, 100}, 4)

Section 14.10 Centering Display Using the FontMetrics Class
21  Which of the following statements are true?

A. You can create a FontMetric using new FontMetrics().
B. You can obtain a FontMetrics from a Font object using the getFontMetrics() method.
C. A font determines the font metrics.
D. You can obtain the leading, ascent, descent, and height for a font from a FontMetrics object.

22  Invoking __________ returns the width of the string in a FontMetrics object fm.

A. getLength(s)
B. fm.getHeight(s)
C. fm.stringWidth(s)
D. fm.getWidth(s)

23  The following are the methods to obtain font properties in a FontMetrics object fm.

A. fm.getAscent()
B. fm.getDescent()
C. fm.getLeading()
D. fm.getHeight()

Section 14.13 (Optional) Displaying Images
24  To create an Image object from an ImageIcon object imageIcon, use the __________ method.

A. imageIcon.image()
B. imageIcon.getImage()
C. imageIcon.setImage()
D. imageIcon.returnImage()

25  Which of the following statements are correct?
A. You can set an image on a label, but the image is not resizable.
B. You can set an image on a button, but the image is not resizable.
C. You can draw an image on a GUI component using the drawImage method in the Graphics object. This image is resizable.