0

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

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Where are the images stored? `paintComponent` should be `protected`, there should never be any reason for anybody else to call it. You should, by convention, call `super.paintComponent` before doing any custom painting – MadProgrammer May 28 '14 at 00:14
  • 2
    Welcome to Stack Overflow! Don't use `null` layouts. Pixel perfect layouts are an illusion in modern UI design, you have no control over fonts, DPI, rendering pipelines or other factors that will change the way that you components will be rendered on the screen. Swing was designed to work with layout managers to overcome these issues. If you insist on ignoring these features and work against the API design, be prepared for a lot of headaches and never ending hard work... – MadProgrammer May 28 '14 at 00:14

2 Answers2

2

The main problem is your use of null layouts, for example...

JLabel m = new JLabel();
m.setIcon(mIcon);
add(m);

m has no size or position information by default (0x0@0x0)

The appropriate answer would be to use one or more appropriate layout managers

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1
public void paintComponent(Graphics g) {
    ImageIcon Backg = new ImageIcon("playbg.png");

Should be:

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
  1. The first call in an overridden paint should be the super method.
  2. The image should be loaded and cached as a class member. Never try to load a resource in a paint method, which is supposed to finish quickly.

General tips

  1. By the time of deployment, those resources will likely become an . That being the case, the resource must be accessed by URL instead of File. See the info page for the tag, for a way to form an URL.
  2. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433