如何将JLabel重叠到gif图像上?

问题描述 投票:0回答:1

我正在尝试制作一个数字将从顶部下降的游戏,如果玩家使用正确的方程式击中数字(数学游戏),它将消失。

事实是,每当我绘制背景时,Jlabel就会继续显示在giF背景的背面。任何想法为什么?

这就像我的核心课程。

package GAME;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GameFrame extends JPanel implements ActionListener{

Image background = Toolkit.getDefaultToolkit().createImage("D:\\SHIPMATH TEXTURES\\simple-water-animation-597-x-800.gif");


Timer mainTimer;

shipPlayer player;
enemyShips enemy;


int level = 5;
int enemyCount = 5;
int numCount = 10;


// arraylists are arrays that has no capacity limit
static ArrayList<Numbers> number = new ArrayList<Numbers>();
static ArrayList<enemyShips> enemies = new ArrayList<enemyShips>();
static ArrayList<Cannons> cannon_balls = new ArrayList<Cannons>();

JLabel numbers;

Random rn = new Random();

public GameFrame() {
    // calls the actionPerformed method every 10 milliseconds   
    mainTimer = new Timer(10, this);

    mainTimer.start();
    setLayout(null);



    numbers = new JLabel("TEST");
    numbers.setBounds(200, 200, 100, 100);
    add(numbers);




    setFocusable(true);

    player = new shipPlayer(0, 500);
    addKeyListener(new KeyAdapt(player));



    startGame();

}


public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;

//      g2d.drawImage(background, 0, 0, this);

    player.draw(g2d);






    repaint();


    for(int i=0; i<enemies.size(); i++) {

        // the value of 'i' is the location/index used to find the value stored in the ArrayList
        enemyShips enemy = enemies.get(i);
        enemy.draw(g2d);

    }

    for(int i=0; i<cannon_balls.size(); i++) {
        Cannons cannon = cannon_balls.get(i);
        cannon.draw(g2d);
    }







    }



    public void actionPerformed(ActionEvent e) {
    player.update();

    // where movement of the enemy happens
    for(int i=0; i<enemies.size(); i++) {
        enemyShips enemy = enemies.get(i);
        enemy.update();
    }


 //     Later...
    for(int i=0; i<cannon_balls.size(); i++) {
        Cannons cannon = cannon_balls.get(i);
        cannon.update();


    }





    repaint();


}


public static void addEnemy(enemyShips e) {

    enemies.add(e);
    // stores what the user puts in the enemyShips' object into the ArrayList "enemies"
}

public static void removeEnemy(enemyShips e) {
    enemies.remove(e);
    // removes what the user inputs from the ArrayList
}

public static ArrayList<enemyShips> getEnemyList() {
    return enemies;
}



public static void addNumbers(Numbers n) {
    number.add(n);
}


public static void addCannons(Cannons c) {

    cannon_balls.add(c);
    // stores what the user puts in the Cannons' object into the ArrayList "cannon_balls"
}

public static void removeCannons(Cannons c) {
    cannon_balls.remove(c);
    // removes what the user inputs from the ArrayList
}

public static ArrayList<Cannons> getCannonsList() {
    return cannon_balls;
}







public void startGame() {

//      enemyCount = level * 5;

    // runs 5 times
    for(int x=0; x<enemyCount; x++) {

        addEnemy(new enemyShips(rn.nextInt(500), -rn.nextInt(800)));

    }



}

}
java jlabel
1个回答
0
投票

解决此问题的方法是将paint方法重命名为paintComponent。这是您现在正在做的事情:

public void paint(Graphics g) {
    super.paint(g); //this paints all of your JPanel's child components, including your JPanel

    Graphics2D g2d = (Graphics2D) g;

//      g2d.drawImage(background, 0, 0, this); // this is painting your background image over your JPanel.

    player.draw(g2d);

    ...

如果您改用paintComponent,则子组件不会被涂上油漆

© www.soinside.com 2019 - 2024. All rights reserved.