2016-10-25 53 views
0

我想在BufferedImage中渲染n個圓圈,但我的問題是隻有最後一個圓圈纔可見。你能幫我找到我的失敗嗎?也許它與我正在使用的循環有關。提前致謝。只保存最後一個圓圈BufferedImage

Scene.java

public class Scene { 
    private final int width; 
    private final int height; 
    private BufferedImage imageOfScene; 
    private ArrayList<Geometry> geometries; 
    private final String folderToSaveIn = "doc/"; 
    private final String fileName = "a02.png"; 

    public Scene(int width,int height){ 
     this.geometries = new ArrayList<Geometry>(); 
     this.width = width; 
     this.height = height; 
     this.imageOfScene = new BufferedImage(
      this.width, this.height, BufferedImage.TYPE_INT_RGB 
     ); 
     System.out.println("The scene has a size of:"+width+"X"+height); 
    } 

    public void renderScene(){ 
     for (int x = 0; x != width; x++) { 
      for (int y = 0; y != this.height; y++) { 
       for (Geometry geometry: this.geometries){ 
        if(geometry.isHit(x,y)){ 
         this.imageOfScene.setRGB(x, y, geometry.getColor().getRGB()); 
        } 
        else 
         this.imageOfScene.setRGB(x,y,giveColorForThisPixel(y)); 
       } 
      } 
     } 
    } 

    public int giveColorForThisPixel(int y){ 
     double y2 = (double) y; 
     double t = y2/this.height; 
     int value = (int) (t* 255); 
     return new Color(0, 0, value).getRGB(); 
    } 

    public void saveScene(){ 

     try { 
      File outputfile = new File(this.folderToSaveIn +fileName); 
      ImageIO.write(this.imageOfScene, "png", outputfile); 
      System.out.println("Wrote image: " + fileName); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    public void addGeometry(Geometry geometry){ 
     geometries.add(geometry); 
    } 


    public int getWidth() { 
     return width; 
    } 

    public int getHeight() { 
     return height; 
    } 

    public ArrayList<Geometry> getGeometries() { 
     return geometries; 
    } 
} 

Circle.java

public class Circle extends Geometry { 

    private final int radius; 
    private final int centerX; 

    private final int centerY; 

    public Circle(int centerX, int centerY, int radius, Color color){ 
     super(color); 
     this.radius = radius; 
     this.centerX = centerX; 
     this.centerY = centerY; 
    } 

    @Override 
    public boolean isHit(int x,int y) { 
     if(Math.pow((x - this.centerX),2) + Math.pow((y - this.centerY),2) < Math.pow(this.radius,2)) 
      return true; 
     else 
      return false; 
    } 

    @Override 
    public String toString() { 
     return "Circle{" + 
       "radius=" + radius + 
       ", centerX=" + centerX + 
       ", centerY=" + centerY + 
       ", color=" + getColor() + 
       '}'; 
    } 
} 

Main.java

public class Main { 

    static String name = "doc/a02.png"; 
    static int  width = 480; 
    static int  height = 270; 
    static int widthRatio = 16; 
    static int heightRatio = 9; 

    public static void main(String[] args) { 


     int numberOfCircles = 2; 
     Random random = new Random(); 
     int ratioMultiplier = ThreadLocalRandom.current().nextInt(30,121); 
     System.out.println(ratioMultiplier); 
     //Scene scene = new Scene(width,height); 
     Scene scene = new Scene(widthRatio*ratioMultiplier,heightRatio*ratioMultiplier); 
     //ThreadLocalRandom.current().nextInt(min, max + 1); 

     for (int counter = 1; counter <= 5; counter++){ 
      int centerX = ThreadLocalRandom.current().nextInt(0,height+1); 
      int centerY = ThreadLocalRandom.current().nextInt(0,width+1); 
      int radius = ThreadLocalRandom.current().nextInt(20,80); 
      scene.addGeometry(new Circle(centerX,centerY,radius,getRandomColor())); 
      System.out.println(scene.getGeometries()); 
     } 

     scene.renderScene(); 
     scene.saveScene(); 
    } 
    static Color getRandomColor(){ 
     return new Color(
      ThreadLocalRandom.current().nextInt(0,256), 
      ThreadLocalRandom.current().nextInt(0,256), 
      ThreadLocalRandom.current().nextInt(0,256) 
     ); 
    } 
} 

回答

1

的問題是在renderScene()方法。更改它:

public void renderScene(){ 
     for (int x = 0; x != width; x++) { 
      for (int y = 0; y != this.height; y++) { 
       this.imageOfScene.setRGB(x, y, giveColorForThisPixel(y)); 
       for (Geometry geometry: this.geometries) { 
        if (geometry.isHit(x, y)) { 
         this.imageOfScene.setRGB(x, y, geometry.getColor().getRGB()); 
        } 
       } 
      } 
     } 
    } 

當你的代碼執行else this.imageOfScene.setRGB(x,y,giveColorForThisPixel(y));renderScene()方法,最後一圈「擦除」以前的圈子。

結果:

enter image description here

+0

這並不解決問題。我認爲只計算一個像素的顏色,如果它不在幾何體上,而是先渲染背景,然後渲染幾何體,則更有意義。 – andreask

+0

我更新了答案。 – Paulo

+0

是的,你是對的。起初它不工作,但現在它工作。 – andreask