[go: up one dir, main page]

0% found this document useful (0 votes)
9 views28 pages

Working With Graphics

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

Working With Graphics

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

Working with Graphics

Working with Graphics


• Awt supports a wide range of graphics methods.
• All graphics are drawn relative to window.
• Several drawing methods are given below
1. Drawing Lines:
• Lines are drawn by means of the drawLine() method,
• Syntax :
• Void drawLine(int x1,int y1,int x2,int y2);
• drawLince() displays a line in the current drawing
color that begins at x1, y1 and ends at x2,y2.
• Eg:
import java.awt.*;
import java.applet.*;
<applet code="Lines" width="100" height="100"></applet>
public class Lines extends Applet
{
public void paint(Graphics g)
{
g.drawLine(0,100,100,0);
}
}
2. Drawing Rectangles:
• The drawRect() and fillRect() methods display an outlined and
filled rectangle, respectively.
• Syntax:
• Void drawRect(int top, int left , int width, int height);
• Void fillRect(int top, int left , int width, int height);
Eg:
import java.awt.*;
import java.applet.*;
<applet code=“Rect" width="100" height="100"></applet>
public class Rect extends Applet
{ public void paint(Graphics g)
{
g.drawRect(10,10,60,50);
}
}
3. Drawing Ellipses and Circles:
• To draw an ellipse , use drawOval().
• To fill an ellipse, us fillOval()
• Syntax :
• Void drawOval(int top, int left , int width, int height);
• Void fillOval(int top, int left , int width, int height);
• Eg:

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()

5. Setting the current Graphics color:


• You can set the color of a component by calling the
method setColor(newColor);
• you can obtain the current color by calling the method
getColor();
Setting the Paint Mode
• There are two painting or drawing modes for the
Graphics class:-
– Paint Mode (which is the default mode)
– XOR Mode.
• In Paint Mode, anything we draw replaces whatever is
already on the screen i.e. it overwrites any preexisting
contents.
• If you draw a red rectangle, you get a red rectangle, no
matter what was underneath
• Example to show how paint mode works:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class PaintMode extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillRect(20,20,70,70);
g.setColor(Color.red);
g.fillRect(50,0,50,50);
}
} output
• The behavior of XOR mode is different. XOR stands for
eXclusive-OR. The concept behind XOR mode is that
drawing the same object twice returns the screen to its
original state.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class PaintMode extends Applet
{
public void paint(Graphics g)
{
g.setXORMode(Color.red);
g.fillRect(20,20,70,70);
g.fillRect(20,20,70,70);
}
} output
• The setXORMode(Color xorColor) method changes the
drawing mode to XOR mode.
• Here, xorColor specifies the color that will be XORed to
the window when an object is drawn.
• For each pixel, the new color is determined by an
exclusive-or of the old pixel color, the painting color, and
the xorColor.
• For example, if the old pixel is red, the XOR color is blue,
and the drawing color is green, the end result would be
white.
• To see why, it is necessary to look at the RGB values of
the three colors. Red is (255, 0,0). Blue is (0, 0, 255).
Green is (0, 255, 0). The exclusive or of these three values
is (255, 255, 255), which is white.
Eg: of XORMode
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class PaintMode extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(20,20,70,70);
g.setXORMode(Color.blue);
g.setColor(Color.green);
g.fillRect(40,40,70,70);
}
} output
• setPaintMode ():- The setPaintMode() method puts the
system into paint mode. When in paint mode, any
drawing operation replaces whatever is underneath it.
setPaintMode() is called to return to normal painting
when finished with XOR mode
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class SetXORPaintMode extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(20,20,70,70);
g.setXORMode(Color.blue);
g.setColor(Color.green);
g.fillRect(40,40,70,70);
g.setPaintMode();
g.setColor(Color.black);
g.fillRect(80,80,130,130);
g.setColor(Color.red);
g.fillRect(100,80,100,80);

}
} 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

Int charHeight(char c) Returns the height of c


Int getAscent() Returns the ascent of the font
Int getDescent() Returns the descent of the font
getFont() Returns the font
Int getHeight() Returns the height of a line of text
Int getLeading() Returns the space between lines of text.

You might also like