[go: up one dir, main page]

0% found this document useful (0 votes)
30 views7 pages

Working With Graphics

This document discusses working with graphics and text in Java. It covers drawing various shapes like lines, rectangles, ellipses and arcs using different graphics methods. It also discusses working with colors in Java including creating custom colors and converting between color models. The document provides examples of drawing different shapes and colors.

Uploaded by

shobyscms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

Working With Graphics

This document discusses working with graphics and text in Java. It covers drawing various shapes like lines, rectangles, ellipses and arcs using different graphics methods. It also discusses working with colors in Java including creating custom colors and converting between color models. The document provides examples of drawing different shapes and colors.

Uploaded by

shobyscms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Module 5- Working with Windows, Graphics, and Text

Working with Graphics


 The AWT supports a rich assortment of graphics methods.
 All graphics are drawn relative to a window.
 This can be the main window of an applet, a child window of an applet,
or a stand-alone application window.
 The origin of each window is at the top-left corner and is 0,0.
 Coordinates are specified in pixels.
 All output to a window takes place through a graphics context.
 A graphics context is encapsulated by the Graphics class and is obtained
in two ways:
 It is passed to an applet when one of its various methods,
such as paint( ) or update( ), is called.
 It is returned by the getGraphics( ) method of Component.

 The Graphics class defines a number of drawing functions.


 Each shape can be drawn edge-only or filled.
 Objects are drawn and filled in the currently selected graphics color,
which is black by default.
 When a graphics object is drawn that exceeds the dimensions of the
window, output is automatically clipped.

Drawing Lines
 Lines are drawn by means of the drawLine( ) method, shown here:
void drawLine(int startX, int startY, int endX, int endY)

 drawLine( ) displays a line in the current drawing color that begins


at startX,startY and ends at endX,endY.
Example:
g.drawLine(40, 25, 250, 180);

Drawing Rectangles
 The drawRect( ) and fillRect( ) methods display an outlined and filled
rectangle, respectively.
 They are shown here:
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)
 The upper-left corner of the rectangle is at top,left.
 The dimensions of the rectangle are specified by width and height.

 To draw a rounded rectangle, use drawRoundRect( ) or


fillRoundRect( ), both shown here:
void drawRoundRect(int top, int left, int width, int height, int
xDiam, int yDiam)
void fillRoundRect(int top, int left, int width, int height, int
xDiam, int yDiam)
 A rounded rectangle has rounded corners.
 The upper-left corner of the rectangle is at top,left.
 The dimensions of the rectangle are specified by width and height.
 The diameter of the rounding arc along the X axis is specified by
xDiam.
 The diameter of the rounding arc along the Y axis is specified by
yDiam.
Example:
g.drawRect(10, 10, 60, 50);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
g.fillRoundRect(70, 90, 140, 100, 30, 40);

Drawing Ellipses and Circles


 To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ).
These methods are shown here:
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)
 The ellipse is drawn within a bounding rectangle whose upper-left corner
is specified by top,left and whose width and height are specified by width
and height.
 To draw a circle, specify a square as the bounding rectangle.

Example:
g.drawOval(10, 10, 50, 50);
g.fillOval(100, 10, 75, 50);

Drawing Arcs
 Arcs can be drawn with drawArc( ) and fillArc( ), shown here:

void drawArc(int top, int left, int width, int height, int startAngle, int
sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int
sweepAngle)

 The arc is bounded by the rectangle whose upper-left corner is specified


by top,left and whose width and height are specified by width and height.
 The arc is drawn from startAngle through the angular distance specified
by sweepAngle.
 Angles are specified in degrees.
 Zero degrees is on the horizontal, at the three o’clock position.
 The arc is drawn counterclockwise if sweepAngle is positive, and
clockwise if sweepAngle is negative.
 Therefore, to draw an arc from twelve o’clock to six o’clock, the start
angle would be 90 and the sweep angle 180.

Example:
g.drawArc(10, 40, 70, 70, 0, 75);
g.fillArc(100, 40, 70, 70, 0, 75);

Drawing Polygons
 It is possible to draw arbitrarily shaped figures using drawPolygon( ) and
fillPolygon( ), shown here:
 void drawPolygon(int x[ ], int y[ ], int numPoints)
 void fillPolygon(int x[ ], int y[ ], int numPoints)
 The polygon’s endpoints are specified by the coordinate pairs contained
within the x and y arrays.
 The number of points defined by x and y is specified by numPoints.
 There are alternative forms of these methods in which the polygon is
specified by a Polygon object.

Example:
int xpoints[] = {30, 200, 30, 200, 30};
int ypoints[] = {30, 30, 200, 200, 30};
int num = 5;
g.drawPolygon(xpoints, ypoints, num);

Sizing Graphics
 Often, you will want to size a graphics object to fit the current size of the
window in which it is drawn.
 To do so, first obtain the current dimensions of the window by calling
getSize( ) on the window object.
 It returns the dimensions of the window encapsulated within a Dimension
object.
 Once you have the current size of the window, you can scale your
graphical output accordingly.

Working with Color


 Java supports color in a portable, device-independent fashion.
 The AWT color system allows you to specify any color you want.
 Color is encapsulated by the Color class.
 Color defines several constants (for example, Color.black) to specify a
number of common colors.
 You can also create your own colors, using one of the color constructors.
 The most commonly used forms are shown here:
1. Color(int red, int green, int blue)
2. Color(int rgbValue)
3. Color(float red, float green, float blue)

 The first constructor takes three integers that specify the color as a mix of
red, green, and blue.
 These values must be between 0 and 255, as in this example:
new Color(255, 100, 100); // light red.

 The second color constructor takes a single integer that contains the mix
of red, green, and blue packed into an integer.
 The integer is organized with red in bits 16 to 23, green in bits 8 to 15,
and blue in bits 0 to 7.
 Here is an example of this constructor:
int newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00);
Color darkRed = new Color(newRed);

 The final constructor, Color(float, float, float), takes three float values
(between 0.0 and 1.0) that specify the relative mix of red, green, and blue.

 Once you have created a color, you can use it to set the foreground and/or
background color by using the setForeground( ) and setBackground( )
methods.
 You can also select it as the current drawing color.

Color Methods
The Color class defines several methods that help manipulate colors.

Using Hue, Saturation, and Brightness


 The hue-saturation-brightness (HSB) color model is an alternative to red-
green-blue (RGB) for specifying particular colors.
 Figuratively, hue is a wheel of color.
 The hue is specified with a number between 0.0 and 1.0 (the colors are
approximately: red, orange, yellow, green, blue, indigo, and violet).
 Saturation is another scale ranging from 0.0 to 1.0, representing light
pastels to intense hues.
 Brightness values also range from 0.0 to 1.0, where 1 is bright white and
0 is black.
 Color supplies two methods that let you convert between RGB and HSB.
 They are shown here:
static int HSBtoRGB(float hue, float saturation, float brightness)
static float[ ] RGBtoHSB(int red, int green, int blue, float
values[ ])

 HSBtoRGB( ) returns a packed RGB value compatible with the


Color(int) constructor.
 RGBtoHSB( ) returns a float array of HSB values corresponding to RGB
integers.
 If values is not null, then this array is given the HSB values and returned.
 Otherwise, a new array is created and the HSB values are returned in it.
 In either case, the array contains the hue at index 0, saturation at index 1,
and brightness at index 2.

getRed( ), getGreen( ), getBlue( )


 You can obtain the red, green, and blue components of a color
independently using getRed( ), getGreen( ), and getBlue( ), shown here:

int getRed( )
int getGreen( )
int getBlue( )
 Each of these methods returns the RGB color component found in the
invoking Color object in the lower 8 bits of an integer.

getRGB( )

 To obtain a packed, RGB representation of a color, use getRGB( ),


shown here:
int getRGB( )
 The return value is organized as described earlier.

Setting the Current Graphics Color


 By default, graphics objects are drawn in the current foreground color.
 You can change this color by calling the Graphics method setColor( ):
void setColor(Color newColor)

 Here, newColor specifies the new drawing color.


 You can obtain the current color by calling getColor( ), shown here:
Color getColor( )
Demonstration Applet
The following applet constructs several colors and draws various objects using
these colors:
// Demonstrate color.
import java.awt.*;
import java.applet.*;
/*
<applet code="ColorDemo" width=300 height=200>
</applet>
*/
public class ColorDemo extends Applet {
// draw lines
public void paint(Graphics g) {
Color c1 = new Color(255, 100, 100);
Color c2 = new Color(100, 255, 100);
Color c3 = new Color(100, 100, 255);

g.setColor(c1);
g.drawLine(0, 0, 100, 100);
g.drawLine(0, 100, 100, 0);
g.setColor(c2);
g.drawLine(40, 25, 250, 180);
g.drawLine(75, 90, 400, 400);
g.setColor(c3);
g.drawLine(20, 150, 400, 40);
g.drawLine(5, 290, 80, 19);
g.setColor(Color.red);
g.drawOval(10, 10, 50, 50);
g.fillOval(70, 90, 140, 100);
g.setColor(Color.blue);
g.drawOval(190, 10, 90, 30);
g.drawRect(10, 10, 60, 50);
g.setColor(Color.cyan);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
}
}

You might also like