【问题标题】:GiF as background for JFrame [duplicate]GiF作为JFrame的背景[重复]
【发布时间】:2016-12-08 23:25:25
【问题描述】:

我正在制作一个数学游戏,并想使用 gif 作为背景。尺寸为1100*800

我搜索了很多帖子如何添加 GIF 作为背景,但没有成功。任何关于简单方法的建议(如果使用其他组件 -JPanel,...;您能说明一下方法吗?)

到目前为止,这是我的 JFrame 代码:

public class Game extends JFrame implements ActionListener {
       private JButton play, endG, tutorial, login, easy, medium, hard, next, checkAnswer;
       private JTextArea answer;
       int total, goodAnswer = 0;

       public Game(String heading) {
           super(heading);
           this.setSize(1100, 800);
           this.setLayout(null);


           firstScreen();
           setResizable(false);

       }

       public void firstScreen() {
           getContentPane().removeAll();

           play = new JButton();
           play.setBounds(373, 350, 354, 80);
           play.setIcon(new ImageIcon("entrancePlayButton.png"));
           play.addActionListener(this);
           play.setOpaque(false);
           play.setContentAreaFilled(false);
           add(play);

           tutorial = new JButton("Tutorial");
           tutorial.setBounds(345, 520, 150, 50);
           tutorial.setFont(new Font("Arial", Font.PLAIN, 20));
           tutorial.addActionListener(this);
           tutorial.setOpaque(false);
           tutorial.setContentAreaFilled(false);
           add(tutorial);

           endG = new JButton("End Game");
           endG.setBounds(605, 520, 150, 50);
           endG.setFont(new Font("Arial", Font.PLAIN, 20));
           endG.addActionListener(this);
           endG.setOpaque(false);
           endG.setContentAreaFilled(false);
           add(endG);

           revalidate();
           repaint();
       }

       public void difficultyScreen() {
           getContentPane().removeAll();

           easy = new JButton("Easy");
           easy.setBounds(450, 310, 200, 80);
           easy.setFont(new Font("Arial", Font.PLAIN, 30));
           easy.addActionListener(this);
           easy.setOpaque(false);
           easy.setContentAreaFilled(false);
           add(easy);

           medium = new JButton("Medium");
           medium.setBounds(450, 440, 200, 80);
           medium.setFont(new Font("Arial", Font.PLAIN, 30));
           medium.addActionListener(this);
           medium.setOpaque(false);
           medium.setContentAreaFilled(false);
           add(medium);

           hard = new JButton("Hard");
           hard.setBounds(450, 570, 200, 80);
           hard.setFont(new Font("Arial", Font.PLAIN, 30));
           hard.addActionListener(this);
           hard.setOpaque(false);
           hard.setContentAreaFilled(false);
           add(hard);

           endG = new JButton("Exit");
           endG.setBounds(1000, 700, 60, 30);
           endG.setFont(new Font("Arial", Font.PLAIN, 15));
           endG.addActionListener(this);
           endG.setOpaque(false);
           endG.setContentAreaFilled(false);
           add(endG);

           revalidate();
           repaint();
       }

       public void playGameScreen() {
           getContentPane().removeAll();



           revalidate();
           repaint();
       }

       public void tutorialScreen() {
           getContentPane().removeAll();



           revalidate();
           repaint();
       }

       private static double stringToDouble(String number) {
           double num = Double.parseDouble(number);
           return num;
       }

       public static void main() {
           Game areaGame = new Game("Area Game");
           areaGame.setVisible(true);
       }

       public void actionPerformed(ActionEvent actionEvent) {
           if (actionEvent.getSource() == play) {
               difficultyScreen();
           }

           if (actionEvent.getSource() == tutorial) {
               tutorialScreen();
           }

           if (actionEvent.getSource() == endG) {
               int reply = JOptionPane.showConfirmDialog(null, "You are about to exit the game, are you sure?", "Exit game", JOptionPane.YES_NO_OPTION);
               if (reply == JOptionPane.YES_OPTION) {
                   System.exit(0);
               }

           }

       }


   }

【问题讨论】:

标签: java swing animation background


【解决方案1】:

有两种方法可以做到这一点:

  • 您可以覆盖JPanelpaintComponent() 方法。 像这样:

@Override

protected void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.drawImage(yourImage, 0, 0, this);

}
  • 或者您可以使用JLabel,将图像加载为ImageIcon,然后将其显示在JLabel 中。

【讨论】:

  • 之前我评论说我会将 gif 分成帧 (41) 并使用计时器功能逐帧加载。如果我只是调用你在计时器中编写的函数,它会正常工作吗?还是需要进行任何其他编辑?
  • 我不确定这是否可以在动画中使用,但您可以随时在您的代码中尝试一下,看看结果如何。因为您正在制作动画,我强烈建议您使用 SwingWorker:@ 987654321@
  • 当然会尝试您推荐的所有方法! :)
  • g.drawImage(yourImage, 0, 0, null); 应该是g.drawImage(yourImage, 0, 0, this);
  • 有许多同步加载图像的方法(代码在加载图像之前被阻塞)。但是将this 用于ImageObserver 涵盖了那些以及 异步加载图像的情况。
猜你喜欢
  • 1970-01-01
  • 2017-04-29
  • 2015-07-04
  • 2021-02-02
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
相关资源
最近更新 更多