RenderingHints do not work

hi, i’m trying to get rendering hints to work (and it seems they are not difficult to use ;)) but for some unknown reason they are ignored in my game.
i want to rotate an image on the fly and turn the following hint on in order to have smoother results:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

i compared a working example (http://www.rgagnon.com/javadetails/java-0248.html - the second one - i only added the line above) with my code and i cannot see a difference which seems to be relevant. but i’m most probably wrong.
some questions:
1: are there any components i should not use in connection with rendering hints (jframe, canvas, …)?
2: could BufferStrategy influence this particular part of the rendering process?
3: is Toolkit doing some magic? i use ImageIO …

here a screenshot to show what i mean:

http://www.rastaduck.org/tmp/rendering_hint_problem.jpg

any help is greatly appreciated!

grml … 1 minute after posting the topic above i did one final test and exchanged the image loading code in the tutorial code with my own (old: Toolkit, new: ImageIO) and now i have the same bad results. how come? i thought ImageIO is the preferred way to load images. ???
maybe i’m doing something wrong with ImageIO, i will browse the forums …

hmm … and it’s got something to do with this piece of code:
Image image = CONFIG.createCompatibleImage(sourceImage.getWidth(null),sourceImage.getHeight(null),trans);

if i comment that out, everything works fine, but do i still have accelerated graphics then? seems like my understanding of how things work was completely wrong… :-\

I have used VALUE_INTERPOLATION_BILINEAR to perform what I think you want to do. The code you linked to does not contain any stuff with RenderingHints, want to explain?

i know but i have written that i just added the renderinghint line above to the tutorial code and it worked. sorry for not pointing that out clearer …

Oh, my bad then. Try posting your code, that might reveal the reason. The createCompatibleImage stuff shouldn’t itself produce a problem AFAI can see.

Could be a difference in the versions of JRE?

i compiled and executed the tutorial code in exactly the same way as my game code.

in the meantime i found out that it’s not the Toolkit/ImageIO thing i mentioned above.

@hjorth: here’s the code:


sprite loading code:
====================

protected static final GraphicsDevice DEVICE;
protected static final GraphicsConfiguration CONFIG;
    
static {
    DEVICE = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    CONFIG = DEVICE.getDefaultConfiguration();
}

// ...

public Sprite getSprite(String ref, int trans) {
    BufferedImage sourceImage = null;
            
    try {
        URL url = this.getClass().getClassLoader().getResource(ref);
		
        if (url == null) {
            fail("Can't find ref: "+ref);
        }
        sourceImage = ImageIO.read( 
            new BufferedInputStream(this.getClass().getClassLoader().getResourceAsStream(ref))
        ); 
        
    } catch (Exception e) {
        fail("Failed to load: "+ref);
        e.printStackTrace();
    }

// solution 1 - old/nok
//    Image image = CONFIG.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),trans);
//    image.getGraphics().drawImage(sourceImage,0,0,null);
//    Sprite sprite = new Sprite(image, ref);
        
// solution 2 - new/ok
    Sprite sprite = new Sprite(sourceImage, ref);
        
    sprites.put(ref,sprite);
	
    return sprite;
}
	
	
	
drawing code:
===========	

public void drawRotated(Graphics2D g2d, int x, int y, double angle) {
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    
    AffineTransform origXform = g2d.getTransform();
    AffineTransform newXform = (AffineTransform)(origXform.clone());

    int xRot = (int) (x + getWidth()  / 2);
    int yRot = (int) (y + getHeight() / 2);

    newXform.translate(-halfWidth, -halfHeight);
    newXform.rotate(-angle, xRot, yRot);

    g2d.setTransform(newXform);
    g2d.drawImage(image, x, y, null);
    g2d.setTransform(origXform);
}

I don’t know about the bufferedinputstream:


sourceImage = ImageIO.read(
            new BufferedInputStream(this.getClass().getClassLoader().getResourceAsStream(ref))
        ); 

I use this, and it works for me:

BufferedImage SPRITE = ImageIO.read(getClass().getResource("sprite.gif"));

are you using double-buffering strategy?

your method does not work for me. i get
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1354)

… although i only changed this piece of code:

sourceImage = ImageIO.read(new BufferedInputStream(this.getClass().getClassLoader().getResourceAsStream(ref)));

… to that one:

sourceImage = ImageIO.read(getClass().getResource(ref));

about the bufferstrategy: yes i use it:

canvas.createBufferStrategy(2);

getClass().getResource(…); obtains the resource using the full path, so you have to specify C:/mygames/ThisGame/stuff/sprite.png

whereas getClass().getClassLoader().getResource(…); obtains the resource relative to the top most package, so if you have com/mygames/thisGame and next to com, you have data/sprite.png, all you have to do is:

getClass().getClassLoader().getResource(“data/spring.png”);

DP

Wondering what Sprite does with the BufferedImage (after you’ve loaded the image):


Sprite sprite = new Sprite(sourceImage, ref);

And where do you define “image” in this? (when drawing the image)


g2d.drawImage(image, x, y, null);

The BufferedInputStream is needed to work around a bug in ImageIO. Look at the sticky post ImageIO / Java Web Start bug: Workaround.

a Sprite is just a wrapper for an Image object. i don’t modify the Image object between loading and drawing. do you see any mistakes in my code?

by the way, rotations are not hardware accelerated (even if you do use createCompatibleImage()). your best bet is to enable the OpenGL pipline or use LWJGL or the like.

remove the code that is enabling the image to be HW accelerated and you should be fine

Try filling the window with a white rectangle, and then draw the image on that background.

yes, i noticed that. when i remove the code, it works just fine but performance severely goes down. i tried that in my chopper game and the framerate went down to 7 fps or so. :frowning:

you mean no transparency around the turtle image? no, you can’t mean that …
???

I think it needs something to have been drawn in the background so it get blend the translucent pixels of the image with it.

ah, now i got it…