为什么屏幕不会改变颜色

时间:2013-12-21 18:32:08

标签: 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.

2 个答案:

答案 0 :(得分:1)

你说:

  

我尝试做的是改变颜色,如render方法中所示。背景是黑色的。

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

如,

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();
         }
      });
   }
}

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

答案 1 :(得分:0)

尝试使用g.dispose();,然后使用bs.show();然后 g = (Graphics2D)bs.getDrawGraphics();。我知道它看起来很奇怪,但你正在清空画布然后用你的策略重新填充它。您可能还需要对g为null进行初始检查,并在第一个显示循环之前对其进行初始化。