Lesson Nine

Creating basic graphics with Java


The new methods we'll be learning is the paint() and the repaint() methods. The header for the paint() method is public void paint(Graphics g). This method runs when you start your applet. "repaint()" occurs when the window needs to be updated. The program below demonstrates how this works.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class DemoPaint extends Applet implements ActionListener
{
Button pressMe = new Button("Press Me");
public void init()
{
add(pressMe);
pressMe.addActionListener(this);
}
public void paint(Graphics g)
{
System.out.println("in paint method");
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
}

Another helpful graphic method is the drawString() method. With drawString(), you need to send it three agruments: a String (or group of text), an x-axis coordinate, and a y-axis coordinate. As with the program above, you need "public void paint(Graphics g)" to call drawString(). To use drawString() you'd type g.drawString(myText, 50, 50);, which would print whatever is in "myText" at the location of x=50, y=50. Click here to see how drawString() works.

To further add to the appearance of your text, you can use the setFont() and setColor() methods. To create the font you want to use, you need to specify which font you want by typing "Font fontName = new Font("Helvetica", Font.ITALIC, 16)". Helvetica is the font we're using, we'll be printing it in ITALIC, and at 16 points in size. Then you need to call the setFont() method to add the changes. With setColor(), you can use any of the 13 constant Java colors (i.e. black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, or yellow), or you can create your own colors using the red, blue, and green color hues. You'd create your own colos by typing "Color colorName = new Color(125, 5, 255);". Then you can call setColor() to change the colors. Here's how you'd go about adding these changes:

Font fontName = new Font("Helvetica", Font.ITALIC, 16);
public void paint(Graphics g)
{
g.setFont(fontName);
g.setColor(Color.blue);
g.drawString(firstSentence, 60, 100);
g.setColor(Color.green);
g.drawString(secondSentence, 90, 120);
}

If you'd like to see how the changes affected the applet, click here.

You can also change the background color of you graphics by using the setBackground() method. To do this, you'd type this.setBackground(Color.yellow);. This link will show you how the color has changed.

The paint() method creates the Graphics objects for you, but there may be time that you want the graphics to respond to a mouse click or any other ActionPerformed() event. For this, you'd type Graphics gr = getGraphics(); Here's how you can use your new object:

public void actionPerformed(ActionEvent e)
{
Graphics gr = getGraphics();
gr.setColor(Color.blue);
gr.drawString(firstSentence, 60, 100);
gr.setColor(Color.green);
gr.drawString(secondSentence, 90, 120);
}

The next few methods require very little explanation. The drawLine() method uses four coordinates to designate the start and end points. The same is similar with drawRect(), fillRect(), and clearRect() methods, only the four coordinates mark the upper-left corner and the width and height of the rectangles. drawRect() draws an open rectangle, fillRect() draws a solid rectangle, and clearRect() acts like fillRect(), only it uses the current background color. Here's how they work:

import java.applet.*;
import java.awt.*;
public class DemoRectangles extends Applet
{
public void paint(Graphics gr)
{
gr.setColor(Color.red);
setBackground(Color.blue);
gr.fillRect(20,20,120,120);
gr.clearRect(40,40,50,50);
}
}

Here's what the code above produces. You can also draw round rectangles with the drawRoundRect() method. As with the other rectangle method, this method requires the same coordinates plus two more arguments to represent the arc width and height of the rounded corners. Also, you can make solid rounded rectangles with the fillRoundRect()

Question: These are simple. Although they're neat graphics, I want to learn more advanced graphics. Can you do anything else with Java?

Answer: You act as if I was just going to leave you with what you've learned. Next, we'll look at more advanced graphic techniques.


Lesson Eight | Home | Lesson Ten

 

If you have a question about any of the lessons, feel free to ask.

1