java - 为什么屏幕不变色

标签 java jframe awt

我看了看,代码对我来说似乎很好。出现错误,但希望是源代码,而不是我的 cpu 或 JDK 有问题。

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.*;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
public static int width = 300;
public static int height = width / 16*9;
public static int scale = 3;

private Thread thread;
private boolean running = false;
private JFrame frame;

public synchronized void start() {
    running = true;
    thread = new Thread(this, "Display");
    thread.start();
}
public synchronized void stop() {
    running = false;

    try{
        thread.join();
    }catch(InterruptedException e){
        e.printStackTrace();
    }
}

public void run(){
    while(running){
        tick();
        render();
    }
}

public void tick() {

}

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if(bs==null){
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.black);
    g.fillRect(0, 0, getWidth(), getHeight());
    bs.dispose();
    bs.show();

}

public Game() {
    Dimension size = new Dimension(width * scale, height * scale);
    setPreferredSize(size);

    frame = new JFrame();
}

public static void main(String[] args) {
    Game game = new Game();

    game.frame.setResizable(false);
    game.frame.setTitle("Title");
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);

    game.start();
}
}

然后我得到了这个错误,即使我无数次地修改了我的源代码。

Exception in thread "Display" java.lang.NullPointerException
at java.awt.Component$BltBufferStrategy.showSubRegion(Component.java:4307)
at java.awt.Component$BltBufferStrategy.show(Component.java:4255)
at com.thecherno.Rain.Game.render(Game.java:58)
at com.thecherno.Rain.Game.run(Game.java:39)
at java.lang.Thread.run(Thread.java:695)

我开始怀疑是不是因为 JDK 过时了。我当前的版本是 JDK 6。

最佳答案

您声明:

What Im trying to do is change color as seen in the render method. The background to be black.

  • 使用 Swing 组件,例如 JComponent 或 JPanel。
  • 只需在组件上调用 setBackground(Color.BLACK) 即可。
  • 您似乎正在创建某种类型的游戏循环。考虑为此使用 Swing 计时器。

例如,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Game2 extends JPanel {
   private static final int PREF_W = 300;
   private static final int PREF_H = PREF_W / 16 * 9;
   private static final int SCALE = 3;
   private static final Color BACKGROUND = Color.BLACK;
   private static final int TIMER_DELAY = 20;
   private Timer swingTimer;

   public Game2() {
      setBackground(BACKGROUND);
      swingTimer = new Timer(TIMER_DELAY, new TimerListener());
      swingTimer.start();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);   
      // TODO: add any custom painting here
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W * SCALE, PREF_H * SCALE);
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         // TODO add code that gets called in game loop
      }
   }

   private static void createAndShowGui() {
      Game2 mainPanel = new Game2();

      JFrame frame = new JFrame("Game2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

请注意,此代码基于您声明的要求,我猜测是基于您的代码的其他要求。如果还有其他未提及的要求,请为我们详细说明。

关于java - 为什么屏幕不变色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20722194/

相关文章:

java - REST:客户端可以通过 POST 传递包装对象吗?

java - 如何将多个组件添加到 JFrame?

java - JPanel 绘图不工作

java - 使用 java.awt.Desktop 打开文件,同时重定向它的 stderr?

java - 为什么我的Font是从文件创建的,为什么还要调用GraphicsEnvorinment.registerFont()?

java - 拖放机器人类

java - 覆盖具有完全相同内容的函数会导致错误

Java - JTable 默认选择第一列

java - 单击后重命名按钮 - Java JButton

java - 甲骨文错误日志文件