The Java Abstract Window Toolkit Package java'awt - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

The Java Abstract Window Toolkit Package java'awt

Description:

... Font - methods and constants to change the font name, font size and font style. Constants. font names - Serif, SansSerif, Dialog, DialogInput, Monospaced ... – PowerPoint PPT presentation

Number of Views:300
Avg rating:3.0/5.0
Slides: 19
Provided by: CS3
Category:

less

Transcript and Presenter's Notes

Title: The Java Abstract Window Toolkit Package java'awt


1
The Java Abstract Window Toolkit Package -
java.awt
  • It contains classes to work with
  • graphics, mouse and keyboard
  • graphics components (Java GUI)
  • events
  • images
  • data transfer
  • We will look at some of the methods and constants
    in the Graphics class

2
Representing Color
  • A black and white picture can be stored using one
    bit per pixel (0 white and 1 black)
  • A colored picture requires more information
  • Every color can be represented as a mixture of
    the three primary colors Red, Green, and Blue
  • In Java, each color is represented by is a triple
    of whole numbers (r, g, b) defining the amount of
    red, green and blue colour called an RGB value
  • Each r, g and b value is a number between 0 and
    256 thus enabling the Java programmer to choose
    from approximately 16 million! colours

3
The Color Class
  • Every drawing surface has a background color
  • Every graphics context has a current foreground
    color
  • Both can be set explicitly

4
Color Control
  • Class Color - methods and constants for
    manipulating colours
  • Colour constants white, black, blue, etc.
  • Defining new colour Color ( r, g, b )
  • Get the colour of a particular pixel getRed( ),
    getGreen( ), getBlue( ), getColor( )
  • Set the colour to draw with setColor ( Color c )
  • For more methods check the Java API documentation

5
Color Constants
6
The Color Class
  • A color is defined in a Java program using an
    object created from the Color class
  • You can use the colour constants by referring to
    the Color class and using the dot operator

Color.black Color.blue Color.cyan Color.orange Co
lor.white Color.yellow
7
Using Colors - Examples
  • Graphics g
  • g.setColor( Color.red )
  • g.setColor( new Color( 0, 64, 255 ))
  • Color color1 Color.green
  • g.setColor( color1 )

8
Font Control
  • Class Font - methods and constants to change the
    font name, font size and font style
  • Constants
  • font names - Serif, SansSerif, Dialog,
    DialogInput, Monospaced
  • font style - Font.BOLD, Font.ITALIC, Font.PLAIN
  • Method to change the Font
  • Font( fontName, fontStyle, fontSize )

9
Font Control - Examples
  • Font font1 new Font ("SansSerif", Font.ITALIC,
    5)
  • Font font2 new Font (Serif", Font.ITALIC
    Font.BOLD, 10)
  • Font font1 new Font (Monospaced", Font.PLAIN,
    24)
  • Font font1 new Font (Dialog", Font.ITALIC,
    10)

10
Co-ordinate System
  • When drawing on the screen we use a co-ordinate
    system
  • In the Applet screen even strings are drawn!
  • Co-ordinate System
  • (0, 0) upper left corner of an applet or window

11
Co-ordinate System - Example
  • import java.applet.Applet
  • import java.awt.Graphics
  • public class StarTriangle extends Applet
  • public void paint( Graphics g )
  • g.drawString( "", 70, 70 )
  • g.drawString( "", 70, 80 )
  • g.drawString( "", 70, 90 )

12
Co-ordinate System - Example
X axis
(0, 0)
70
70
(70, 70)
80
(70, 80)
90
(70, 90)
Y axis
13
Drawing Shapes
  • In the Graphics class you have methods to draw
  • Lines
  • Rectangles
  • Polygons
  • Ovals (cyrcles)
  • Arcs
  • A shape can be filled with colour or unfilled,
    depending on the method invoked
  • The method parameters specify coordinates and
    sizes

14
Drawing Shapes (2)
  • Drawing a shape
  • drawLine( x1, y1, x2, y2 )
  • drawRect( x, y, width, height )
  • drawRoundRec( x, y, width, height,
    arcWidth,arcHeight )
  • drawOval( x, y, width, height )
  • Drawing a solid shape filled with particular
    colour
  • fillRect( x, y, width, height )
  • fillRoundRec( x, y, width, height,
    arcWidth, arcHeight )
  • fillOval( x, y, width, height )

15
Drawing Shapes (2)
  • Shapes with curves, like an oval, are usually
    drawn by specifying the shapes bounding
    rectangle
  • An arc can be thought of as a section of an oval

16
Drawing a Line
17
Drawing a Rectangle
50
20
drawRect (50, 20, 100, 40)
18
Drawing an Oval
150
20
bounding rectangle
drawOval (150, 20, 40, 60)
Write a Comment
User Comments (0)
About PowerShow.com