gpt4 book ai didi

java - KeyListener 不工作并且映射重叠字符

转载 作者:行者123 更新时间:2023-12-01 18:33:25 26 4
gpt4 key购买 nike

我的KeyListener 和方法可以工作,但它们不执行任何操作。当我运行代码并尝试使用箭头键时,没有任何反应。我还有另一个问题。我正在制作游戏,并且已经制作了 map ,但是当我放入 map 时, map 具有优先级并覆盖了我的角色。我该如何解决这些问题?这是 JFrame 包。

package MyGamePKG;

import java.awt.BorderLayout;

public class GameJFrame extends JFrame {

private JPanel contentPane;
private int x,y,x_vel,y_vel;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GameJFrame frame = new GameJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public GameJFrame() {


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 720, 520);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

GameJPanel Jpanel = new GameJPanel();
Jpanel.setBounds(0, 0, 720, 480);
contentPane.add(Jpanel);
Jpanel.setLayout(null);




}


}

这是 JPanel。

package MyGamePKG;

import java.awt.BasicStroke;

public class GameJPanel extends JPanel implements ActionListener, KeyListener {

private int frameRate = 30;
private int x=0;
private int y=0;
private int x_vel, y_vel;
private Image map;

/**
* Create the panel.
*/
public GameJPanel() {
new ImageIcon(getClass().getResource("/Resources/gamemap.png"));
gamemap = map.getImage(); //error here
this.addKeyListener(this);
this.setFocusable(true);
Timer timer = new Timer(30, this);
timer.start();


}


}

public void background()
{
ImageIcon icon = new ImageIcon(this.getClass().getResource("/Resources/gamemap.png"));
map = icon.getImage();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(Color.WHITE);

Graphics2D g2d = (Graphics2D) g;

RenderingHints rh = new RenderingHints( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setRenderingHints(rh);

g2d.setColor(Color.green);
g2d.setStroke(new BasicStroke(5));


myDrawBoxOvalsandRect(x,y,100,g2d);
myDrawArc(x+25,y+40,50,50,550,170,g2d);
} // paintComponent
public void myDrawBoxOvalsandRect( int x, int y, int scale, Graphics my_g)
{
my_g.setColor(Color.cyan);
my_g.fillOval(x, y, 100, 100); //face

my_g.setColor(Color.red);
my_g.fillOval(x+20, y+25, 15, 15); //left eye

my_g.setColor(Color.red);
my_g.fillOval(x+60, y+25, 15, 15); //right eye

my_g.setColor(Color.cyan);
my_g.fillRect(x+25,y+100,50,80);//body

my_g.setColor(Color.cyan);
my_g.fillRect(x-30,y+105,55,20); //left arm

my_g.setColor(Color.cyan);
my_g.fillRect(x+70, y+105, 60, 20); //right arm

my_g.setColor(Color.red);
my_g.fillOval(x-47, y+105, 20, 20); //left hand

my_g.setColor(Color.red);
my_g.fillOval(x+126,y+105,20,20); //right hand

my_g.setColor(Color.cyan);
my_g.fillRect(x+25, y+175, 20, 50); //left leg

my_g.setColor(Color.cyan);
my_g.fillRect(x+55,y+175,20,50); // right leg

}
public void myDrawArc(int x, int y, int height, int width, int angle1, int angle2, Graphics my_g)
{
my_g.setColor(Color.red);
my_g.drawArc(x, y, 50, 50, 550, 170); //happy face
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int c = e.getKeyCode();

if(c == KeyEvent.VK_LEFT)
{
x_vel = -1;
y_vel = 0;
}
if(c == KeyEvent.VK_UP)
{
x_vel = 0;
y_vel = -1;
}
if( c == KeyEvent.VK_RIGHT)
{
x_vel = 1;
y_vel = 0;
}
if( c == KeyEvent.VK_DOWN)
{
x_vel = 0;
y_vel = 1;
}
repaint();

}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
x_vel = 0;
y_vel = 0;
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(x < 0)
{
x_vel = 0;
x = 0;
}
if(x > 720)
{
x_vel = 0;
x = 720;
}
if(y < 0)
{
y_vel = 0;
y = 0;
}
if(y > 480)
{
y_vel = 0;
y = 480;
}
x = x + x_vel;
y = y + y_vel;
repaint();

}


}

最佳答案

您有一些问题需要处理

  1. 您已经创建了两组变量。一组在框架中,一组在面板中。使用 Keyallistener,您只需更改框架中的值,这不会影响面板中的值。

  2. UI 的更新应在事件调度线程上完成。不要将新线程与 while(true)Thread.sleep() 一起使用。而是使用 javax.swing.Timer 来实现动画。查看更多How to Use Swing Timers 。您还可以看到一堆示例 herehereherehereherehere .

  3. 您应该使用键绑定(bind)来执行特定于键的操作,而不是 KeyListener。查看更多How to Use Key Bindings 。您还可以看一个简单的例子here

<小时/>

其他:

  • 为什么在没有将 ActionListener 添加到任何内容时使用它?
  • 不要使用空布局。学习使用 LayoutManager,并让它们为您调整大小和位置。
  • 对于绘制的面板,您应该覆盖 getPreferredSize() 以设置面板的首选尺寸,并且当您 pack() 框架时,它将受到尊重(如你应该,而不是设置大小)
  • 不要在paint方法中设置背景。在构造函数中执行此操作。
<小时/>

更新

这是完整的代码。对我来说效果很好

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class GameFrame extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GameFrame frame = new GameFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public GameFrame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 720, 520);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

GameJPanel Jpanel = new GameJPanel();
Jpanel.setBounds(0, 0, 720, 480);
contentPane.add(Jpanel);
Jpanel.setLayout(null);

}
}

class GameJPanel extends JPanel implements ActionListener, KeyListener {

private int frameRate = 30;
private int x = 0;
private int y = 0;
private int x_vel, y_vel;
private Image map;

/**
* Create the panel.
*/
public GameJPanel() {
map = new ImageIcon(getClass().getResource("/resources/gamemap.png")).getImage();
this.addKeyListener(this);
this.setFocusable(true);
Timer timer = new Timer(30, this);
timer.start();
/*
Thread myAnimationThread = new Thread() {
@Override
public void run() {
while (true) {
repaint(); // Refresh the display which calls paintComponent
try {
Thread.sleep(1000 / frameRate);
} // try
catch (InterruptedException ex) {
}
} // while
} // run
}; // Thread
*/
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);

Graphics2D g2d = (Graphics2D) g;

RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setRenderingHints(rh);

g2d.setColor(Color.green);
g2d.setStroke(new BasicStroke(5));


g2d.drawImage(map, 0, 0, 720, 480, getParent());
myDrawBoxOvalsandRect(x, y, 100, g2d);
myDrawArc(x + 25, y + 40, 50, 50, 550, 170, g2d);
} // paintComponent

public void myDrawBoxOvalsandRect(int x, int y, int scale, Graphics my_g) {
my_g.setColor(Color.cyan);
my_g.fillOval(x, y, 100, 100); // face

my_g.setColor(Color.red);
my_g.fillOval(x + 20, y + 25, 15, 15); // left eye

my_g.setColor(Color.red);
my_g.fillOval(x + 60, y + 25, 15, 15); // right eye

my_g.setColor(Color.cyan);
my_g.fillRect(x + 25, y + 100, 50, 80);// body

my_g.setColor(Color.cyan);
my_g.fillRect(x - 30, y + 105, 55, 20); // left arm

my_g.setColor(Color.cyan);
my_g.fillRect(x + 70, y + 105, 60, 20); // right arm

my_g.setColor(Color.red);
my_g.fillOval(x - 47, y + 105, 20, 20); // left hand

my_g.setColor(Color.red);
my_g.fillOval(x + 126, y + 105, 20, 20); // right hand

my_g.setColor(Color.cyan);
my_g.fillRect(x + 25, y + 175, 20, 50); // left leg

my_g.setColor(Color.cyan);
my_g.fillRect(x + 55, y + 175, 20, 50); // right leg

}

public void myDrawArc(int x, int y, int height, int width, int angle1,
int angle2, Graphics my_g) {
my_g.setColor(Color.red);
my_g.drawArc(x, y, 50, 50, 550, 170); // happy face
}

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int c = e.getKeyCode();

if (c == KeyEvent.VK_LEFT) {
x_vel = -1;
y_vel = 0;
}
if (c == KeyEvent.VK_UP) {
x_vel = 0;
y_vel = -1;
}
if (c == KeyEvent.VK_RIGHT) {
x_vel = 1;
y_vel = 0;
}
if (c == KeyEvent.VK_DOWN) {
x_vel = 0;
y_vel = 1;
}
repaint();

}

public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
x_vel = 0;
y_vel = 0;
repaint();
}

public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (x < 0) {
x_vel = 0;
x = 0;
}
if (x > 720) {
x_vel = 0;
x = 720;
}
if (y < 0) {
y_vel = 0;
y = 0;
}
if (y > 480) {
y_vel = 0;
y = 480;
}
x = x + x_vel;
y = y + y_vel;
repaint();

}
}

关于java - KeyListener 不工作并且映射重叠字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23121755/

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