模糊的图像而不是纯色?

时间:2014-11-17 20:54:55

标签: java bufferedimage

我正在使用BufferedImage作为我的背景但是当我使用以下代码重现它以使其变大时为什么我的图像代码会产生模糊边缘而不是纯色?

public final class State_Play extends State_Template {
    private BufferedImage background;
    private int[] pixelelements;
    private int width,height;

    State_Play(){
        init();
    }

    public void init(){
        width=8; //Configuration.appwidth,
        height=6; //Configuration.appheight
        background=new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
        pixelelements=((DataBufferInt) background.getRaster().getDataBuffer()).getData();
        maketuringarray();
    }

    public void maketuringarray(){      
       int singlecolor=new Random().nextInt();
       pixelelements[new Random().nextInt(width*height)]=singlecolor | 0xFF000000;  
    }

    @Override
    public void update() {
       maketuringarray(); 
    }

    @Override
    public void render(Graphics g) {
        g.drawImage(background, 0, 0, Configuration.appwidth, Configuration.appheight, null);  
    }

而不是纯色?

1 个答案:

答案 0 :(得分:1)

最有可能的问题是平台上的插值默认设置为立方或双三次。这将使模糊变得模糊。

明确地将其设置为最近邻居,以获得您想要的“块状像素”效果(如果我理解正确的话):

@Override
public void render(Graphics g) {
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g.drawImage(background, 0, 0, Configuration.appwidth, Configuration.appheight, null);  
}