Im doing a final project for my high school computer science class (a really mediocre Pokemon Game) and I am having trouble with a switch statement that i made to add a Jlabel with an ImageIcon in it.
When i run the driver and it opens this panel it is supposed to Add a random opponent, that is what the switch statement is for and it gives the oppHP variable a value depending on who the opponent is. The bottom PaintComponent sets the background image. The problem is when i do run it no opponent picture shows up its just the background image.
keep in mind im very new to coding so i might not understand the explination, in other words, if its not too much trouble, try to keep it simple. Thanks!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lucario extends JPanel {
private int myHP;
private int oppHP;
private boolean ok;
private int rand;
private JButton olabel;
private JButton mlabel;
public Lucario() {
ImageIcon lIcon = new ImageIcon("opplucario.png");
ImageIcon sIcon = new ImageIcon("oppsylveon.png");
ImageIcon cIcon = new ImageIcon("oppcharizard.png");
ImageIcon grIcon = new ImageIcon("oppgreninja.png");
ImageIcon geIcon = new ImageIcon("oppgengar.png");
ImageIcon mIcon = new ImageIcon("oppmystery.png");
for (int k = 0; k < 1; k++) {
rand = (int) (Math.random() * 6);
}
switch (rand) {
case 0:
JLabel l = new JLabel();
l.setIcon(lIcon);
add(l);
oppHP = 344;
break;
case 1:
JLabel s = new JLabel();
s.setIcon(sIcon);
add(s);
oppHP = 394;
break;
case 2:
JLabel c = new JLabel();
c.setIcon(cIcon);
add(c);
oppHP = 360;
break;
case 3:
JLabel gr = new JLabel();
gr.setIcon(grIcon);
add(gr);
oppHP = 348;
break;
case 4:
JLabel ge = new JLabel();
ge.setIcon(geIcon);
add(ge);
oppHP = 324;
break;
case 5:
JLabel m = new JLabel();
m.setIcon(mIcon);
add(m);
oppHP = 400;
break;
}
myHP = 344;
this.setLayout(null);
olabel = new JButton("HP: " + oppHP);
olabel.setFont(new Font("Serif", Font.BOLD, 20));
olabel.setBounds(70, 100, 150, 40);
olabel.setOpaque(false);
olabel.setContentAreaFilled(false);
olabel.setBorderPainted(false);
add(olabel);
mlabel = new JButton("HP: " + myHP);
mlabel.setFont(new Font("Serif", Font.BOLD, 20));
mlabel.setBounds(350, 330, 150, 40);
mlabel.setOpaque(false);
mlabel.setContentAreaFilled(false);
mlabel.setBorderPainted(false);
add(mlabel);
}
public void paintComponent(Graphics g) {
ImageIcon Backg = new ImageIcon("playbg.png");
g.drawImage(Backg.getImage(), 0, 0, 590, 590, this);
}
}