2013-12-14 86 views
-1

希望你能幫助我。像素應該呈現,但它不是

我參與了Ludum Dare 28,我碰到一個問題,我將像素渲染到屏幕上,他們應該出現,但他們不是?

我是新來的這一點,我不明白的編碼插入,所以我希望你不介意我送你去一個gyazo linkg(屏幕截圖上傳)

我有我的代碼如前所述,它應該是渲染像素。但事實並非如此。

(希望我有這個代碼的東西的工作*)我也沒有錯誤出現在日食

Screen.java

 package com.cmnatic.cmnatic.graphics; 

    public class Screen { 

     private int width, height; 
     public int[] pixels; 

     int xtime = 0, ytime = 50; 
     int counter = 0; 

     public Screen(int width, int height) { 
      this.width = width; 
      this.height = height; 
      pixels = new int[width * height]; // 0 - 50,399 = 50,400 
     } 

     public void clear() { 
      for (int i = 0; i < pixels.length; i++) { 
       pixels[i] = 0; 
      } 
     } 

     public void render() { 
      counter++; 
      if (counter % 100 == 0) { 
       xtime++; 
      if (counter % 100 == 0) { 
       ytime++; 

      for (int y = 0; y < height; y++) { 
       if (ytime >= height) break; 
       for (int x = 0; x < width; x++) { 
        if (xtime >= width) break; 
        pixels[xtime + ytime * width] = 0xff00ff; 
       } 

      } 
     } 
    } 
    } 
    } 

          Game.java 



    package com.cmnatic.cmnatic; 

import java.awt.Canvas; 
import java.awt.Color; 
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; 

import com.cmnatic.cmnatic.graphics.Screen; 

public class Game extends Canvas implements Runnable { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private static final Screen Screen = null; 
    public static int width = 300; 
    public static int height = width/16 * 9; // 168 
    public static int scale = 3; 

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

    private Screen screen; 

    private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
    private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

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

     screen = new Screen(width, height); 

     frame = new JFrame(); 
    } 

    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 == true) { 
      update(); 
      render(); 
     } 
    } 

    public void update() { 

    } 

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

     screen.clear(); 

     screen.render(); 

     for (int i = 0; i < pixels.length; i++) { 
      pixels[i] = screen.pixels[i]; 
     } 

     Graphics g = bs.getDrawGraphics(); 
     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 
     g.dispose(); 
     bs.show(); 
    } 

    public static void main(String[] args) { 
     Game game = new Game(); 
     game.frame.setResizable(false); 
     game.frame.setTitle("The Last Hit"); 
     game.frame.add(game); 
     game.frame.pack(); 
     game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     game.frame.setLocationRelativeTo(null); 
     game.frame.setVisible(true); 

     game.start(); 
    } 

    public static Screen getScreen() { 
     return Screen; 
    } 

} 
+0

您發佈了兩個屏幕類。你有多少? –

+0

*「我把你送到gyazo linkg」*我不知道heck是一個'gyazo linkg',但這聽起來很痛苦。你可以把它翻譯成*英語嗎?* –

+0

我決定不加入一個:P它是一個屏幕截圖上傳器,所以你在屏幕上選擇一個區域並上傳它,以便快速和輕鬆地共享。 另外@ElliottFrisch我更新了代碼,很抱歉。 對不起,如果不明確。 – user2908236

回答

0

this javadoc。具體來說,這句話:必須重寫paint方法才能在畫布上執行自定義圖形。

然後覆蓋您的Game類(其類型爲Canvas)中的paint。我認爲它應該開始顯示一些東西。

// That is, something like this - 
public void paint(Graphics g) { 
    super.paint(g); 
    g.setColor(Color.BLACK); 
    g.fillRect(0, 0, getWidth(), getHeight()); 
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 
} 
+0

@ElliotFrisch我會如何去做這件事?或者我只是非常愚蠢的權利現在呢? – user2908236

+0

添加一個帶有這個簽名的方法到Game'public void paint(Graphics g){super.paint(g); // DRAW HERE}' –

+0

「構造函數調用必須是構造函數」 – user2908236

相關問題