2017-03-26 11 views
0

今は画面にゲームウィンドウを表示したかっただけですが、赤色がウィンドウ全体に表示されないという点を除いて、すべてうまくいくと思いました。理由は分かりませんが、私はコードを何度か行ってしまい、何も理解できません。Java - グラフィックスピクセルが正しく更新されていませんか?

package game; 

import java.awt.BorderLayout; 
import java.awt.Canvas; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 
import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferInt; 
import javax.swing.JFrame; 

public class Game extends Canvas implements Runnable { 
    private final String GAME_NAME; 
    private final int GAME_WIDTH; 
    private final int GAME_HEIGHT; 
    private final int SCALE; 

    private boolean isRunning; 
    private JFrame frame; 
    private BufferedImage image; 
    private int[] pixels; 

    public Game(String name, int width, int aspectRatio[], int scale) { 
     // Variabe Initialization 
     this.GAME_NAME = name; 
     this.GAME_WIDTH = width; 
     this.GAME_HEIGHT = width/aspectRatio[0] * aspectRatio[1]; 
     this.SCALE = scale; 

     this.image = new BufferedImage(GAME_WIDTH, GAME_WIDTH, BufferedImage.TYPE_INT_RGB); 
     this.pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

     this.frame = new JFrame(GAME_NAME); 

     Init(); 
    } 

    private void Init() { 
     // Frame Setup 
     setMinimumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE)); 
     setMaximumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE)); 
     setPreferredSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE)); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new BorderLayout()); 

     frame.add(this, BorderLayout.CENTER); 
     frame.pack(); 

     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private void Start() { 
     isRunning = true; 
     new Thread(this).start(); 
    } 

    private void Stop() { 
     isRunning = false; 
    } 

    @Override 
    public void run() { 
     long lastTime = System.nanoTime(); 
     long timer = System.currentTimeMillis(); 

     final double ns = 1000000000d/60d; 
     double delta = 0d; 

     int frames = 0; 
     int updates = 0; 

     while (isRunning) { 
      long now = System.nanoTime(); 
      delta += (now - lastTime)/ns; 
      lastTime = now; 

      while (delta >= 1) { 
       updates++; 
       Update(); 
       delta--; 
      } 

      Render(); 
      frames++; 

      if (System.currentTimeMillis() - timer > 1000) { 
       timer += 1000; 
       frame.setTitle(GAME_NAME + " | " + updates + " ups, " + frames + " fps"); 

       updates = 0; 
       frames = 0; 
      } 
     } 
    } 

    public void Update() { 
     for (int y = 0; y < GAME_HEIGHT; y++) { 
      for (int x = 0; x < GAME_WIDTH; x++) { 
       pixels[x + y * GAME_WIDTH] = 0xffff0000; 
      } 
     } 
    } 

    public void Render() { 
     BufferStrategy bs = getBufferStrategy(); 

     if (bs == null) { 
      createBufferStrategy(3); 
      requestFocus(); 
      return; 
     } 

     Graphics g = bs.getDrawGraphics(); 
     { 
      g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 
     } 
     g.dispose(); 
     bs.show(); 
    } 

    public static void main(String[] args) { 
     new Game("Game", 160, new int[]{12, 9}, 1).Start(); 
    } 
} 

答えて

1

あなたが誤ってBufferedImageコンストラクタ呼び出しのheight引数としてGAME_WIDTHに合格しました:これは私のコードであるあなたは、おそらく使用することを意図し

public Game(String name, int width, int aspectRatio[], int scale) { 
    // Variabe Initialization 
    this.GAME_NAME = name; 
    this.GAME_WIDTH = width; 
    this.GAME_HEIGHT = width/aspectRatio[0] * aspectRatio[1]; 
    this.SCALE = scale; 

    this.image = new BufferedImage(GAME_WIDTH, GAME_WIDTH, BufferedImage.TYPE_INT_RGB); 
    //           ^^^^^^^^^^ 
    this.pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

    this.frame = new JFrame(GAME_NAME); 

    Init(); 
} 

GAME_HEIGHT

this.image = new BufferedImage(GAME_WIDTH, GAME_HEIGHT, BufferedImage.TYPE_INT_RGB); 
+0

まあI馬鹿のように感じる。私は約1時間私のコードを見てきました。私はそれがあなたの飲酒とコーディングの笑いが起こるときに起こると思います。 – Vince

+0

Heh。時にはもう一組の目がかかることもあります。ところで、どのIDEを使用しているのかわかりませんが、IntelliJ IDEAは実際にはこの場合警告を表示します:http://i.imgur.com/CuHKTxM.png。答えに満足すれば、左側のチェックマークをクリックして回答を受け入れることができます。 – cubrr

+0

私はNetBeansを使用していますが、その点について検討します。 – Vince

関連する問題