Week 8: Java GUI Part I

 

Overview

 

Frames and Windows

import java.awt.*;
import java.awt.event.*;

public class CloseableFrame extends Frame 
{  public CloseableFrame()
   {  addWindowListener(new WindowAdapter() { public void
         windowClosing(WindowEvent e) { System.exit(0); } } );
      setSize(300, 200);
      setTitle(getClass().getName());
   }
}
import java.awt.*;
public FirstFrame extends CloseableFrame
{
	public static void main(String [] args)
	{
		FirstFrame f = new FirstFrame();
		f.show();
	}
}

 

Display Information in a Window

Example:

import java.awt.*;

public class myAWT extends CloseableFrame
{
	public void paint(Graphic g)
	{
		g.drawString("This is a line of text", 75, 100);
	}
	public static void main(String[] args)
	{
		Frame f = new myAWT();
		f.show();
	}
}

 

Text and Fonts

Example:

public void paint(Graphic g)
{
	Font f = new Font("ScansSerif", Font.BOLD, 14);
	g.setFont(f);
	g.drawString("This is a line of text", 75, 100);
}

 

Colors

Example:

f.setColor(Color.pink);
f.drawString("Hello", 75, 100);
f.setColor(new Color(0, 128, 128)); // some kind of blue and green
f.drawString("www.openloop.com", 75, 125);

f.setBackground(SystemColor.window); // set bg color of frame to default color used by all windows on desktop

 

Drawing

Example:

      // Make sure we start in the left upper corner without the window system overhead
      g.translate(getInsets().left, getInsets().top);
      int r = 40; // radius of circle bounding PacMan(R)
      int cx = 50; // center of that circle
      int cy = 100;
      int angle = 30; // opening angle of mouth
      
      int dx = (int)(r * Math.cos(angle * Math.PI / 180));
      int dy = (int)(r * Math.sin(angle * Math.PI / 180));
      
      // Draw the "PackMan" shape
      g.drawLine(cx, cy, cx + dx, cy + dy); // lower jaw
      g.drawLine(cx, cy, cx + dx, cy - dy); // upper jaw
      g.drawArc(cx - r, cy - r, 2 * r, 2 * r, angle, 
         360 - 2 * angle); 
 
      // Draw a "Five angles" shape
      Polygon p = new Polygon();
      cx = 150;
      int i;
      for (i = 0; i < 5; i++)
         p.addPoint(
            (int)(cx + r * Math.cos(i * 2 * Math.PI / 5)),
            (int)(cy + r * Math.sin(i * 2 * Math.PI / 5)));
            
      g.drawPolygon(p);

Example:

g.fillRect(0, 0, 80, 30); // x,y,width,height
g.fillOval(0, 100, 80, 30); // x,y,width,height

 

Images

Example:

// Get from local file system
String filename = "ball.gif";
Image myimage = Toolkit.getDefaultToolkit().getImage(filename);

// Get from the internet
URL myurl = new URL("http://www.openloop.com/myimage.gif");
Image myimage = Toolkit.getDefaultToolkit().getImage(myurl);

// ...
// Draw out the image
public void paint(Graphics g)
{
	g.drawImage(myimage, 0, 0, this);
}

 

User Interface Components - Layout Manager

 

Java UI Components

 

Button

// Create a button
Button myButton = new Button("OK");
// Set the layout manager
// FlowLayout will automatically arrange the item
setLayout(new FlowLayout());
// Add the button
add(myButton);
// Add the event handler, next week
myButton.addActionListener(this);

 

Panel

Example:

public FirstPanel()
{
	Panel p = new Panel();
	p.add(button1);
	p.add(button2);
	add(p, "South");
}

 

Canvases

class myCan extends Canvas
{
	public void paint (Graphics g)
	{
		g.drawOval(0, 0, 50, 50);
		...
	}
	...
}
...
// In other file
...
hi = new myCan();
add(hi, "Center");
...

 

Text field

// Declare the new TextField
TextField age = new TextField("25", 20);

// Set the value
age.setText("30");
...
// Get the value 
String internalAgeV = age.getText().trim();
// Change to integer value
int agei = Integer.parseInt(age.getText().trim());

 

Text Areas

 

Labels

p.add(new Label("Options");

 

Check Box

// Create one checkbox
Checkbox c = new Checkbox(name);
c.addItemListener(this);
p.add(c);

 

Check Box Groups

// Create the Choice box and add two items within that
style = new Choice();
style.add("Option 1");
style.add("Option 2");

// insert or remove element
style.insert("Option X", 0); // Add at the beginning
...
// Remove stuffs
style.remove("Option 2");
style.remove(0); // Remove whatever in index 0

 

List

states = new List(4, true); // List of 4 items, allow mult selection
states.add("State 1");
states.add("State 2");

...
// Add and select some items
states.add("State X");
states.select(0); // select index 0
states.select(1); // select index 1

 

Lab

Create a Java application Week 8 that has the look and feel of the following:

Note:

 

 

 

Copyright 1996-2001 OpenLoop Computing. All rights reserved.