프레임을 생성하고 그래픽을 구현하는 클래스를 보유하는 MainApplication입니다.Keylistener가 작동하지만 의도 한 작업을 실행하지 않습니다. 왜?
MainApplication.java
import java.awt.*;
public class MainApplication
{
public MainApplication()
{
Frame frame= new Frame("Test App");
frame.add(new KeyTest());
frame.setBackground(Color.RED);
frame.setLayout(null);
frame.setSize(700,750);
frame.setVisible(true);
}
public static void main(String args[])
{
new MainApplication();
}
}
이 클래스는 모든 그래픽 모양을 만들고 너무 모든 KeyListener를 구현합니다.
KeyTest.java
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.RoundRectangle2D;
public class KeyTest extends Canvas {
/**
*
*/
private static final long serialVersionUID = 3L;
Graphics2D graphics2D;
Color color = Color.BLACK;
private static float borderThickness = 5;
Shape firstShape = new RoundRectangle2D.Double(20,40,300,50,10,10);
Shape secondShape = new RoundRectangle2D.Double(20,150,300,50,10,10);
public KeyTest()
{
setBackground(Color.RED);
setSize(700,750);
}
public void paint(Graphics graphics)
{
graphics2D = (Graphics2D)graphics; //TypeCasting to 2D
System.out.println("I am inside paint");
// Smoothening the corners
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Apple border color
Stroke oldStroke = graphics2D.getStroke();
graphics2D.setStroke(new BasicStroke(borderThickness));
graphics2D.setColor(Color.WHITE);
// Drawing a RoundedRectangle for Apple
graphics2D.draw(firstShape);
graphics2D.setStroke(oldStroke);
// Setting the Background Color
graphics2D.setColor(Color.BLACK);
graphics2D.fill(firstShape);
// Setting the font inside the shape
Font firstFont = new Font("Serif", Font.BOLD,35);
graphics2D.setFont(firstFont);
graphics2D.setColor(Color.WHITE);
graphics2D.drawString("Apple",30,80);
// Pineapple border color
graphics2D.setStroke(new BasicStroke(borderThickness));
graphics2D.setColor(Color.WHITE);
// Drawing a RoundedRectangle for Pineapple
graphics2D.draw(secondShape);
graphics2D.setStroke(oldStroke);
// Setting the Background Color
graphics2D.setColor(Color.BLACK);
graphics2D.fill(secondShape);
// Setting the font inside the shape
Font secondFont = new Font("Serif", Font.BOLD,35);
graphics2D.setFont(secondFont);
graphics2D.setColor(Color.WHITE);
graphics2D.drawString("Pineapple",30,190);
addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
System.out.println(KeyEvent.VK_UP);
if(keyCode==KeyEvent.VK_UP){
System.out.println("Going to move up");
move(firstShape);
}
if(keyCode==KeyEvent.VK_DOWN){
System.out.println("Going to move down");
move(secondShape);
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}});
}
public void move(Shape s){
System.out.println("Check:"+s.getBounds2D());
graphics2D.setColor(Color.GREEN);
graphics2D.fill(s);
System.out.println("moving out");
}
}
내 콘솔 출력 명확하게 보여줍니다 내 키 리스너 작동하지만 내가 그것을 할 의도 작업을 실행하지.
콘솔 출력
나는 페인트
확인 이동하기 위해가는 내부입니다 : java.awt.geom.Rectangle2D $ 더블 [X = 20.0, y를 = W, 40.0 = 300.0 단, h =]
50.0
체크를 움직이려고 이사 : $ java.awt.geom.Rectangle2D 더블 [X = 20.0, Y, W = 300, 150.0를 =.
,536 : 0, H = 50.0]는
OUTPUT 이사 지금은 무엇입니까
아웃풋 .. (IMAGE 1)
내가 화살표 버튼을 누를 때 예상 출력 (IMAGE 2)
내가 화살표 버튼을 누를 때 예상 아웃풋 (IMAGE 3)
아마도 프레임에서 다시 칠하기/새로 고침을 트리거해야합니다. 힌트 : 질문에 ** 모든 ** 이미지를 포함시키지 않아도됩니다. – GhostCat
'paint' 메소드 안에'KeyListener'를 추가하는 것이 전혀 좋지 않습니다. – Berger
외부에 추가하면 목적을 달성하지 못했습니다. 그 이유는'paint' 메소드 안에 추가했기 때문입니다. –