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
答案 0 :(得分:0)
getGraphics
,当然也不要尝试自己致电paint
,这不是绘画的工作原理 - 看一下Painting in Swing和Performing Custom Painting来了解绘画是如何运作的以及如何利用它KeyListener
无效的所有问题一样,请使用Key Bindings API代替