0
\$\begingroup\$

Why when using this code for the background for my game do I receive blocks with blurred edges?

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

Like this

enter image description here

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Most likely it is antialiasing the image, in an attempt to avoid looking pixelated. If you have a Graphics2D called g2, then you can modify the settings:

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
      RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);

The abstract Graphics class does not have this method, but the Graphics object you have likely is a Graphics2D internally.

\$\endgroup\$
3
  • \$\begingroup\$ I thought it may be something like that but did not know any of the commands. Funny thing is the rest of render code is just Graphics g=screen.getGraphics();Render(g) - and therefore I am not using a Graphics2d object. Anyway I can do this with a Graphics class? \$\endgroup\$
    – Munkybunky
    Nov 19, 2014 at 11:35
  • \$\begingroup\$ Hi,I have just tried converting it to a Graphics2D class and used " g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);" which has fixed the problem. Is there no way to do the same thing for a Graphics class? Thanks... \$\endgroup\$
    – Munkybunky
    Nov 19, 2014 at 13:00
  • \$\begingroup\$ Graphics simply lacks the setRenderingHint method. This is the way the designers of Java structured things, for some reason which I don't know. \$\endgroup\$
    – user41442
    Nov 19, 2014 at 23:12
1
\$\begingroup\$

It looks like you are first rendering the buffered image with bilinear interpolation and the result is later stretched with nearest neighbor filtering. It's hard to say without seeing the rest of the rendering code.

When I copied your code and used it to fill the background of a JFrame, I got either large solid color quads or fully smooth image depending whether I used VALUE_INTERPOLATION_NEAREST_NEIGHBOR or VALUE_INTERPOLATION_BILINEAR as the interpolation rendering hint.

\$\endgroup\$
1
  • \$\begingroup\$ I thought it may be something like that but did not know any of the commands. Funny thing is the rest of render code is just Graphics g=screen.getGraphics();Render(g) - and therefore I am not using a Graphics2d object. \$\endgroup\$
    – Munkybunky
    Nov 19, 2014 at 11:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .