Chapter 5

northcarolinawrySoftware and s/w Development

Nov 3, 2013 (3 years and 5 months ago)

76 views

Chapter 5

R5
-
1

Console applications

provide text
-
only interfaces and cannot display any
drawings/figures (except ASCII art).
Graphical applications

are more user friendly and
can display drawings inside frames (windows).

Furthermore, in a
console applicati
on
, the programmer dictates the control flow and
forces the user to enter input in a predetermined order. A
graphical application
, however,
generally makes a large number of controls available to the program user (buttons, input
fields, scroll bars, and so

on), which users can manipulate in any order they please.
Therefore, the program must be prepared to process input from multiple sources in
random order.

R5
-
2

The Swing toolkit calls the
paintComponent

method whenever the component needs to
be repainted.
For example, it is called when the window is shown for the first time, when
it is resized, or when it is shown again after it was hidden.

R5
-
3

The designers of Java did not want to inconvenience those programmers who had
produced programs that used simple
graphics from the
Graphics

class after they
developed the newer, more powerful
Graphics2D

class, so they did not change the
parameter of the
paintComponent

method to
Graphics2D
.

R5
-
4

The purpose of a graphics context is to store the programmer choices for
colors, fonts,
and so on, and to draw and fill shapes.

R5
-
5

The
paintCompoment

method of the component class produces a drawing, while the
main

method of the viewer class constructs a frame and a component, adds the
component to the frame, and makes the fr
ame visible.

R5
-
6

Because it is a good idea to make a class for each complex graphical shape; in this case, a
Car

class. The component class draws two cars, unsing the
draw

method of the
Car

class.

R5
-
7

You specify a text color simply by calling the
setCol
or

method of the graphics context
before calling the
drawString

method.

R5
-
8

The following classes are used in this chapter to draw graphical shapes:

Rectangle

Ellipse2D.Double

Line2D.Double

R5
-
9

final double WIDTH = 20;

final double HEIGHT = 30;


// draw
X

Line2D.Double up = new Line2D.Double(0, HEIGHT, WIDTH, 0);

Line2D.Double down = new Line2D.Double(0, 0, WIDTH, HEIGHT);

g2.draw(up);

g2.draw(down);


// draw T

Line2D.Double horiz = new Line2D.Double(0, 0, WIDTH, 0);

Line2D.Double vert = new Line2D.Double
(WIDTH / 2, 0, WIDTH / 2,
HEIGHT);

g2.draw(horiz);

g2.draw(vert);

R5
-
10

g2.draw(e);

Ellipse2D.Double e2 = new Ellipse2D.Double(e.getX() + e.getWidth(),
e.getY(), e.getWidth(), e.getHeight());

g2.draw(e2);

R5
-
11

The intersection points are drawn in a diff
erent location
--
they no longer fall on the actual
intersection of the circle and the line.

R5
-
12

If
x

is 30, then we need to compute

r * r
-

(x
-

a) * (x
-

a)

Since
a

=
b

=
r

=
100
, the value is

100 * 100
-

70 * 70 = 5100

The square root is 71.41. Therefor
e,

y1

=
b

+
root

=
171.41

y2

=
b

-

root

=
28.59

P5
-
14

Ch05/ExP5_14/ExP5_14.java

import javax.swing.JFrame;


public class ExP5_14

{


public static void main(String[] args)


{


JFrame frame = new JFrame();



final int FRAME_WIDTH = 300;


f
inal int FRAME_HEIGHT = 230;



frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);


frame.setTitle("ExP5_14");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



OlympicRingComponent component = new OlympicRingComponent();


frame.add(c
omponent);



frame.setVisible(true);


}

}

Ch05/ExP5_14/OlympicRingComponent.java

import javax.swing.JComponent;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;


/**


Draws the olympic rings.


*/

public class OlympicRin
gComponent extends JComponent

{


public void paintComponent(Graphics g)


{


Graphics2D g2 = (Graphics2D) g;



final int RADIUS = 50;


final int DISTANCE = RADIUS * 8 / 5;



double x = 0;


double y = 0;



Ring blue = new Ri
ng(x, y, RADIUS, Color.blue);


blue.draw(g2);



x = x + DISTANCE;


Ring black = new Ring(x, y, RADIUS, Color.black);


black.draw(g2);



x = x + DISTANCE;


Ring red = new Ring(x, y, RADIUS, Color.red);


red.draw(g2);




x = DISTANCE / 2;


y = DISTANCE;


Ring yellow = new Ring(x, y, RADIUS, Color.yellow);


yellow.draw(g2);



x = x + DISTANCE;


Ring green = new Ring(x, y, RADIUS, Color.green);


green.draw(g2);


}

}

Ch05/ExP5_14/Ring.java

im
port java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;


/**


A class that draw the Olympic rings.

*/

public class Ring

{


/**


Constructs a circle that represents the Olympic rings.


@param
anX the x coordinate


@param aY the y coordinate


@param aRadius the radius of the circle


@param aColor the color of the ring


*/


public Ring(double anX, double aY, double aRadius, Color aColor)


{


x = anX;


y = aY;


radius = aRadius;


color = aColor;


}



/**


Draw the rings.


@param g2 the graphic content


*/


public void draw(Graphics2D g2)


{


g2.setColor(color);



Ellipse2D.Double e = new Ellipse2D.Double(x, y, 2 * radius, 2 *
ra
dius);



g2.draw(e);


}



private double x;


private double y;


private double radius;


private Color color;

}