Frames in JAVA: Opening A Window

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Frames in JAVA

Opening a Window
So far, we've just written programs that run right from the shell, taking text input from the
keybord and outputting text to the shell window. Most programs won't work like this and will
instead open their own window. A simple Java object to use is Frame, in the java.awt package.
The basic Frame object has methods for doing things, but a default constructor that doesn't draw
anything, not even an empty frame. To make a Frame that does something we use the extends
keyword like so:
import java.awt.*;
public class FrameTest extends Frame
{
// here goes the extra stuff FrameTest does or has
// that Frame doesn't
}

This means that every FrameTest is a Frame, and so has everything a Frame has and possibly
more, and is everything a Frame is and possibly more. Below is a sample FrameTest that has two
integers to represent its width and height and performs three functions:

A Sample Frame
import java.awt.*;
import java.awt.event.*;
public class FrameTest extends Frame
{
private int w = 400,
h = 300;
// ctor that does something
// (overrides do-nothing Frame ctor)
public FrameTest()
{
super("FrameTest"); // call Frame ctor, telling what to label the
frame
// without this, you won't be able to close the frame
this.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e ) { System.exit(0); }
});

setBackground( Color.black );
setSize(w,h);
show();

// how to draw itself


// (overrides Frame's do-nothing paint behavior)
public void paint(Graphics g)
{
g.setColor(Color.red);
// set pen to red
g.drawLine(0,0,w,h);
// draw an x
g.drawLine(0,h,w,0);
g.setColor(new Color(200,50,255)); // set pen to purplish

// draw concentric ovals round the center


for (int x = 10; x < w/2 && x < h/2; x+=10)
{
g.drawOval(x,x,w-2*x,h-2*x);
}

// to run from the command line


public static void main(String[] args)
{
FrameTest tf = new FrameTest();
}
}

The Graphics Object


When we call show() in our FrameTest constructor, the frame draws itself, (the border, the little
x-button etc..) then calls the paint method, which we defined to draw the big x and the ovals. It
passes paint a Graphics object. (java.awt.Graphics) This object represents what is called a
graphics context, which in this case is just the applet's space for you to draw into and a bunch of
operations you can use to do this drawing. The Graphics class has a lot of methods, but we'll just
look at the most basic functionality here.
Drawing in the graphics context is done on a grid. On this grid, one unit is equal to one pixel.
The origin is in the upper left corner, right is positive x and down is positive y. The size of the
grid is the size (width and height) of the applet that you specify in the applet tag of the html file.
If you want to find out what they are in your java code, you can call the Applet's getSize()
method (Applet inherits this method from a superclass, component), which returns a Dimension
object, which has height and width as public members. That might sound like a pain , but really
it's just:
int width = getSize().width;
int height = getSize().height;

The Graphics object has basic methods for doing things like drawing lines, rectangles and ovals
(which includes circles, of course), as well as drawing text. Here are some of them:

setColor(Color c)
used to set the current Color for drawing. There are some Color constants you can use,
like Color.red, or you can specify the red, green and blue of the color in the range 0-255,
like setColor(new Color(0,128,128)) for a dull cyan. Look at the description of the Color
class for more details on colors.
drawLine(int x1, int y1, int x2, int y2)
draw a line from (x1,y1) to (x2,y2) using the current color
drawString(String str, int x1, int y1)
draw the text given by str with the graphics context's current color and font
drawRect(int x, int y, int width, int height)
draw a rectangle whose upper left corner is at (x,y) and whose width and height are as
given
drawOval(int x, int y, int width, int height)
draw an oval bounded by the rectangle whose upper left corner is at (x,y) and whose
width and height are as given

You might also like