保存的BufferedImage中仅显示最后一个圆圈

时间:2016-10-25 16:43:28

标签: java graphics

我想在BufferedImage中渲染n个圆圈,但我的问题是只有最后一个圆圈可见。你能帮我找到失败的吗?也许它与我正在使用的循环有关。提前谢谢。

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 个答案:

答案 0 :(得分: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()时,最后一个圈&#34;擦除&#34;上一个圈子。

结果:

enter image description here