import java.awt.*;
public class EventsOld extends Frame
{
public boolean keyDown(Event e, int key)
{
if ((char)key == Event.RIGHT)
{
current_color++;
if (current_color >= colors.length)
current_color = 0;
showCurrentColor();
return true; // We handled the event
}
if ((char)key == Event.LEFT)
{
current_color--;
if (current_color < 0)
current_color = colors.length-1;
showCurrentColor();
return true; // We handled the event
}
return super.keyDown(e, key);
}
public boolean mouseDown(Event e, int x, int y)
{
start = new Point(x, y);
end = new Point(x, y);
if (e.clickCount == 2)
{
Graphics g = getGraphics();
g.setColor(colors[current_color]);
g.drawOval(start.x, start.y, 70, 30);
}
return true; // We handled the event
}
public boolean mouseDrag(Event e, int x, int y)
{
end = new Point(x, y);
Graphics g = getGraphics();
g.setColor(colors[current_color]);
g.drawLine(start.x, start.y, end.x, end.y);
start = new Point(x, y);
return true; // We handled the event
}
public void paint(Graphics g)
{
showCurrentColor();
super.paint(g);
}
public EventsOld()
{
super("EventsOld");
setBackground(Color.red);
resize(300, 200);
show();
}
public static void main(String[] arga)
{
Frame f = new EventsOld();
}
private void showCurrentColor()
{
Graphics g = getGraphics();
g.setColor(colors[current_color]);
g.drawString("Current color", 20, 180);
}
///////////// Data //////////////////
private Point start, end;
private final Color[] colors =
{Color.white, Color.cyan, Color.blue, Color.yellow};
private int current_color = 0;
}
|