Can’t get to load my icon in JFrame

Recently I am working on my project. I am trying to make the old pang game.
But I couldn’t managed to get pass my player class.

To my Knowledge I have done everything right, but nothing is showing up on my JFrame. Can anybody help me out?

Below is my java code for player class:

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

public class player extends KeyAdapter {

    JFrame frame;
    JLabel player_lbl;
    ImageIcon icon = new ImageIcon(("./pang1.PNG"));
    ImageIcon upIcon = new ImageIcon(("./pang1.PNG"));
    ImageIcon downIcon = new ImageIcon(("./pang1.PNG"));
    ImageIcon rightIcon1 = new ImageIcon(("./pang2.PNG"));
    ImageIcon rightIcon2 = new ImageIcon(("./pang5.PNG"));
    ImageIcon leftIcon1 = new ImageIcon(("./pang3.PNG"));
    ImageIcon leftIcon2 = new ImageIcon(("./pang4.PNG"));
    private static final int frameW = 640;
    private static final int frameH = 480;

    public player()
    {
        frame = new JFrame("Moving Pang");
        frame.setLayout(null); 
        frame.setBackground(Color.white);
        player_lbl = new JLabel();
        player_lbl.setIcon(icon);
        player_lbl.setLocation(frameW/2,frameH);
        player_lbl.setSize(50,50);
        player_lbl.setHorizontalAlignment(JLabel.CENTER);
        frame.setResizable(false);

        frame.add(player_lbl);
        frame.setSize(frameW, frameH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        frame.setVisible(true);
        frame.addKeyListener(this);
    }
    public void keyPressed(KeyEvent e)
    {
        int posX = player_lbl.getX();
        int posY = player_lbl.getY();

        if(e.getKeyCode() == KeyEvent.VK_UP)
        {
            player_lbl.setIcon(new ImageIcon("./pang1.PNG"));
        }
        if(e.getKeyCode() == KeyEvent.VK_LEFT)
        {
            posX-=10;
            int a;
            for(int i =0 ; i<2 ; i++)
            {
                if(i == 0)
                {
                player_lbl.setIcon(leftIcon1);
                a = 1;
                }
                if(i == 1)
                {
                    player_lbl.setIcon(leftIcon2);
                    a = 0;
                }
                }
            }
        if(e.getKeyCode() == KeyEvent.VK_RIGHT )
        {
            posX+=10;
            int a;
            for(int i =0 ; i<2 ; i++)
            {
                if(i == 0)
                {
                player_lbl.setIcon(rightIcon1);
                a = 1;

                }
                if(i == 1)
                {
                    player_lbl.setIcon(rightIcon2);
                    a = 0;
                }
                }
            }
        if(e.getKeyCode() == KeyEvent.VK_SPACE)
        {
        }
        if(e.getKeyCode() == KeyEvent.VK_DOWN)
        {
            player_lbl.setIcon(downIcon);
        }
        player_lbl.setLocation(posX,posY);
        player_lbl.setIcon(icon);
        }

    public static void main(String[] args)
    {
        player p = new player();
    }
    }


Source: oop

Leave a Reply