gpt4 book ai didi

java - 在 Java 中编码图形和 KeyListener

转载 作者:行者123 更新时间:2023-11-29 05:06:59 27 4
gpt4 key购买 nike

我想知道如何在 JAVA 中正确使用图形库和 Keylistener。下面是我的代码,我相信我做错了什么,因为窗口是空白的,没有椭圆形。请帮帮我!

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class Game extends JFrame implements KeyListener{
/**
*
*/
private static final long serialVersionUID = 1L;

int x=100,y=100;

boolean u,d,r,l;

public <addKeyListener> void run(){
setBackground(Color.gray);
setSize(800,800);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
}
public static void main(String[] args) {
Game game = new Game();
game.run();
}

public void paint(Graphics g){
g.setColor(Color.black);
g.fillOval(x,y,40,40);
repaint();
}
{


if(u =true){
y-=200;

}
if(d = true){
y+=2;
}
if(r = true){
x-=2;
}
if(l = true){
x+=2;}
}








@Override
public void keyPressed(KeyEvent e) {
char code = e.getKeyChar();
if(code == KeyEvent.VK_W){
u = true;}
if(code == KeyEvent.VK_A){
l = true;}
if(code == KeyEvent.VK_S){
d = true;}
if(code == KeyEvent.VK_D){
r = true;}

}



@Override
public void keyReleased(KeyEvent e) {
char code = e.getKeyChar();
if(code == KeyEvent.VK_W){
u = false;}
if(code == KeyEvent.VK_A){
l = false;}
if(code == KeyEvent.VK_S){
d = false;}
if(code == KeyEvent.VK_D){
r = false;}

}

@Override
public void keyTyped(KeyEvent e) {

}

}

最佳答案

  1. 您直接在 JFrame 中绘图,这样做很危险(正如您发现的那样)。
  2. 您没有从您的绘画覆盖中调用 super 的 paint(...) 方法,从而阻止 JFrame 进行它本身需要进行的绘画。
  3. 您正在从绘画方法中调用 repaint() - 永远不要这样做。
  4. 您还没有阅读 Swing 教程中的绘画。

相反:

  1. 在 JPanel 的 paintComponent 方法中绘制。
  2. 在您的重写中调用 super 的 paintComponent。
  3. 使用 Key Bindings 而不是 KeyListeners(Google 教程,因为它将解释如何使用这些)。

检查 Swing Info指向教程和资源的链接。

例如

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class SimpleAnimationEg extends JPanel {
private static final int OVAL_WIDTH = 20;
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private int x = 0;
private int y = 0;

public SimpleAnimationEg() {
addKeyBindings();
}

private void addKeyBindings() {
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();

KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
inputMap.put(keyStroke, keyStroke.toString());
actionMap.put(keyStroke.toString(), new MyAction(0, -1));

keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
inputMap.put(keyStroke, keyStroke.toString());
actionMap.put(keyStroke.toString(), new MyAction(0, 1));

keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
inputMap.put(keyStroke, keyStroke.toString());
actionMap.put(keyStroke.toString(), new MyAction(-1, 0));

keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
inputMap.put(keyStroke, keyStroke.toString());
actionMap.put(keyStroke.toString(), new MyAction(1, 0));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.fillOval(x, y, OVAL_WIDTH, OVAL_WIDTH);
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

private class MyAction extends AbstractAction {
private int xDirection;
private int yDirection;

public MyAction(int xDirection, int yDirection) {
this.xDirection = xDirection;
this.yDirection = yDirection;
}

@Override
public void actionPerformed(ActionEvent e) {
x += xDirection;
y += yDirection;
repaint();
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame("SimpleAnimationEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SimpleAnimationEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 在 Java 中编码图形和 KeyListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30082210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com