Chapter 5

Programming Graphics


Chapter Goals

Frame Windows

A Frame Window


File EmptyFrameViewer.java

Self Check

  1. How do you display a square frame with a title bar that reads "Hello, World!"?
  2. How can a program display two frames at once?

Answers

  1. Modify the EmptyFrameViewer program as follows:
    frame.setSize(300, 300);
    frame.setTitle("Hello, World!");
  2. Construct two JFrame objects, set each of their sizes, and call setVisible(true) on each of them

Drawing Shapes

Drawing Shapes

Drawing Rectangles


Rectangle Drawing Program Classes

File RectangleComponent.java

File RectangleViewer.java

Self Check

  1. How do you modify the program to draw two squares?
  2. How do you modify the program to draw one rectangle and one square?
  3. What happens if you call g.draw(box) instead of g2.draw(box)?

Answers

  1. Rectangle box = new Rectangle(5, 10, 20, 20);
  2. Replace the call to box.translate(15, 25) with box = new Rectangle(20, 35, 20, 20);
  3. The compiler complains that g doesn't have a draw method

Applets

Applets

File RectangleApplet.java

File RectangleApplet.html

File RectangleAppletExplained.html

Applets


Graphical Shapes

An Ellipse

Drawing Lines

Drawing Strings

g2.drawString("Message", 50, 100);
 

Self Check

  1. Give instructions to draw a circle with center (100, 100) and radius 25
  2. Give instructions to draw a letter "V" by drawing two line segments
  3. Give instructions to draw a string consisting of the letter "V"

Answers

  1. g2.draw(new Ellipse2D.Double(75, 75, 50, 50);
  2. Line2D.Double segment1 = new Line2D.Double(0, 0, 10, 30);
    g2.draw(segment1);
    Line2D.Double segment2 = new Line2D.Double(10, 30, 20, 0);
    g2.draw(segment2);
  3. g2.drawString("V", 0, 30);

Colors

Self Check

  1. What are the RGB color values of Color.BLUE?
  2. How do you draw a yellow square on a red background?

Answers

  1. 0.0F, 0.0F, and 0.1F
  2. First fill a big red square, then fill a small yellow square inside:
    g2.setColor(Color.RED);
    g2.fill(new Rectangle(0, 0, 200, 200));
    g2.setColor(Color.YELLOW);
    g2.fill(new Rectangle(50, 50, 100, 100));

Drawing Complex Shapes

Drawing Cars


Plan Complex Shapes on Graph Paper


File CarComponent.java

File Car.java

File CarViewer.java

Self Check

  1. Which class needs to be modified to have the two cars positioned next to each other?
  2. Which class needs to be modified to have the car tires painted in black, and what modification do you need to make?
  3. How do you make the cars twice as big?

Answers

  1. CarComponent
  2. In the draw method of the Car class, call
    g2.fill(frontTire);
    g2.fill(rearTire);
  3. Double all measurements in the draw method of the Car class

Drawing Graphical Shapes

Rectangle leftRectangle = new Rectangle(100, 100, 30, 60);
Rectangle rightRectangle = new Rectangle(160, 100, 30, 60);
Line2D.Double topLine
= new Line2D.Double(130, 100, 160, 100);
Line2D.Double bottomLine
= new Line2D.Double(130, 160, 160, 160);

Reading Text Input

File ColorViewer.java

File ColoredSquareComponent.java

Output


Self Check

  1. Why does this program produce three separate dialog boxes instead of inviting the user to type all three values in a single dialog box?
  2. Why does this program place the showInputDialog call into the main method of the ColorViewer class and not into the paintComponent method of the ColorComponent class?

Answers

  1. If the user entered a string, such as "1.0 0.7 0.7", you would need to break it up into three separate strings. That can be done, but it is more tedious to program than three calls to showInputDialog.
  2. You don't want the dialog boxes to appear every time the component is repainted.

Comparing Visual and Numerical Information


Comparing Visual and Numerical Information

Intersection of a Line and a Circle


File IntersectionComponent.java

File IntersectionViewer.java

File LabeledPoint.java

Self Check

  1. Suppose you make a mistake in the math, say, by using a + sign instead of a - sign in the formula for root. How can you tell that the program does not run correctly?
  2. Which intersection points does the program draw when you provide an input of 0?

Answers

  1. The intersection points will be drawn at a location that is different from the true intersection of the line and the circle
  2. The point (0, 100) is drawn twice