1 import java
.awt
.Container
;
2 import java
.awt
.Graphics
;
4 import java
.awt
.event
.KeyEvent
;
5 import java
.awt
.event
.KeyListener
;
6 import java
.awt
.event
.WindowAdapter
;
7 import java
.awt
.event
.WindowEvent
;
9 import javax
.swing
.JFrame
;
10 import javax
.swing
.JPanel
;
12 public class SketchPanel
extends JPanel
implements KeyListener
{
14 private Point startPoint
= new Point(0, 0);
16 private Point endPoint
= new Point(0, 0);
18 public SketchPanel() {
22 public void keyPressed(KeyEvent evt
) {
23 int keyCode
= evt
.getKeyCode();
25 if (evt
.isShiftDown())
29 if (keyCode
== KeyEvent
.VK_LEFT
)
31 else if (keyCode
== KeyEvent
.VK_RIGHT
)
33 else if (keyCode
== KeyEvent
.VK_UP
)
35 else if (keyCode
== KeyEvent
.VK_DOWN
)
39 public void keyReleased(KeyEvent evt
) {
42 public void keyTyped(KeyEvent evt
) {
45 public boolean isFocusTraversable() {
49 public void add(int dx
, int dy
) {
52 Graphics g
= getGraphics();
53 g
.drawLine(startPoint
.x
, startPoint
.y
, endPoint
.x
, endPoint
.y
);
55 startPoint
.x
= endPoint
.x
;
56 startPoint
.y
= endPoint
.y
;
59 public static void main(String
[] args
) {
60 JFrame frame
= new JFrame();
61 frame
.setTitle("Sketch");
62 frame
.setSize(300, 200);
63 frame
.addWindowListener(new WindowAdapter() {
64 public void windowClosing(WindowEvent e
) {
69 Container contentPane
= frame
.getContentPane();
70 contentPane
.add(new SketchPanel());
This page took 1.348472 seconds and 4 git commands to generate.