Graphics Flashcards
The coordinate of the upper-left corner of a frame is \_\_\_ A. (0, 0) B. (25, 25) C. (100, 100) D. (10, 10)
(0, 0)
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)
A. (bt.getX(), jbt.getY())
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.
All are true
The header for the paintComponent method is ___
protected void paintComponent(Graphics g)
You should override the _____ method to draw things on a Swing component.
paintComponent()
Can you draw graphics on any GUI component?
Yes, but you should use JPanel (or an extension of JPanel)
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.
B, C and D are true
To draw graphics, it is better to define a class that extends \_\_\_\_ and override the paintComponent method. A. JLabel B. JButton C. JPanel D. JComponent
It’s best to extend JPanel. JLabel and JButton are specifically designed with particular features for labels and buttons. They are not appropriate for custom drawing. The problem with JComponent is that you have to write the code to paint the background color if you wish to set a background in the canvas. A simple setBackground(Color color) method will not set a background color in a JComponent.
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); } }
What happens?
The program runs, but it does not display the message, because the paintComponent method is not overridden. You have to spell paintComponent not paintcomponent.
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); } }
What happens?
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”).
(A) is incorrect because the constructor of Test is not invoked. (B) is incorrect because of the compile error on Test(). (C) is correct. (D) is incorrect because the Test class does not have a constructor with a String argument.
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.
The correct answer is D Explanation: (B) is incorrect since MyCanvas does not have a constructor with a string argument. (C) is incorrect since new Test1() is an instance of JFrame so it is fine to assign it to frame.
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())
B. g,drawLine(0, 0, getWidth(), getHeight())
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 ____
g.drawRect(20, 20, 20, 50)
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)
D. g.drawOval(30, 30, 40, 40)
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.
B, C and D are true