2012-11-18 28 views
1

我使用RadialGradientPaint創建了一個cricle漸變,將它放在BufferedImage上,並在我的2D遊戲屏幕上渲染圖像,創建一個很好的光照效果。然而,我想創建更多的光源,但是爲每個燈創建和渲染新的BufferedImage並不能完成這項工作(通常只是看到最後一個燈,其他所有燈都是黑色的)。是否可以將一些RadialGradientPaints烘焙成一個BufferedImage或以其他方式實現多燈效果?一個BufferedImage上的多個漸變

附加,你可以找到一個燈的形象。這是一個黑色的BufferedImage,在屏幕頂部應用了RadialGradientPaint。我想以某種方式添加更多。

Single light

+1

來自不同光源一起添加的掩模,夾在[255](不太現實的,但它有助於東西),由乘地圖。雖然不知道如何在Slick中做到這一點。 –

+0

這裏所做的唯一事情就是將BufferedImage轉換成它自己的Image類並調用Image的draw()函數。所以,如果你可以給我提供一些關於如何做的例子代碼,我將能夠以某種方式使用浮油渲染輸出BufferedImage。 – Myzreal

+1

請參閱[Java - 合併兩個圖像](http://stackoverflow.com/questions/2318020/merging-two-images)。您應該能夠在黑色畫布上繪製幾個白色透明漸變,從黑白轉換爲黑色並在地圖上繪製。 –

回答

2

解決這個問題是使用這個(如在由@JanDvorak評論指出:Merging two images

我使用完全相同的代碼是這樣的:

public static BufferedImage mergeImages2(BufferedImage base, Collection<Light> images) { 
    BufferedImage output = new BufferedImage(base.getWidth(), base.getHeight(), BufferedImage.TYPE_INT_ARGB); 

    Graphics2D g = output.createGraphics(); 
    g.drawImage(base, 0, 0, null); 
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 1.0f)); 
    for (Light l : images) { 
     g.drawImage(l.LIGHT_IMAGE, l.x, l.y, null); 
     l.LIGHT_IMAGE.flush(); 
    } 
    g.dispose(); 
    output.flush(); 
    return output; 
} 

注意:這解決了問題,但產生內存泄漏,我已經描述了希望在這裏得到一些幫助:BufferedImage.createGraphics() memory leak

0

Ano其他方法是以兩個不同的方式混合你的圖像。 第一遍,您只需在白色綠色透明膠片上添加黑色矩形上的多點輻射點即可。

然後,在同一點上,您從第1步挖出第一個圖像並進行XOR混合。

結果:

http://img11.imageshack.us/img11/7066/step3yr.png

的代碼:

public void creerLightMap(){ 
     lightMap = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g = lightMap.createGraphics(); 
     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
     float radius = 100; 
     //create color circle 
     for(int i=0; i<lumiere.size(); i++){ 
      Point2D center = lumiere.get(i); 
      float[] dist = {0.0f, 0.5f, 1.0f}; 
      Color[] colors = {new Color(1.0f , 1.0f , 1.0f , 1.0f), new Color(0.0f , 1.0f , 0.0f , 0.5f) , new Color(0.0f , 1.0f , 0.0f , 0.0f)}; 
      RadialGradientPaint p = new RadialGradientPaint(center, radius, dist, colors); 
      g.setPaint(p); 
      g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
     } 

     //add an alpha into theses same circle 
     for(int i=0; i<lumiere.size(); i++){ 
      Point2D center = lumiere.get(i); 
      float[] dist = {0.0f, 1.0f}; 
      Color[] colors = {new Color(1.0f , 1.0f , 1.0f , 1.0f) , new Color(1.0f , 1.0f , 1.0f , 0.0f)}; 
      RadialGradientPaint p = new RadialGradientPaint(center, radius, dist, colors); 
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.XOR, 1.0f)); 
      g.setPaint(p); 
      g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
     } 
     g.dispose(); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     Graphics2D g2d = (Graphics2D)g; 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.drawImage(this.lightMap, 0, 0, null); 
    }