Alex Capistran Alex Capistran - 7 months ago 11
Java Question

Why do the panels disappear when i press UP?

This is my code for a game im making. At the moment im not worried about how the game functions I've been more so worried about the fact that each time I hit the UP button the panels disappear and sometimes when i hit the LEFT button as well. Is there an explanation to this can anyone help me understand why this happens?? I have a feeling it has something to do with my if statements but im not really sure. also, im messing around with the key listener and if you could give me some advice on key listeners like some dos and donts I really appreciate the help!!

import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;


public class Game extends Applet implements ActionListener,KeyListener {
Image image;
MediaTracker tr;
JLabel label,computerLabel;
JPanel panel,computerPanel;
Button start,up,down;
Label result;
Dimension SIZE = new Dimension(50,50);

int x = 0;
int y = 0;
int w = 100;
int q = 100;
int WIDTH = 50;
int HEIGHT = 50;
//Player Integers
int zeroPosX,zeroPosY,xLeft,xUp;
//Computer integers
int compZeroPosX,compZeroPosY,compXLeft,compXUp;

//--------------------------------------
public void init() {
setLayout(new FlowLayout());

start = new Button("Start");
up = new Button("UP");
down = new Button("LEFT");
//PlayerPiece stuff
ImageIcon icon = new ImageIcon("playerpiece.png");
label = new JLabel(icon);
panel = new JPanel();

label.setVisible(true);
panel.add(label);
panel.setPreferredSize(SIZE);
//ComputerPiece Stuff
ImageIcon computerIcon = new ImageIcon("computerPiece.png");
computerPanel = new JPanel();
computerLabel = new JLabel(computerIcon);

computerLabel.setVisible(true);
computerPanel.add(computerLabel);
computerPanel.setPreferredSize(SIZE);

//===============================================

result = new Label("=========");
addKeyListener(this);

setSize(650,650);

up.addActionListener(this);
down.addActionListener(this);
start.addActionListener(this);

label.setSize(WIDTH,HEIGHT);
label.setLocation(0,0);

add(computerPanel);
add(panel);
add(start);
add(up);
add(down);
add(result);


}

//--------------------------------------
public void paint(Graphics g) {


Graphics2D firstLayer = (Graphics2D)g;
Graphics2D secondLayer = (Graphics2D)g;
Graphics2D thirdLayer = (Graphics2D)g;


secondLayer.setColor(Color.BLACK);


for(x=100; x<=500; x+=100)
for(y=100; y <=500; y+=100)
{

firstLayer.fillRect(x,y,WIDTH,HEIGHT);


}
for(w=150; w<=500; w+=100)
for(q=150; q <=500; q+=100)
{

secondLayer.fillRect(w,q,WIDTH,HEIGHT);


}





}
//--------------------------------------
public void actionPerformed(ActionEvent ae) {

int [] range = {50,0,0,0,0};
int selection = (int)Math.random()*5 ;


//~~~~~~~~~~~~~~~~~~~~~~~~~

//PlayerPositioning
zeroPosX = panel.getX();
zeroPosY = panel.getY();
xLeft = zeroPosX - 50;
xUp = zeroPosY - 50;
//ComputerPositioning
compZeroPosX = computerPanel.getX();
compZeroPosY = computerPanel.getY();
compXLeft = compZeroPosX - range[selection];
compXUp = compZeroPosY - range[selection];
//~~~~~~~~~~~~~~~~~~~~~~~~~~


Button user = (Button)ae.getSource();




//Starting the game
if(user.getLabel() == "Start") {
result.setText("=========");
//playersetup
label.setLocation(0,0);
panel.setLocation(300,500);
//============================
//npc setup
computerLabel.setLocation(0,0);
computerPanel.setLocation(500,300);


}



if(compZeroPosX >= 150) {
if(compZeroPosY >= 150) {
if(zeroPosX >= 150) {
if(zeroPosY >=150) {


if(user.getLabel() == "UP") {
panel.setLocation(zeroPosX,xUp);

}
else
computerPanel.setLocation(compZeroPosX,compXUp);




if(user.getLabel() == "LEFT") {
panel.setLocation(xLeft,zeroPosY);

}

else
computerPanel.setLocation(compXLeft,compZeroPosY);

if(panel.getX() < 150)
result.setText("GAME-OVER");


if(panel.getY() < 150)
result.setText("GAME-OVER");

}
}
}
}
}
@Override
public void keyPressed(KeyEvent kp) {
int keycode = kp.getKeyCode();

switch (keycode) {
case KeyEvent.VK_W:
panel.setLocation(xLeft,zeroPosY);
break;
}





}
@Override
public void keyReleased(KeyEvent kr) {

}
@Override
public void keyTyped(KeyEvent kt) {

}

}

Answer

Issues and suggestions:

  1. You're mixing AWT (e.g., Applet, Button, Label) with Swing (e.g., JPanel, JLabel) dangerously and without need. Stick with Swing and get rid of all vestiges of AWT.
  2. You're painting directly in a top-level window, here the Applet, a dangerous thing to do. Don't. Follow the Swing graphics tutorials and do your drawing in a JPanel's paintComponent method.
  3. You're not calling the super method within your painting method override, another dangerous thing to do, and another indication that you're trying to do this without reading the important relevant tutorials.
  4. Don't compare Strings using == or !=. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two object references are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here.
  5. You're trying to directly set the location of a component such as a JPanel without regard for the layout managers. Don't do this. Instead move logical (non-component) entities and display the movement in your graphics.

You can find links to the Swing tutorials and to other Swing resources here: Swing Info

Later we can talk why you should avoid applets of all flavors...

Comments