Etch-A-Sketch

Compiled By Unknown - No Comments
Modify the game so that the circle can move left and right instead of just up and down.
Make the circle a bit smaller, and make it move 10 pixels at a time instead of 5.
Make it so that if you press F1, the current color changes to red, F2 changes it to green, F3 changes to blue, and F5 resets to black.
Use following code name it EtchASketch.java and compile
 import java.awt.*;  
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class EtchASketch extends Canvas
{
int x, y;
Color cur;
public static void main( String[] args )
{
JFrame win = new JFrame("Use the arrow keys!");
win.setSize(1024,768);
win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
win.add( new EtchASketch() );
win.setVisible(true);
}
public EtchASketch()
{
enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK);
requestFocus();
x = 500;
y = 400;
cur = Color.black;
}
public void paint( Graphics g )
{
g.setColor(cur);
g.fillOval(x, y, 50, 50);
}
public void update( Graphics g )
{
paint(g);
}
public void processKeyEvent(KeyEvent e)
{
// this method automatically gets called with they press a key
if ( e.getID() == KeyEvent.KEY_PRESSED )
{
if ( e.getKeyCode() == KeyEvent.VK_UP )
y -= 5;
if ( e.getKeyCode() == KeyEvent.VK_DOWN )
y += 5;
// and we manually call paint() again to redraw
repaint();
}
}
public boolean isFocusable()
{
return true;
}
}

Tags:

No Comment to " Etch-A-Sketch "