Java Reference
In-Depth Information
Self-Check Problems
Section 3G.1: Introduction to Graphics
1. There are two mistakes in the following code, which attempts to draw a line from coordinates (50, 86) to (20, 35).
What are they?
DrawingPanel panel = new DrawingPanel(200, 200);
panel.drawLine(50, 20, 86, 35);
2. The following code attempts to draw a black-filled outer rectangle with a white-filled inner circle inside it:
DrawingPanel panel = new DrawingPanel(200, 100);
Graphics g = panel.getGraphics();
g.setColor(Color.WHITE);
g.fillOval(10, 10, 50, 50);
g.setColor(Color.BLACK);
g.fillRect(10, 10, 50, 50);
However, the graphical output looks like Figure 3G.17 instead. What must be changed for it to look as intended?
Figure 3G.17
3. The following code attempts to draw a black rectangle from (10, 20) to (50, 40) with a line across its diagonal:
DrawingPanel panel = new DrawingPanel(200, 100);
Graphics g = panel.getGraphics();
g.drawRect(10, 20, 50, 40);
g.drawLine(10, 20, 50, 40);
However, the graphical output looks like Figure 3G.18 instead. What must be changed for it to look as intended?
4. What sort of figure will be drawn by the following program? Can you draw a picture that will approximately match
its appearance without running it first?
1 import java.awt.*;
2
3 public class Draw7 {
 
Search WWH ::




Custom Search