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.
public void update(Graphics g)
{
paint(g);
}
implements ItemListener
.
Choice colorChoice;
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);
ItemListener
interface):
public void itemStateChanged(ItemEvent ae)
{
if (colorChoice.getSelectedItem() == "Red")
currentColor = Color.red;
repaint();
requestFocus();
}
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.
//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");
Graphics offscreen;
Image image;
init
method
image = createImage(640, 480);
//replace 640 & 480 with your screen dimensions
offscreen = image.getGraphics();
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);
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.