-2
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 sssssssssssssssssskeyListenner 및 mousemove
들여 쓰기를 바랍니다 사용 이해합니다. 아무도 이런 식의 코드를 읽지 않아야합니다. – khelwood
1) 애플릿은 더 이상 사용되지 않으므로 사용을 중지해야합니다. 2) 결코 getGraphics를 사용하지 말고 절대로 페인트하지 말고 직접 페인트를 호출하십시오. 이것은 페인팅의 작동 방식이 아닙니다. [Painting in Swing] (http://www.oracle.com/technetwork/java/painting)을보십시오. -140037.html) 및 [사용자 정의 페인팅 수행] (https://docs.oracle.com/javase/tutorial/uiswing/painting/)을 참조하여 페인팅이 어떻게 작동하고 어떻게 사용할 수 있는지 이해하십시오. 3) KeyListener가 작동하지 않는 모든 질문처럼 [Key Bindings API] (https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)를 대신 사용하십시오 – MadProgrammer