2016-08-07 44 views
0

我正在關注一個Java遊戲開發教程,並且我遇到了一個障礙,因爲我無法弄清楚我的代碼出了什麼問題。我應該通過將緩衝圖像的每個像素改變爲我想要的顏色來渲染一幅新圖像。然後我將該數組複製到我的Game類中的數組,然後繪製渲染的圖像。相關代碼直接在下面。我會在代碼片段之後直接討論我所嘗試的內容。爲什麼在用洋紅色的替換BufferedImage對象的所有像素後,我會得到黑色圖像?

此代碼段是從我Game類:

private Screen screen; 
private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 

// Converts image into array of integers 
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(); 
    pixels = new int[width * height]; 
} 


// Displays images to the screen 
public void render() { 
    BufferStrategy bs = getBufferStrategy(); 

    // Checks if the buffer strategy exists. If it doesn't create a triple buffer strategy 
    if(bs == null) { 

     // Triple buffering 
     createBufferStrategy(3); 
     return; 
    } 
    screen.render(); 

    // Copies array in Screen class to pixels array in (this) class 
    for (int i = 0; i < pixels.length; i++) { 
     pixels[i] = screen.pixels[i]; 
    } 

    // Must be in chronological order 
    Graphics g = bs.getDrawGraphics(); 
    g.setColor(Color.CYAN); 
    g.fillRect(0, 0, getWidth(), getHeight()); 
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 
    g.dispose(); 

    // Display next available buffer 
    bs.show(); 
} 

此代碼段是從我Screen類:

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

public Screen(int width, int height) { 
    this.width = width; 
    this.height = height; 

    // Size of the pixels array reserves one element for each pixel 
    pixels = new int[width * height]; 
} 

public void render() { 
    for (int y = 0; y < height; y++) { 
     for (int x = 0; x < width; x++) { 
      pixels[x + y * width] = 0xff00ff; 
     } 
    } 
} 

我已經通過檢查試圖 「調試」如果GameScreen類中的pixels陣列都加載了洋紅色。我壓根兒:

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

,我也試過:

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

在這兩種情況下,紅色的十進制表示(FF00FF十六進制)被打印出來,直到我關閉該窗口。

我試過的另一件事是使用image.setRGB方法來改變圖像的顏色。

我加入這一行

image.setRGB(20, 20, 16711935);到渲染方法如下:

// Displays images to the screen 
public void render() { 
    BufferStrategy bs = getBufferStrategy(); 

    // Checks if the buffer strategy exists. If it doesn't create a triple buffer strategy 
    if(bs == null) { 

     // Triple buffering 
     createBufferStrategy(3); 
     return; 
    } 
    screen.render(); 

    // Copies array in Screen class to pixels array in Game (this) class 
    /* for (int i = 0; i < pixels.length; i++) { 
     pixels[i] = screen.pixels[i]; 
    } 
    */ 

    // Must be in chronological order 
    Graphics g = bs.getDrawGraphics(); 
    g.setColor(Color.CYAN); 
    g.fillRect(0, 0, getWidth(), getHeight()); 
    image.setRGB(20, 20, 16711935); 
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 
    g.dispose(); 

    // Display next available buffer 
    bs.show(); 
} 

用品紅色點在我的框架出現了,而且我還能夠把整個框架成固體品紅通過修改Screen類中的render方法來修改顏色。現在,我想知道爲什麼我的「原始」代碼無法正常工作。據我所知,setRGB方法比我最初用來渲染圖像的速度慢得多,所以我不想放棄並用setRGB來解決。

我幾乎花了整整一天的時間去檢查我的代碼中的所有拼寫,確保在Screen類中嵌套的循環是正確的,等等。我在這裏感到茫然。我不明白這個問題是什麼。

如果我收錄的代碼片段不夠,我已經創建了我的代碼的完整主題here

+0

透明度字節洋紅色色詮釋? –

回答

0

這段代碼獲得了輸入的RGB顏色

public static int getIRGB(int Red, int Green, int Blue){ 
    Red = (Red << 16) & 0x00FF0000; //Shift red 16-bits and mask out other stuff 
    Green = (Green << 8) & 0x0000FF00; //Shift Green 8-bits and mask out other stuff 
    Blue = Blue & 0x000000FF; //Mask out anything not blue. 

    return 0xFF000000 | Red | Green | Blue; //0xFF000000 for 100% Alpha. Bitwise OR everything together. 
} 

我懷疑你是畫有0%不透明度

相關問題