Code sample: java

The java language from Sun Microsystems originally was an attempt to make program code independent of the hardware it would use. Java makes the computer look like a generic 'virtual machine' which is the same everywhere (in theory).

So far, I've not used java for anything 'real'; but when it comes to computing, I'll definitely try anything once. This little program was intended to tweak another programmer. If your web browser has java, you can see it in action here.

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

public class twit_keith extends Applet {

	private Font font;
	private Panel gbox;
	private int xcoord;
	private int ycoord;
	private int len;

	private	Color fg;
	private	Color bg;

	private Button c_butt;
	private Button cpp_butt;
	private Button javabutt;

	private String kewlr;
	private String biter;
	private String ruler;
	private String what_we_printed;

	public void init() {

		fg = getColorParameter("foreground");
		bg = getColorParameter("background");

		font = new Font("TimesRoman", Font.BOLD, 48);

		gbox = this;

		if (fg != null) gbox.setForeground(fg);
		else gbox.setForeground(Color.yellow);

		if (bg != null) gbox.setBackground(bg);
		else gbox.setBackground(Color.blue);

		c_butt = new Button(" c ");
		cpp_butt = new Button("c++");
		javabutt = new Button("java");

		c_butt.setForeground(Color.black);
		c_butt.setBackground(Color.white);

		cpp_butt.setForeground(Color.black);
		cpp_butt.setBackground(Color.green);

		javabutt.setForeground(Color.black);
		javabutt.setBackground(Color.red);

		this.add(c_butt);
		this.add(cpp_butt);
		this.add(javabutt);

		kewlr = new String("is kewl!!");
		biter = new String("b1t3z!!");
		ruler = new String("r00lz!!");
		what_we_printed = new String(" ");
		
		xcoord = 80;
		ycoord = 150;

	};

	public boolean action(Event event, Object arg) {
		Graphics g = getGraphics();
		g.setFont(font);
		g.setColor(bg);
		if (event.target == c_butt) {
			g.drawString(what_we_printed,xcoord,ycoord);
			g.setColor(Color.red);
			what_we_printed = (String)arg + " " + kewlr;
			g.drawString(what_we_printed,xcoord,ycoord);
		};
	
		if (event.target == cpp_butt) {
			g.drawString(what_we_printed,xcoord,ycoord);
			g.setColor(Color.yellow);
			what_we_printed = (String)arg + " " + biter;
			g.drawString(what_we_printed,xcoord,ycoord);
		};
	
		if (event.target == javabutt) {
			g.drawString(what_we_printed,xcoord,ycoord);
			g.setColor(Color.white);
			what_we_printed = (String)arg + " " + ruler;
			g.drawString(what_we_printed,xcoord,ycoord);
		};
		return true;
	}

	protected Color getColorParameter(String name) {
		String val = this.getParameter(name);
		int intval;
		try {
			intval = Integer.parseInt(val, 16);
		}
		catch(NumberFormatException e) {
			return null;
		};
		return new Color(intval);
	}
			
}

Return to language index.


A publication of the School of Tyrannus

Copyright © 1998, The School of Tyrannus
All rights reserved