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();
}
}