import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class FirstSample{
public static void main(String[] args){
SketchFrame frame=new SketchFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class SketchFrame extends JFrame{
public SketchFrame(){
setTitle("Michael");
setSize(WIDTH,HEIGHT);
SketchPanel panel=new SketchPanel();
add(panel);
}
public static final int WIDTH=800;
public static final int HEIGHT=600;
}
class SketchPanel extends JPanel{
public SketchPanel(){
begin=new Point(100,100);
lines=new ArrayList<Line2D>();
KeyHandler listener=new KeyHandler();
addKeyListener(listener);
setFocusable(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
for(Line2D l:lines)
g2.draw(l);
}
public void add(int dx,int dy){
Point2D end=new Point2D.Double(begin.getX()+dx,begin.getY()+dy);
Line2D line=new Line2D.Double(begin,end);
lines.add(line);
repaint();
begin=end;
}
private Point2D begin;
private ArrayList<Line2D> lines;
private static final int SMALL_INCREMENT=1;
private static final int LARGE_INCREMENT=5;
private class KeyHandler implements KeyListener{
public void keyPressed(KeyEvent event){
int keyCode=event.getKeyCode();
int d;
if(event.isShiftDown())
d=LARGE_INCREMENT;
else
d=SMALL_INCREMENT;
if(keyCode==KeyEvent.VK_LEFT) add(-d,0);
else if(keyCode==KeyEvent.VK_RIGHT) add(d,0);
else if(keyCode==KeyEvent.VK_UP) add(0,-d);
else if(keyCode==KeyEvent.VK_DOWN) add(0,d);
}
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event){
char keyChar=event.getKeyChar();
int d;
if(Character.isUpperCase(keyChar)){
d=LARGE_INCREMENT;
keyChar=Character.toLowerCase(keyChar);
}
else
d=SMALL_INCREMENT;
if(keyChar=='j') add(-d,0);
else if(keyChar=='l') add(d,0);
else if(keyChar=='i') add(0,-d);
else if(keyChar=='k') add(0,d);
}
}
}