将图片覆盖在背景图片上

我正在用Java制作街头霸王游戏,我已经完成了所有背景图片,并且尝试将角色添加到背景上,但是在输出中,角色图片始终显示在背景旁边而不是顶部它。我希望角色像普通的街头霸王游戏一样出现在背景的顶部。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.Border;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class CastleFighter extends JFrame {
    Image image;
    public static void main(String[] args) {
        CastleFighter angela = new CastleFighter();
        angela.runIt();
    }
    public void runIt() {
        final JFrame frame = new JFrame("Castle Fighter");
        frame.setSize(500, 477);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pa = new JPanel();
        JButton ba1 = new JButton("Start");
        ba1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                final JFrame frame2 = new JFrame("Menu");
                frame2.setDefaultCloseOperation(HIDE_ON_CLOSE);
                JPanel pa2 = new JPanel();
                frame2.add(pa2);
                ImageIcon iii = new ImageIcon(this.getClass().getResource("fight.gif"));
                JLabel imageLabel2 = new JLabel();
                imageLabel2.setIcon(iii);
                pa2.add(imageLabel2, java.awt.BorderLayout.CENTER);
                frame2.setVisible(true);
                frame2.pack();
                frame2.requestFocus();
                imageLabel2.setLayout(new GridLayout(3, 0));
                JButton ba2 = new JButton("How to play");
                ba2.setFont(new Font("Merriweather", Font.PLAIN, 28));
                imageLabel2.add(ba2, BorderLayout.SOUTH);
                ba2.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        frame2.dispose();
                        final JFrame frame3 = new JFrame("How to play");
                        frame3.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                        JPanel pa3 = new JPanel();
                        frame3.add(pa3);
                        JButton ba5 = new JButton("Return to menu");
                        ba5.setFont(new Font("Merriweather", Font.PLAIN, 28));
                        ba5.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                frame3.dispose();
                                frame2.setVisible(true);
                            }
                        });
                        pa3.setLayout(new BorderLayout());
                        pa3.add(ba5, BorderLayout.SOUTH);
                        ImageIcon iiiii = new ImageIcon(this.getClass().getResource("ezgif.com-crop-7.gif"));
                        JLabel imageLabel4 = new JLabel();
                        imageLabel4.setIcon(iiiii);
                        pa3.add(imageLabel4, java.awt.BorderLayout.CENTER);
                        frame3.setVisible(true);
                        frame3.pack();
                        frame3.requestFocus();
                    }
                });
                JButton ba3 = new JButton("Start game");
                ba3.setFont(new Font("RussoOne", Font.PLAIN, 28));
                imageLabel2.add(ba3, BorderLayout.CENTER);
                ba3.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        frame2.dispose();
                        final JFrame frame4 = new JFrame("Level one");
                        frame4.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                        JPanel pa4 = new JPanel();
                        frame4.add(pa4);
                        ImageIcon iiii = new ImageIcon(this.getClass().getResource("ezgif.com-add-text-7.gif"));
                        JLabel imageLabel3 = new JLabel();
                        imageLabel3.setIcon(iiii);
                        pa4.add(imageLabel3, java.awt.BorderLayout.CENTER);
                        ImageIcon i14 = new ImageIcon(this.getClass().getResource("fightercharacter.gif"));
                        JLabel imageLabel14 = new JLabel();
                        imageLabel14.setIcon(i14);
                        pa4.add(imageLabel14, java.awt.BorderLayout.NORTH);
                        JButton ba6 = new JButton("menu");
                        ba6.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                frame4.dispose();
                                frame2.setVisible(true);
                            }
                        });
                        pa4.add(ba6);
                        JButton ba7 = new JButton("Level two");
                        ba7.setSize(400, 200);
                        pa4.add(ba7);
                        ba7.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                frame4.dispose();
                                final JFrame frame6 = new JFrame("Level two");
                                frame6.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                                frame6.setVisible(true);
                                JPanel pa6 = new JPanel();
                                frame6.add(pa6);
                                ImageIcon iiiiiii = new ImageIcon(this.getClass().getResource("ezgif.com-add-text-6.gif"));
                                JLabel imageLabel6 = new JLabel();
                                imageLabel6.setIcon(iiiiiii);
                                pa6.add(imageLabel6, BorderLayout.CENTER);
                                frame6.pack();
                                frame6.requestFocus();
                                JButton ba7 = new JButton("menu");
                                pa6.add(ba7, BorderLayout.SOUTH);
                                ba7.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                                        frame6.dispose();
                                        frame2.setVisible(true);
                                    }
                                });
                                JButton ba9 = new JButton("Level one");
                                pa6.add(ba9, BorderLayout.SOUTH);
                                ba9.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                                        frame6.dispose();
                                        frame4.setVisible(true);
                                    }
                                });
                                frame6.setVisible(true);
                                frame6.pack();
                                frame6.requestFocus();
                            }
                        });
                        frame4.setVisible(true);
                        frame4.pack();
                        frame4.requestFocus();
                    }
                });
                JButton ba4 = new JButton("Quit game");
                ba4.setFont(new Font("MerriWeather", Font.PLAIN, 28));
                imageLabel2.add(ba4, BorderLayout.WEST);
                ba4.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.exit(0);
                    }
                });
                frame2.requestFocus();
                ba2.setOpaque(false);
                ba2.setContentAreaFilled(false);
                ba2.setBorderPainted(false);
                ba2.setForeground(Color.WHITE);
                ba3.setOpaque(false);
                ba3.setContentAreaFilled(false);
                ba3.setBorderPainted(false);
                ba3.setForeground(Color.WHITE);
                ba4.setOpaque(false);
                ba4.setContentAreaFilled(false);
                ba4.setBorderPainted(false);
                ba4.setForeground(Color.WHITE);
            }
        });
        ba1.setPreferredSize(new Dimension(100, 100));
        ba1.setFont(new Font("RussoOne", Font.PLAIN, 28));
        ba1.setBackground(Color.BLUE);
        ba1.setOpaque(true);
        frame.add(pa);
        pa.setLayout(new BorderLayout());
        pa.add(ba1, BorderLayout.SOUTH);
        ImageIcon ii = new ImageIcon(this.getClass().getResource("ezgif.com-add-text-2.gif"));
        JLabel imageLabel = new JLabel();
        imageLabel.setIcon(ii);
        pa.add(imageLabel, java.awt.BorderLayout.CENTER);
        frame.setVisible(true);
        frame.pack();
        frame.requestFocus();
    }

}
评论