keyListenner和mousemove

时间:2017-05-04 22:10:41

标签: java awt

 import java.applet.Applet;
 import java.awt.Color;
 import java.awt.Event;
 import java.awt.Graphics;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;

public class Drawing extends Applet implements KeyListener {
 /**
  * 
  */

 char c;
 int xCoord, yCoord, xCoord2, yCoord2;
 int lastx = getX();
 int lasty = getY();
 int count = 0;
 public void paint(Graphics g) {


  switch (c) {
   case 'b':
    {
     g.setColor(Color.black);
     break;
    }
   case 'p':
    {
     g.setColor(Color.pink);
    }
  }
  g.drawLine(xCoord, yCoord, xCoord2, yCoord2);
 }
 public void keyPressed(KeyEvent e) {

 }
 public void keyReleased(KeyEvent e) {

 }

 public void keyTyped(KeyEvent e) {
  c = e.getKeyChar();
  System.out.println("check if it run");
  paint(this.getGraphics());

 }
 public boolean mouseMove(Event e, int x, int y) {
  System.out.println("mouse");
  xCoord2 = x;
  yCoord2 = y;
  xCoord = lastx;
  yCoord = lasty;
  lastx = x;
  lasty = y;
  if (xCoord != 0 || yCoord != 0)
   this.paint(getGraphics());

  return true;
 }


}

为什么我的键输入方法没有运行,我想通过键改变颜色,但它没有运行,代码运行良好,但我不能通过输入改变颜色。请帮助我。目标这个程序差不多完成但是我无法改变color.ssss ssssssssssssssssssss

1 个答案:

答案 0 :(得分:0)

  1. 不推荐使用小程序,您应该停止使用它们
  2. 永远不要使用getGraphics,当然也不要尝试自己致电paint,这不是绘画的工作原理 - 看一下Painting in SwingPerforming Custom Painting来了解绘画是如何运作的以及如何利用它
  3. KeyListener无效的所有问题一样,请使用Key Bindings API代替