You are on page 1of 15

Chapter 01Introduction to AWT

AWT Classes
AWTEvent AWTEventMulticaster BorderLayout
Button Canvas CardLayout
Checkbox CheckboxGroup
CheckboxMenuItem Choice Color
Component Container Cursor
Dialog Dimension Event
EventQueue FileDialog FlowLayout
Font FontMetrics Frame
Graphics GraphicsDevice
GraphicsEnvironment GridBagLayout GridLayout
Image Insets Label
List MediaTracker Menu
MenuBar MenuComponent MenuItem
MenuShortcut Panel Point
Polygon PopupMenu PrintJob
Rectangle Robot Scrollbar
ScrollPane SystemColor TextArea
TextComponent TextField Toolkit
Window

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -1-


Chapter 01Introduction to AWT

Window Fundamentals

Component

Container

MenuContainer
Window Panel
Interface

Frame Applet

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -2-


Chapter 01Introduction to AWT

Dimension

Fields:

int height
int width

Constructors:

1. Dimension()
2. Dimension(Dimension d)
3. Dimension(int width, int height)
Methods:

1. boolean equals(Object obj)

2. double getHeight()

3. Dimension getSize()

4. double getWidth()

5. void setSize(Dimension d)

6. void setSize(double wid, double hgt)

7. void setSize(int width, int height)

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -3-


Chapter 01Introduction to AWT

Frame Window

Constructors:

1. Frame( )

2. Frame(String title)

Setting the Windows Dimensions

1. void setSize(Dimension newSize)

2. void setSize(int nWidth, int nHeight)

3. Dimension getSize( )

Hiding and showing a Window

void setVisible(boolean visibleFlag)

Setting a Windows Title

void setTitle(String newTitle)

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -4-


Chapter 01Introduction to AWT

Creating a Frame within Applet


/*
<applet code="AppletFrame" width=300 height=50>
</applet>
*/
class SampleFrame extends Frame
{
SampleFrame(String title)
{
super(title);
}
public void paint(Graphics g)
{
g.drawString("This is in frame window", 10, 40);
}
}
public class AppletFrame extends Applet
{
Frame f;
public void init()
{
f = new SampleFrame("A Frame Window");
f.setSize(250, 250);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
public void stop()
{
f.setVisible(false);
}
public void paint(Graphics g)
{
g.drawString("This is in applet window", 10, 20);
}
}

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -5-


Chapter 01Introduction to AWT

Output:

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -6-


Chapter 01Introduction to AWT

A Windowed Program

public class AppW extends Frame


{
AppW(String title)
{
super(title);
}
public void paint(Graphics g)
{
setForeground(Color.red);
setBackground(Color.cyan);
g.drawString("This is my frame", 30, 70);
}
public static void main(String args[])
throws Exception
{
AppW appwin = new AppW("Frame window");
appwin.setSize(new Dimension(300, 200));
appwin.setVisible(true);
Thread.sleep(5000);
appwin.setTitle("An AWT-Based Application");
Thread.sleep(5000);
System.exit(0);
}
}

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -7-


Chapter 01Introduction to AWT

Color

Constructors:
Color(int red, int green, int blue)
Color(int rgbValue)
Color(float r, float g, float b)

e.g.
new Color(255, 100, 100);//light red.

Using Hue, Saturation, and Brightness

static int HSBtoRGB(float hue, float sat, float bright)


static float[ ] RGBtoHSB(int r, int g, int b, float values[ ])

int getRed( )
int getGreen( )
int getBlue( )
int getRGB( )

Setting the Current Graphics Color


void setColor(Color newColor)
Color getColor( )

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -8-


Chapter 01Introduction to AWT

Using Color
/*
<applet code="ColorDemo" width=300 height=200>
</applet>
*/
public class ColorDemo extends Applet
{
public void paint(Graphics g)
{
Color c1 = new Color(202, 146, 20);
Color c2 = new Color(110, 169, 107);
Color c3 = new Color(160, 100, 200);

g.setColor(c1);
g.drawLine(0, 0, 100, 100);
g.drawLine(0, 100, 100, 0);

g.setColor(Color.red);
g.drawLine(40, 25, 250, 180);

g.setColor(c3);
g.drawLine(20, 150, 400, 40);

g.setColor(c2);
g.drawOval(10, 10, 50, 50);
g.fillOval(70, 90, 140, 100);
}
}

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. -9-


Chapter 01Introduction to AWT

Font
Variables:
Variable Meaning
String name Name of the font
float pointSize Size of the font in points
int size Size of the font in points
int style Font style

Methods:
static Font decode(String str)
boolean equals(Object FontObj)
String getFamily( )
String getFontName()
String getName( )
int getSize( )
int getStyle( )
int hashCode( )
boolean isBold( )
boolean isItalic( )
boolean isPlain( )
String toString( )

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. - 10 -


Chapter 01Introduction to AWT

Determining the Available Fonts


1. String[ ] getAvailableFontFamilyNames( )
2. Font[ ] getAllFonts( )
3. static GraphicsEnvironment getLocalGraphicsEnvironment( )

/*
<applet code="ShowFonts" width=550 height=60>
</applet>
*/
import java.applet.*;
import java.awt.*;
public class ShowFonts extends Applet
{
public void paint(Graphics g)
{
String msg = "";
String FontList[];
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
FontList = ge.getAvailableFontFamilyNames();
for(int i = 0; i < FontList.length; i++)
msg += FontList[i] + " ";
g.drawString(msg, 4, 16);
}
}

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. - 11 -


Chapter 01Introduction to AWT

Creating and Selecting a Font


Font(String fontName, int fontStyle, int pointSize)
Font Name:
• Dialog
• Sans Serif
• DialogInput
• Serif
• Monospaced
• Symbol

Font Style:
Font.PLAIN
Font.BOLD
Font.ITALIC

For combining styles, we can OR them together.


For example, Font.BOLD | Font.ITALIC

Setting the font:


void setFont(Font fontObj)

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. - 12 -


Chapter 01Introduction to AWT

Displaying different fonts


public class Fonts extends Applet
{
public void paint(Graphics g)
{
Font f1 = new Font("TimesRoman", Font.PLAIN, 18);
Font f2 = new Font("Courier", Font.BOLD, 16);
Font f3 = new Font("Arial", Font.ITALIC, 20);
Font f4 = new Font("Times", Font.BOLD | Font.ITALIC, 22);
g.setFont(f1);
g.drawString("Times Roman plain font: 18", 10, 30);
g.setFont(f2);
g.drawString("Courier bold font: 16", 10, 60);
g.setFont(f3);
g.drawString("Arial italic font: 20", 10, 80);
g.setFont(f4);
g.drawString("Times bold italic font: 22", 10, 120);
}
}

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. - 13 -


Chapter 01Introduction to AWT

Display font information


public class FontInfo extends Applet
{
public void paint(Graphics g)
{
Font f = g.getFont();
String fontName = f.getName( );
String fontFamily = f.getFamily( );
int fontSize = f.getSize( );
int fontStyle = f.getStyle( );

String msg = "Family: " + fontName;


msg += ", Font: " + fontFamily;
msg += ", Size: " + fontSize + ", Style: ";
if((fontStyle & Font.BOLD) == Font.BOLD)
msg += "Bold ";
if((fontStyle & Font.ITALIC) == Font.ITALIC)
msg += "Italic ";
if((fontStyle & Font.PLAIN) == Font.PLAIN)
msg += "Plain ";

g.drawString(msg, 4, 16);
}
}

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. - 14 -


Chapter 01Introduction to AWT

Advanced Java Programming (TYIF) 2008-2009 by Kute T. B. - 15 -