Java Assignment 9 -- Etching and Sketching Tablet


Press the number pad to move the curser:
8 for up, 2 for down, 4 for left, 6 for right
Change colors and line thickness with the pull down menus, refresh to clear

This assignment asks you to write an applet that replicates another classic toy from the 1960’s the "Etch a Sketch". Your electronic "Etch a Sketch" will add several features the original lacked: The ability to change the color and size of the line as well as "mirrors" that add a symmetrical reflection.

Overriding update

To keep the screen from erasing the line as the etch a sketch draws it, we will need to override the update function with the following code in the applet class:

public void update(Graphics g)
{
		paint(g);
}

Pull Down “Choice” Menus

To add pull down menus, you will need to add a component to your applet called a “Choice”. One way to do this is to add to your applet
implements ItemListener.
In your applet you might then declare
Choice colorChoice;
In the init method you could initialize and add the various items to the pulldown menu:

colorChoice = new Choice();
colorChoice.addItem("White");
You add and position the Choice box just as you would a button. To activate the choice add
colorChoice.addItemListener(this);
You will also need to add a method to handle the events the choice box generates (this is required when you use the ItemListener interface):

public void itemStateChanged(ItemEvent ae) 
{
          if (colorChoice.getSelectedItem() == "Red")
              currentColor = Color.red;
          repaint();
          requestFocus();
}

Handling Keyboard Events

Your program will need to get user input from the keyboard. One way to do this is for your applet to use a KeyListener interface. To implement more than one interface just separate the interface names with commas. To implement the KeyListener interface you will need to add the following three methods to your applet:

public void keyTyped(KeyEvent ke)
{
		cKey = ke.getKeyChar();
		switch(cKey){
			case '8':
				nY--;
				break;
		//and so on
		repaint();				
}
//While the following methods are declared, you may want to just leave them as empty "stubs".	
public void keyPressed (KeyEvent ke)
{
}
public void keyReleased (KeyEvent ke)
{
}
To process the key events, you will need to invoke requestFocus(); once in init() and after any other type of event is processed. Just like any other listener, you will want to addKeyListener(this) in your applets init() method.

Radio Buttons

Radio Buttons are a variation of a Java component called a checkbox. First, you need to declare a checkbox group and then you declare the individual checkboxes that make up the group. For instance:

//declare and initialize a checkbox group to handle turning the mirrors //on and off
CheckboxGroup mirrorsGroup = new CheckboxGroup();

//declare and initialize each radio button in the group
Checkbox mirrorsOn = new Checkbox("Mirrors On", mirrorsGroup, true);
//where "Mirrors On" is the label, mirrorsGroup is the group it
//belongs to and true is it initial ("checked") state. 
Once you have declared the group and the radio buttons that make it up, you can add them to the applet, add anItemListener, set the bounds, set the background color much as you would with a choice box. The getState() will return a boolean for the state of the button: true if it has been checked, false otherwise. For example:

if(mirrorsOn.getState())
    System.out.print("The mirrors on radio button has been checked");

Optional: Double Buffering

If you have extra time, you may want to implement an advanced technique called "double buffering". In double buffering, you create a second "offscreen" image. All drawing is done to the offscreen image, and then copied all at once to the visible screen. This solves two problems: flicker, and in the case of this assignment, erasing part of the visible image when the screen is redrawn. To implement double buffering you will need:
  1. Two additional declarations in applet class
    Graphics offscreen;
    Image image;
  2. Two added initializations in the init method
    image = createImage(640, 480);
    //replace 640 & 480 with your screen dimensions
    offscreen = image.getGraphics();
  3. Modify code in paint method to paint offscreen by changing g to offscreen. For example
    g.setColor(CurrentColor);
    g.fillArc(nX, nY, nSize, nSize, 0, 360);
    should be
    offscreen.setColor(CurrentColor);
    offscreen.fillArc(nX, nY, nSize, nSize, 0, 360);
  4. Add one line of code on last line of the paint method that copies offscreen to the visible screen
    g.drawImage(image,0,0,this);

You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.

Submit the URL of your applet along with your Java code (the .java file(s)) in an email message to mrsimon@lycos.com.