简体   繁体   中英

Java. Why does my image not appear in my GUI

so I have this class of a Laser. My problem is the laser texture is not showing up. Can you point out where the mistake is, please? I'm having a project to present next week and I'm hitting this problem. I'm most likely not executing it right outside this class. The code is doing what it is supposed to do, the image is the problem though. When I press my UP arrow it is supposed to launch a laser beam to the opposite ship, destroying it, I press my UP key, I don't see any laser beam but the adversary gets destroyed.

private class Player1weapon {
        int centerX, centerY;
        boolean isFalling;
        public Image laserp1;
        public ImageIcon icon;


        public Player1weapon() {
            isFalling = false;
            icon = new ImageIcon("resources/laserplayer1.png");
            laserp1 = icon.getImage();
        }

        void updateForNewFrame() {
            if (isFalling) {
                if (centerY > height + 10) {
                    isFalling = false;
                }
                else 
                    if (Math.abs(centerX - player2.centerX - 40) <= 30 && Math.abs(centerY - player2.centerY) <= 21) {
                        player2.isExploding = true;
                        player2.explosionFrameNumber = 1;
                        isFalling = false;
                        player1.score+=10;
                    }
                    else
                        if(player1.isExploding){
                            isFalling = false;
                        }
                        else
                            centerY -= 30;
            }//end if   
        }//end updateForNewFrame()

        void draw(Graphics g) {
            if (!isFalling) {
                centerX = player1.centerX + 37;
                centerY = player1.centerY + 23;
            }
                g.drawImage(laserp1, centerX, centerY - 8, laserp1.getWidth(null), laserp1.getHeight(null),null);
        }//end Draw()

    }//end player1weapon

If you're running from an IDE (like eclipse or netbeans), make sure your directories look something like this

ProjectRoot
         resources
                laserplayer1.png
         src

May be just a problem with your file path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM