Learning Java
Custom Search

Previous Next

Graphics

The Graphics class found in the java.awt package is used for drawing images or text onto components.

We saw the use of the Graphics class in the discussion on applets. The example below is similar to our previous applet in that we are using the drawString method of the Graphics class to write text directly on the applet.

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class GText extends JApplet {
    public void paint(Graphics g) {
        g.drawString("Here we are at 5 20", 5, 20);
        g.drawString("Here we are at 25 40", 25, 40);
        g.drawString("Here we are at 45 60", 45, 60);
        g.drawString("Here we are at 65 80", 65, 80);
        g.drawString("Here we are at 85 100", 85, 100);
        g.drawString("Here we are at 105 120", 105, 120);
    }
}

Here we see that Graphics g is a parameter in the paint method for the JApplet. In the drawString method we have 3 parameters, a String, and the x and y coordinate on the screen. We use it to place text in different areas of our applet.

Let's not forget that we need HTML code to execute our applet.

<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="GText.class" WIDTH=250 HEIGHT=150>
</APPLET>
</BODY>
</HTML>

In the next example, we use the Graphics method drawImage to display an image file on our applet. The image file in our example is "Sails.jpg". The applet gets its directory location from the method getDocumentBase(). The top-left corner of our image will be at the x,y location of 1,1. The "this" parameters tells us that "this" object (our class GImage) will be notified as the image is drawn.

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class GImage extends JApplet {
    Image picx;

    public void init() {
	picx = getImage(getDocumentBase(), "Sails.jpg");
    }

    public void paint(Graphics g) {
        g.drawImage(picx, 1, 1, this);
        g.drawString("Sailing soothes the soul", 50, 140);
    }
}

We modified the HTML code to pick up this class.

<APPLET CODE="GImage.class" WIDTH="250" HEIGHT="150">

For our next example, we are going to manually draw lines, rectangles and an oval on our applet.

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class GDrawing extends JApplet {

    public void paint(Graphics g) {
        g.clearRect(0, 0, 250, 150);
        g.drawLine(1, 1, 50, 60);
        g.drawLine(50, 60, 50, 120);
	g.setColor(Color.blue);
        g.drawLine(50, 120, 130, 80);
	g.setColor(Color.green);
        g.drawRect(5, 10, 110, 90);
	g.setColor(Color.yellow);
        g.fillRect(15, 20, 90, 85);
	g.setColor(Color.red);
        g.fillOval(65, 75, 20, 20);
    }
}

Of course, you will modify the class name in the HTML code. Remember that the WIDTH and HEIGHT parameters in the HTML limits your drawing area.

<APPLET CODE="GDrawing.class" WIDTH="250" HEIGHT="150">

We used the clearRect method to clear our drawing area before using it. We should have done that in the prior 2 programs. You should have noticed that when you go to other sites from your applet and then return to it, your screen has extraneous garbage on it.

The Graphics methods we used in this program are

    clearRect(int x, int y, int width, int height)
    drawLine(int x1, int y1, int x2, int y2)
    setColor(Color c)
    drawRect(int x, int y, int width, int height)
    fillRect(int x, int y, int width, int height)
    fillOval(int x, int y, int width, int height)

See the java.sun.com documentation on Graphics for more information on available methods.

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved