Generic Applet  

 

 

 

examples of applets

Tute14 (2270 bytes)Tute14.1 (2591 bytes)

Tute14.4 (2374 bytes)Tute14.3 (2819 bytes)

 

Yin-yang

   // Post: a ying-yang icon has been drawn on the screen.
   public void paint(Graphics g) {
      g.fillArc(0,0,100,100,270,180);
      g.fillOval(25,0,50,50);
      g.setColor(Color.white);
      g.fillOval(25,50,50,50);
      g.setColor(Color.black);
      g.drawOval(0,0,100,100);
   }

 

example 1

import java.applet.Applet;
import java.awt.*;

public class TheApplet extends Applet{

public void init( ) {

setBackground(Color.yellow);

} // end of init()

public void paint(Graphics g) {

g.setColor(Color.black);

g.drawString("Name: Sammy Q. Student", 20, 20);
g.drawString("Hometown: Barcelona, Spain", 20, 40);
g.drawString("Class: Junior", 20, 60);
g.drawString("Major: Art History", 200, 60);
g.drawString("My Favorite Movie: The Fifth Element", 20, 100);
g.drawString("My Favoirte Musical Group: London Philharmonic", 20, 120);
g.drawString("Favorite Food: Pizza with Guacamole and Pineapple", 20, 140);
g.drawString("Favorite Sports Team: Los Angeles Clippers", 20, 160);
g.drawString("Sign of the Zodiac: Pisces", 20, 200);
g.drawString("Personality in one word: Introverted", 200, 200);

} // end of paint()

} // end of TheApplet

 

example 2

import java.applet.*;
import java.awt.Graphics;

public class MultiRect extends Applet{
    public void paint(Graphics g){
        g.drawRect(10, 10, 30, 30);
        g.drawRect(20, 20, 10, 10);
        g.drawRect(50, 20, 30, 10);
        g.drawRect(60, 10, 10, 30);
        g.drawRect(90, 30, 10, 10);
        g.drawRect(100, 10, 10, 10);
        g.drawRect(110, 30, 10, 10);
    }
}

example 3

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

public class drawlines extends Applet {
public void paint ( Graphics g )
{
    int y;
    for ( int x = 0 ; x < 150 ; x++ )
{
        y=2*x+10;
        g.drawString ( "." , x , y );
    }
}
}

 

example 4

import java.applet.*;
import java.awt.Graphics;

public class comet extends Applet{
    public void paint(Graphics g){
        g.drawOval(20, 10, 100, 80);
        g.fillOval(90, 40, 20, 20);
        g.fillOval(113, 30, 6, 6);
    }
}

example 5

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

public class mywind extends java.applet.Applet{
public void paint(Graphics g){
int x[] = { 1, 6, 11, 16, 21, 26, 31, 36, 41, 46};
int y[] = {30, 40, 70, 90, 120, 50, 40, 30, 20, 10};
g.drawLine(10, 10, 100, 100);
g.drawRect(10, 30, 40, 40);
g.drawPolygon(x, y, 10);
g.fillArc(70, 50, 40, 40, 0, 90);
}
public void update(Graphics g)
{
paint(g);
}
}

example 6

// this requires three files ConsoleCanvas, Consolepanel, ConsoleApplet

public class PrintProduct extends ConsoleApplet {

public void program() {

double x,y; // Numbers input by the user.
double prod; // The product, x*y.

console.put("What is your first number? ");
x = console.getlnDouble();
console.put("What is your second number? ");
y = console.getlnDouble();

prod = x * y;
console.putln();
console.put("The product is ");
console.putln(prod);

} // end program()

} // end class PrintProduct

example 7

import java.applet.*;
import java.awt.Graphics;
import java.util.Random;

public class RandomWalk1 extends Applet{
    public void paint(Graphics g){
        int x=100, y=100, xx, yy;
        Random ransuu=new Random();
        g.drawRect(20,20,160,160);
        xx=x+ransuu.nextInt()%10;
        yy=y+ransuu.nextInt()%10;
        while((xx>=20)&&(xx<=180)&&(yy>=20)&&(yy<=180)){
            g.drawLine(x,y,xx,yy);
            x=xx;     y=yy;
            xx=x+ransuu.nextInt()%10;
            yy=y+ransuu.nextInt()%10;
        }
    }
}

 

EXAMPLE 8 -- HOW TO SHOW A PICTURE

 

 

The code for the applet attached below follows:

import java.awt.*;
import java.net.*;
import java.applet.*;

public class rock extends Applet{

    Image i;
    public void init(){
	
	i = getImage(getDocumentBase(), "rockefeller.jpg");

    }

    public void paint(Graphics g){
	int w = getSize().width;
	int h = getSize().height;
	
	int x = (w - 105)/2;
	int y = (h - 155)/2;
	if(x < 0) 
	    x = 0;
	if(y < 0)
	    y = 0;
	
	g.drawImage(i,x+0,y+0,50,75,this);
	g.drawImage(i,x+55,y+0,50,75,this);
	g.drawImage(i,x+0,y+80,50,75,this);
	g.drawImage(i,x+55,y+80,50,75,this);
    }
}

example 9

// Simple applet to display a digital clock.  It has been
// modified from the original - it now switches back
// forth between time and date, like the cloak in the 
// MacOS.
//
// This source code has no warranty - use at your own risk!
//
// B. Sheehan 12/22/96
//
import java.util.*;
import java.awt.*;
public class DateClock extends java.applet.Applet implements Runnable {
   Thread	runner;
   Font		fon = new Font("Times", Font.BOLD, 18);
   FontMetrics	fonmet = getFontMetrics(fon);
   Date		dat = new Date();
   String 	old = dat.toString();
   String	cur;
   boolean      UseTime = true;
   int          startx, starty;
   public void init() {
      old = old.substring(11,20);
      setBackground(Color.black);
   }
   public void start() {
      if (runner == null) {
         runner = new Thread(this);
         runner.start();
      }
   }
   public void stop() {
      if (runner != null) {
         runner.stop();
         runner = null;
      }
   }
   public void run() {
      while (runner != null) {
         try { runner.sleep(1000); }
         catch(InterruptedException e) {}
         repaint();
      }
   }
   public void paint(Graphics g) {
      cur = GetDateOrTime();
      g.setFont(fon);
      int oldx = startx;
      int oldy = starty;
      startx = (this.size().width - fonmet.stringWidth(cur))/2;
      starty = (this.size().height + fonmet.getHeight())/2;
// Blank old string
      g.setColor(getBackground());
      g.drawString(old, oldx, oldy);
// Place string in centre of applet
      g.setColor(Color.white);
      g.drawString(cur, startx, starty);
      old = cur;
   }
   public boolean mouseDown(Event evt, int x, int y) {
      if (UseTime) UseTime = false;
         else UseTime = true;
      return true;
   }
   public void update(Graphics g) {
      paint(g);
   }
   String GetDateOrTime() {
      String result;
      dat = new Date();
      dat.getSeconds();
      result = dat.toString();
      if (UseTime) {
         result = result.substring(11,16);
      } else {
         String theday, themonth, theyear;
         Integer day = new Integer(dat.getDate());
         Integer month = new Integer(dat.getMonth() + 1);
         Integer year  = new Integer(dat.getYear());
// What if it's the year 2000?
         if (year.intValue() > 100)
            year = new Integer(year.intValue()-100);
         if (day.intValue() < 10)
            theday = new String("0" + day);
         else
            theday = day.toString();
         if (month.intValue() < 10)
            themonth = new String("0" + month);
         else
            themonth = month.toString();
         if (year.intValue() == 0)
            theyear = new String("00");
         else if (year.intValue() < 10)
            theyear = new String("0" + year);
         else
            theyear = year.toString();
         result = new String(themonth + "/" + theday + "/" +
                             theyear);
      }
      return result;
   }
}

 

For example, you could use the Java 2D API to display complex charts and graphs that use various line and fill styles to distinguish sets of data, like those shown in the following figure.

 

The Java 2D API also enables you to store and to manipulate image data--for example, you can easily perform image-filter operations, such as blur and sharpen, as shown in the following figure.

 

This trail covers the most common uses of the Java 2D APIs and briefly describes some of the more advanced features. For additional information about using the Java 2D APIs, see the Java 2D Programmer's Guide(outside of the tutorial). Additional sample programs illustrating the Java 2D API features are also available online(outside of the tutorial).