Lab Notes Twelve: Simple Applets
We investigate how paint works.

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

public class One extends Applet {
    int i = 0; 
    public void paint(Graphics g) {
	i += 1; 
	System.out.println("paint has been called: " + i + " times."); 
    } 
} 
And use a basic HTML file to dispatch the applet.

<html>
  <head>
    <title>All eyes on the mouse!</title>
  </head>
<body>
  <applet code=One.class height=300 width=300>

  </applet>
</body>
</html>
Now let's make paint work harder.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class One extends Applet {
    int i = 0; 
    public void paint(Graphics g) {
	i += 1; 
	System.out.println("paint has been called: " + i + " times."); 
	int width = getWidth(), height = getHeight(); 
	for (int line = 0; line < height; line += 3) {
	    g.drawLine(0, line, width, line); // horizontal line 
	} 
    } 
} 
Now let's introduce some colors, and let's show them.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class One extends Applet {
    int i = 0; 
    Color[] c = { Color.red    , Color.yellow, Color.blue , Color.pink, 
		  Color.magenta, Color.green , Color.black, Color.white }; 
    public void paint(Graphics g) {
	i += 1; 
	System.out.println("paint has been called: " + i + " times."); 
	int width = getWidth(), height = getHeight(); 
	int j = 0; 
	for (int line = 0; line < height; line += 3) {
	    g.setColor(c[j % c.length]); 
	    g.drawLine(0, line, width, line); 
	    j += 1; 
	} 
    } 
} 
Now let's paint in one color, but each time another one.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class One extends Applet {
    int i = 0; 
    Color[] c = { Color.red    , Color.yellow, Color.blue , Color.pink, 
		  Color.magenta, Color.green , Color.black, Color.white }; 
    public void paint(Graphics g) {
	i += 1; 
	System.out.println("paint has been called: " + i + " times."); 
	int width = getWidth(), height = getHeight(); 

	for (int line = 0; line < height; line += 3) {
	    g.setColor(c[i % c.length]); 
	    g.drawLine(0, line, width, line); 

	} 
    } 
}
Now it is easy to prove that paint is actually optimized.

It only refreshes that part of the screen that has just become visible.

Now we add circles.

import java.awt.*; 

public class Circle {
    int x, y, radius; 
    public Circle(int x, int y, int radius) {
	this.x = x; this.y = y; this.radius = radius; 
    } 
    public void moveTo(int x, int y) { // x, y coordinates of center 
	this.x = x - radius; 
	this.y = y - radius; 
    } 
    public void draw(Graphics g) {
	g.drawOval(x, y, 2 * radius, 2 * radius); 
    } 
} 
And we draw them.
import java.applet.*;
import java.awt.*;

public class One extends Applet {
    Circle c = new Circle(20, 20, 10); 
    public void paint(Graphics g) {
	c.draw(g); 
    } 
} 
Now let's pay attention to mouse movement.

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

public class One extends Applet implements MouseMotionListener {
    Circle c = new Circle(20, 20, 10); 
    public void init() {
	addMouseMotionListener(this); 
    } 
    public void paint(Graphics g) {
	c.draw(g); 
    } 
    public void mouseMoved(MouseEvent e) {
	int x = e.getX(), y = e.getY();
	c.moveTo(x, y); 
	repaint(); 
    }
    public void mouseDragged(MouseEvent e) {

    }
} 
Notice that you can leave the circle behind while dragging.

As anticipated here is:

A201/A597 LAB ASSIGNMENT TWELVE

Lab assignment 12: Take a look at the pictures below. Choose one that you like. Write an applet that draws that picture. The lab notes on Thursday this week will contain this lab assignment.


Last updated: Jul 25, 2001 by Adrian German for A201