2016-10-25 6 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()方法です。それに変更します:あなたのコードがrenderScene()メソッドの内部else this.imageOfScene.setRGB(x,y,giveColorForThisPixel(y));を実行すると

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

、最後の円は、前の円を「消去します」。

結果:問題を解決していません

enter image description here

+0

。そして、私は、それが幾何学的にではなく、その代わりに背景をレンダリングし、その後にジオメトリをレンダリングする場合にのみ、ピクセルの色を計算するほうが理にかなっていると思います。 – andreask

+0

私は答えを更新しました。 – Paulo

+0

はい、そうです。最初は動作しませんが、今は動作しています。 – andreask

関連する問題