0

I am making a rpg style game when the character can walk around using the arrowkeys. When the character walks to a door the variable inside would change to true or false. In my main class i put a logic statement

if (inside == true) {
add (new inside());
} else if (inside == false) {
add(new outside());

this didn't work. I have done a lot of research and found cardlayout everywhere but as far as i know cardlayout can't draw graphics. In addition to second and inside there is also an outside class but it is the same as inside except for graphics and the door's location.

this is my inside class

public class inside extends JPanel implements ActionListener, KeyListener{

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.Timer;

public class inside extends JPanel implements ActionListener, KeyListener{

Timer timer = new Timer(5, this);

int x = 10;
int y = 10; 

public inside() {

timer.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void paint(Graphics g) {

super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fill(new Rectangle2D.Double(x, y, 30, 30)); //player
g2.setColor(Color.blue);
g2.fill(new Rectangle2D.Double(135, 85, 30, 30)); //old man
g2.setColor(Color.darkGray);
g2.fill(new Rectangle2D.Double(290, 0, 10, 300)); //wall
g2.fill(new Rectangle2D.Double(0, 0, 300, 10)); //wall
g2.fill(new Rectangle2D.Double(0, 0, 10, 300)); //wall
g2.fill(new Rectangle2D.Double(0, 290, 300, 10)); //wall
g2.setColor(Color.white);
g2.fill(new Rectangle2D.Double(290, 135, 10, 30)); //door
g2.setColor(Color.black);
g2.drawString(second.inside + "", 15, 15);
}

public void actionPerformed(ActionEvent e) {

repaint();
}

public void keyPressed(KeyEvent e) {

int code = e.getKeyCode();

if (code == KeyEvent.VK_UP) {
if (y <= 10) {
y = 10;
} else {
y -= 5;
}
} else if (code == KeyEvent.VK_DOWN) {
if (y >= 260) {
y = 260;
} else {
y += 5;
}
} else if (code == KeyEvent.VK_LEFT) {
if (x <= 10) {
x = 10;
} else {
x -= 5;
}
} else if (code == KeyEvent.VK_RIGHT) {
if (x >=260) {
x = 260;
if (second.inside == true && x == 260 && y == 135) {
second.inside = false;
}
} else {
x += 5;
}
}
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}
}

and this is the class that is supposed to switch between panels

import javax.swing.JFrame;

public class second extends JFrame {

public static boolean inside = true;

public second() {

if (inside == true) {
add(new inside());
} else if (inside == false) {
add(new outside());
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(316, 338);
setLocationRelativeTo(null);
setTitle("Test Game 3");
setVisible(true);
}

public static void main(String[] args) {
new second();
}
}
4

1 回答 1

0

我看到的第一个问题是您没有先删除现有面板。第二个问题是交换面板的逻辑在 JFrame 的构造函数中。理想情况下,在 JFrame 中你会有这样的循环

while(true) 
{
    updateGameLogic();
    repaint();
    try{
      System.sleep(30);
    } catch(Exception e) {}
}

updateGameLogic() 可以做很多事情,其中​​之一是检查布尔值以确定是否交换 JPanel。Thread.sleep() 方法是必要的,因此 UI 不会阻塞其他线程(输入)。我会在 Second 类中放置一个名为 mainLoop() 的方法,并在创建对象后调用该方法。

于 2012-04-13T21:28:22.527 回答