Working With Graphics
Working With Graphics
g.drawOval(70,90,140,100);
4. Drawing Polygons:
• It is possible to draw arbitrarily shaped figures using
drawPolygon() and fillPolygon()
• Syntax:
• Void drawPolygon(int x[] , int y[] , int numPoints)
• Void fillPolygon(int x[] , int y[] , int numPoints)
Eg:
import java.awt.*;
import java.applet.*;
<applet code=“Poly " width="100" height="100"></applet>
public class Poly extends Applet
{
public void paint(Graphics g)
{
int x[] = {30 , 200 ,30 200 ,30};
int y[] = {30,30,200,200,30};
int num = 5;
g.drawPolygon(xpoints,ypoints ,num);
}
}
5. Sizing Graphics:
• setSize() and getSize() are used to set the size of
the window and to obtain the size of the window
Working with colors
• The AWT color system allows you to specify any
color you want.
• Color is encapsulated by the Color class.
• Colors define several constants such as (Color.black).
• You can also create your own colors using one of the
color constructors
• Color (int red, int green , int blue)
• Color(int rgbValue)
• First, constructor takes 3 integers that specify the
color as a mix of red, green, and blue.
• These values must be between 0 and 255 eg:
• new Color(255 , 100 , 100);
• Second color constructor takes a single integer that
contains the mix of red, green, and blue packed into
an integer.
• Eg: new Color(200);
• Color methods:
1. setBackground():
• You can set the background color by using the
setBackground() method.
2. setForground():
• You can set the foreground color by using the
setForground() method.
3. Using Hue , Saturation and Brightness
• Hue is the actual color. Brightness refers to how much
white (or black) is mixed in the color while Saturation
indicates the amount of grey in a color.
• The HSB color model is an alternative to red-green-blue
for specifying colors
• float[] RGBtoHSB(int red , int green , int blue, float
values[])
• This method converts RGB components to it's equivalent
HSB values.
• int HSBtoRGB(float hue , float saturation , float
brightness)
• This method converts HSB components to it's equivalent
RGB values.
4. getRed() , getGreen() , getBlue():
• You can obtain the red, green, and blue components of a
color independently using getRed(),getGreen(),and
getBlue() method.
• Int getRed()
• Int getGreen()
• Int getBlue()
}
} output
Working with fonts
• Awt supports multiple type fonts.
• The Font class states fonts, which are used to render text
in a visible way.
• Within AWT, a font is specified by its name, style, and
point size.
• Each platform that supports java provides a basic set of
fonts;
• To find the fonts supported on any platform, call
Toolkit.getDefaultToolkit(). getFontList().
• This method returns a string array of the fonts available.
• The standard java font names are Courier, Helvetica,
TimesRoman etc.
• setFont() is used to set a font in the graphics context.
Eg: Design a java applet to display “Thank you” and the font should be Times
new roman and Bold & the font size =24
apps1.html (html file)
import java.awt.*;
<html>
import java.applet.*; <applet code=“apps1.class”
public class apps1 extends Applet width=“100”
{ height=“100’></applet>
public void init( ) </html>
{
output
setBackground(Color.blue);
setForeground(Color.red); Thank You
}
public void paint(Graphics g)
{
Font f = new Font("Times new Roman",Font.BOLD, 24);
g.setFont(f);
g.drawString("Thank You", 100 ,100);
}
• Some of the methods of Font classes are given below
Managing Text Output Using
FontMetrics
• Java supports a number of fonts.
• For most fonts, characters are not all the same dimension,
most fonts are proportional.
• Also, the height of each character, the length of
descenders (the hanging parts of letters, such as y), and the
amount of space between horizontal lines vary from font to
font.
• There must be some way to determine the dimensions and
various other attributes of the currently selected font.
• For more example, to write one line of text after another
implies that you have some way of knowing how tall the
font is and how many pixels are needed between lines. To
fill this need, the AWT includes the FontMetrics class
which encapsulates various information about a font.
• Perhaps the most common use of FontMetrics is to
determine the spacing between lines of test.
• The second most common use is to determine
the length of a string that is being displayed.
• To display multiple lines of text, your program must
manually keep track of the current output position.
• Each time a newline is desired, the Y coordinate must be
advanced to the beginning of the nextline.
• Each time a sting is displayed, the X coordinate must be
set to the point at which the string ends.
• This allows the next string to be written so that it begins at
the end of the preceding one
• FontMetrics defines several methods that help you mana
ge text output. Several commonly used ones are listed in
Table
Method Description
Int charWidth(char c) Returns the width of c