我的KeyListener
和方法有效,但他们什么也没做。当我运行代码并尝试使用箭头键时,没有任何反应。我还有另一个问题。我正在制作游戏而且我已经完成了地图,但是当我将地图放入时,地图优先并覆盖我的角色。我该如何解决这些问题呢?
这是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();
}
}
答案 0 :(得分:4)
您需要处理一些问题
您已经创建了两组变量。框架中有一组,面板中有一组。使用Keyalistener,您只需更改框架中的值,这对面板中的值没有影响。
UI的更新应该在Event Dispatch Thread上完成。请勿使用while(true)
和Thread.sleep()
的新主题。而是使用javax.swing.Timer
来制作动画。点击How to Use Swing Timers了解更多信息。您还会看到一系列示例here和here以及here和here以及here和here。
您应该使用键绑定来执行特定于键的操作,而不是KeyListener。点击How to Use Key Bindings了解更多信息。您还可以看到一个简单示例here
其他:
getPreferredSize()
以设置面板的首选尺寸,并且当您pack()
框架时(您应该这样做,而不是设置尺寸),它将得到尊重< / LI>
<强>更新强>
这是完整的代码。它适用于我
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();
}
}