Java repaint()没有调用paintComponent()

时间:2015-01-31 03:21:15

标签: java swing paintcomponent repaint paintevent

为了尝试制作一个非常简单的子弹地狱游戏来学习java,我遇到了障碍:repaint()没有调用paintComponent()。

以下是整个程序,现在只需将我每秒创建50次的图像绘制到JPanel上,该图像依赖于JFrame。

/*
 * Bullet hell, by Nematodes
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class bulletHell extends JFrame
{
    private static final long serialVersionUID = 0L;
    JPanel gamePanel = new JPanel();
    int gameTimerDelay = 20;
    int x, y = 0;
    BufferedImage lightOrb;
    javax.swing.Timer gameTimer;

    public static void main(String[] args)
    {
        bulletHell createFrame = new bulletHell();
        createFrame.frameConstructor();
    }

    public void frameConstructor()
    {
        // Construct frame and frame components
        setTitle("Bullet hell");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new GridBagLayout());
        setVisible(true);

        GridBagConstraints gridConstraints;

        gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 0;
        gamePanel.setBackground(Color.BLACK);
        gamePanel.setPreferredSize(new Dimension(700, 700));
        getContentPane().add(gamePanel, gridConstraints);

        pack();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((int) (0.5 * (screenSize.width - getWidth())),
                 (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight());

        try
        {
            lightOrb = ImageIO.read(new File("C:/Users/Owner/Downloads/orb.bmp"));
        }
        catch(IOException e)
        {
            System.out.println("An issue occurred while trying to read orb.bmp");
        }

        // Start timer that draws game objects 50 times per second (50 FPS)
        gameTimer = new javax.swing.Timer(gameTimerDelay, gameTimerAction);
        gameTimer.setInitialDelay(0);
        gameTimer.start();
    }

    ActionListener gameTimerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            repaint();
        }
    };

    class GraphicsPanel extends JPanel
    {
        public GraphicsPanel()
        {

        }

        // Draw all of the components
        @Override
        public void paintComponent(Graphics g)
        {
            Graphics2D g2D = (Graphics2D) g;
            super.paintComponent(g2D);
            g2D.drawImage(lightOrb, x, y, this);
            g2D.dispose();
        }
    }
}

在使用断点和println方法进行一些调试之后,我可以确认正在读取正确的图像,gameTimerAction中的计时器每秒被调用50次,而repaint()根本不会调用paintComponent()。

我对Java编程有些新意,可能只是缺少一些简单的东西。

编辑:通过将gamePanel更改为GraphicsPanel对象已解决问题。不幸的是,这也意味着我更大的乒乓项目(这个项目有缺陷的绘图逻辑基本上是从中复制过来的)只能通过奇迹工作,并且在某些代码添加时可能会不稳定。

1 个答案:

答案 0 :(得分:3)

我可以立即看到几个问题:

  • 最重要:您永远不会实例化GraphicsPanel对象,也不会将其添加到任何内容中。永远不会在既未呈现也未创建的JPanel上调用paintComponent(...)方法。为什么不让你的gamePanel变量成为GraphicsPanel对象而不是JPanel对象?
  • 您永远不会更改计时器中的x和y,因此如果不进行更改,将不会发生任何动画。
  • 此外,您在JVM提供给您的Graphics对象上调用dispose,这是您永远不应该做的事情。这打破了Swing绘画链,使得GUI的图形不稳定。

所以坚持下去,你就会到达那里。

例如:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class BulletExample extends JPanel {
   public static final String IMG_PATH = "http://www.i2clipart.com/cliparts/f/0/5/8/clipart-blue-circle-f058.png";
   private static final int PREF_W = 700;
   private static final int PREF_H = PREF_W;
   private static final int TIMER_DELAY = 20;
   private BufferedImage bullet;
   private int bulletX;
   private int bulletY;

   public BulletExample() throws IOException {
      URL imgUrl = new URL(IMG_PATH);
      bullet = ImageIO.read(imgUrl);
      new Timer(TIMER_DELAY, new BulletListener()).start();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (bullet != null) {
         g.drawImage(bullet, bulletX, bulletY, this);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private class BulletListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         bulletX++;
         bulletY++;
         repaint();
      }
   }

   private static void createAndShowGui() throws IOException {

      // create the drawing JPanel
      BulletExample mainPanel = new BulletExample();

      JFrame frame = new JFrame("BulletExample");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      // add it to the JFrame
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            try {
               createAndShowGui();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      });
   }
}