利用键盘画线

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DemoForDrawLineByKey extends JFrame {

/**
  * <p>功能描述:[字段功能描述]</p>
  */
private static final long serialVersionUID = -8155745524182056025L;
private ArrayList<MyPoint> pointArrayList;
private int intNowX = 10;
private int intNowY = 10;
private MyPanelForDrawLine panel;
private int oldCode = 0;
private int movePx = 3;
private JPanel mainpanel;
/**
  * Launch the application
  *
  * @param args
  */
public static void main(String args[]) {
  DemoForDrawLineByKey frame = new DemoForDrawLineByKey();
  frame.setBounds(100, 100, 500, 375);
  frame.setVisible(true);
  frame.init();
}
public void init() {
  panel.requestFocusInWindow();
}
/**
  * Create the frame
  */
public DemoForDrawLineByKey() {
  super();
  setTitle("利用键盘画线");
  pointArrayList = new ArrayList<MyPoint>();
  getContentPane().setLayout(null);
  setBounds(100, 100, 500, 375);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  mainpanel = new JPanel();
  mainpanel.setBounds(0, 0, 492, 341);
  getContentPane().add(mainpanel);
  mainpanel.setLayout(null);
  panel = new MyPanelForDrawLine();
  panel.setBounds(0, 0, 492, 341);
  panel.requestFocusInWindow();
  mainpanel.add(panel);

}
class MyPanelForDrawLine extends JPanel {
  private static final long serialVersionUID = 3470945714820691208L;
  KeyListenerForPanel mKeyListenerForPanel = new KeyListenerForPanel();
  public MyPanelForDrawLine() {
   super();
   addKeyListener(mKeyListenerForPanel);
  }
  public void paintComponent(Graphics g) {
   super.paintComponents(g);
   g.setColor(Color.WHITE);
   g.fillRect(0, 0, getWidth(), getHeight());
   g.setColor(Color.BLACK);
   drawOldLine(g);
   g.fillRect(intNowX, intNowY, 3, 3);
  }
  public void drawOldLine(Graphics g) {
   if (pointArrayList.size() > 0) {
    Object tmpObj[] = pointArrayList.toArray();
    MyPoint lasePoint = null;
    if (tmpObj.length >= 1) {
     lasePoint = (MyPoint) tmpObj[tmpObj.length - 1];
    }
    for (int i = 0; i < tmpObj.length - 1; i++) {
     MyPoint tmpPoint1 = (MyPoint) tmpObj[i];
     MyPoint tmpPoint2 = (MyPoint) tmpObj[i + 1];
     g.drawLine(tmpPoint1.getPx(), tmpPoint1.getPy(), tmpPoint2
       .getPx(), tmpPoint2.getPy());
    }
    g.drawLine(lasePoint.getPx(), lasePoint.getPy(), intNowX,
      intNowY);
   }
  }
}
class MyPoint {
  int px = 0;
  int py = 0;
  public MyPoint(int x, int y) {
   px = x;
   py = y;
  }
  public int getPx() {
   return px;
  }
  public void setPx(int px) {
   this.px = px;
  }
  public int getPy() {
   return py;
  }
  public void setPy(int py) {
   this.py = py;
  }
}
class KeyListenerForPanel implements KeyListener {
  public void keyPressed(KeyEvent e) {
   int keycodeOfKey = e.getKeyCode();
   if (keycodeOfKey != oldCode) {
    oldCode = keycodeOfKey;
    MyPoint tmpPoint = new MyPoint(intNowX, intNowY);
    pointArrayList.add(tmpPoint);
   } else {
    switch (keycodeOfKey) {

     // 向上
     case KeyEvent.VK_UP :
      intNowY = intNowY - movePx;

      break;
     // 向下
     case KeyEvent.VK_DOWN :
      intNowY = intNowY + movePx;
      break;
     // 向左
     case KeyEvent.VK_LEFT :
      intNowX = intNowX - movePx;
      break;
     // 向右
     case KeyEvent.VK_RIGHT :
      intNowX = intNowX + movePx;
      break;
    }
   }
   panel.repaint();
  }

  public void keyReleased(KeyEvent e) {
   int keycodeOfKey = e.getKeyCode();
   if (keycodeOfKey != oldCode) {
    oldCode = keycodeOfKey;
    MyPoint tmpPoint = new MyPoint(intNowX, intNowY);
    pointArrayList.add(tmpPoint);
   } else {
    switch (keycodeOfKey) {
     // 向上
     case KeyEvent.VK_UP :
      intNowY = intNowY - movePx;
      break;
     // 向下
     case KeyEvent.VK_DOWN :
      intNowY = intNowY + movePx;
      break;
     // 向左
     case KeyEvent.VK_LEFT :
      intNowX = intNowX - movePx;
      break;
     // 向右
     case KeyEvent.VK_RIGHT :
      intNowX = intNowX + movePx;
      break;
    }
   }
   panel.repaint();
  }
  public void keyTyped(KeyEvent e) {
  }
}

}

你可能感兴趣的:(swing,UP)