import java.awt.*; import java.applet.*; import java.lang.*; public class Widgets extends Applet implements Runnable { Thread t; // thread private Image bufferImage; // buffer image private Graphics buffer; // buffer private int width; // width private int height; // height private String message; // message private Button myButton = new Button("Click Here"); // creates an AWT button (can click the button) private Checkbox myCheckbox = new Checkbox("Yes"); // creates a checkbox (can check/uncheck a square) private Choice myChoice = new Choice(); // creates a choicebox (drop down list to choose from) private List myList = new List(); // creates a listbox (list of items) private TextField myTextbox = new TextField("Blah"); // creates a textbox (can type things in it) private Color bgcolor; private String textstring; public void init() { t = new Thread(this); // create new thread t.start(); // start thread width=size().width; // set width to width of applet height=size().height; // set height to height of applet bufferImage = createImage(width, height); // create buffer image buffer=bufferImage.getGraphics(); // set buffer to buffer image myChoice.addItem("Red"); myChoice.addItem("Blue"); myChoice.addItem("Yellow"); myChoice.addItem("Green"); // place some items in our choice box for the user to choose from bgcolor = Color.black; add(myButton); // places the widget on the applet add(myCheckbox); add(myChoice); add(myList); add(myTextbox); setBackground(Color.black); myButton.reshape(100, 200, 50, 50); // x, y, width, height } public void run() { while(true) { repaint(); try{ t.sleep(5); }catch(InterruptedException e) { ; } } } public void paint(Graphics summer) { buffer.setColor(bgcolor); buffer.fillRect(0,0,width,height); buffer.setColor(Color.red); buffer.drawString(message, 100, 100); summer.drawImage(bufferImage, 0, 0, this); myButton.reshape(100, 200, 50, 50); // x, y, width, height } public void update(Graphics fujitsu) { paint(fujitsu); } public boolean action(Event e, Object obj) { if (e.target == myButton) { message = "Button pressed" + obj; } else if (e.target == myCheckbox) { message = "Checkbox is " + obj; } else if (e.target == myChoice) { message = "Choice " + obj + " is selected"; if (obj == "Red") bgcolor = Color.red; else if (obj =="Blue") bgcolor = Color.blue; else if (obj=="Yellow") bgcolor = Color.yellow; else if (obj == "Green") bgcolor = Color.green; } else if (e.target == myList) { message = "List " + obj + " is selected"; } else if (e.target == myTextbox) { message = "Text is " + obj; textstring = toString(obj); myList.addItem(textstring); } else return false; return true; } }