2017-03-26 2 views
0

Im Moment wollte ich nur mein Spielfenster auf dem Bildschirm sehen und ich dachte, alles wäre gut, abgesehen von der Tatsache, dass die Farbe rot nicht auf dem ganzen Fenster angezeigt wird. Ich bin mir nicht sicher warum, ich habe meinen Code ein paar Mal durchgegangen und kann nichts herausfinden. Dies ist mein Code:Java - Grafikpixel werden nicht korrekt aktualisiert?

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

Antwort

1

Sie haben versehentlich GAME_WIDTH als height Argument des BufferedImage Konstruktoraufruf geben:

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

Sie wahrscheinlich GAME_HEIGHT verwenden gemeint:

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

Nun, ich fühle mich wie ein Idiot. Ich habe fast eine Stunde lang über meinen Code nachgedacht. Ich schätze, das passiert, wenn du trinkst und lutest. – Vince

+0

Heh. Manchmal braucht es nur ein weiteres Augenpaar. Übrigens bin ich mir nicht sicher, welche IDE Sie verwenden, aber IntelliJ IDEA zeigt in diesem Fall tatsächlich eine Warnung an: http://i.imgur.com/CuHKTxM.png. Wenn Sie mit der Antwort zufrieden sind, können Sie sie akzeptieren, indem Sie auf das Häkchen auf der linken Seite klicken. – cubrr

+0

Ich benutze NetBeans, aber ich werde das untersuchen. – Vince

Verwandte Themen