[java] image rotation

Started by
5 comments, last by virtuosity 23 years, 9 months ago
I''ve been trying to rotate a bitmap using a SpinFilter class available on the webpage http://hplasim2.univ-lyon1.fr/c.ray/bks/java/htm/ch51.htm but I haven''t been able to display the rotated image. The code I used is as follows: Image image = getImage(getDocumentBase(),"wraith.gif"); Image filteredImage = createImage(new FilteredImageSource(image.getSource(),new SpinFilter(90.0)); I can draw the unfiltered original image with no problems but when I try to draw filteredImage, nothing is drawn. What am I doing wrong? Thanks in advance, virtuosity
Advertisement
It''s been my recent experience with images that if it comes out blank, something along the way is null. Try making each variable individually and doing
System.out.println(var);
to see if it''s null. For example:

ImageProducer producer = image.getSource();
System.out.println(producer);
...
Image filtered = Toolkit.getDefaultTooklit().createImage(...);

Also, if those two lines are right next to each other in the source, then it''s probably that the image has not yet been fully loaded, you can use a media tracker to wait untill it''s loaded, but if you only have one image you might try

while ( image.getHeight(null) == -1) ;

before trying to get it''s source pixels. While an Image is loading it''s height & width are -1.
quote:Original post by Jim_Ross

Also, if those two lines are right next to each other in the source, then it''s probably that the image has not yet been fully loaded, you can use a media tracker to wait untill it''s loaded,


I second this. My gut feeling is your image probably hasn''t been loaded yet.

ManaSink


I modified my code as follows and I was able to draw the image:
Image image = getImage(getDocumentBase(),"wraith.gif");
while ( image.getHeight(null) == -1) ;
ImageProducer producer = image.getSource();
ImageFilter filter = new SpinFilter(Math.PI/2);
ImageProducer filterProducer = new FilteredImageSource(producer,filter);
Image filteredImage = createImage(filterProducer);

I think the key thing was to make each of the variables individually. If I don''t wait for the image to load, I can still draw the image, albeit with a small delay. Thanks a lot for the suggestions Jim_Ross.
Now that I can rotate images and draw them, I have another problem. What I''ve been attempting to do is to rotate an image a full 360 degrees in an animation sequence. The problem is that when I rotate the image, it seems that the center of the image doesn''t remain in the same place even though I''m drawing all the rotated images in the same location. This results in a jerky rotation animation because the center of rotation seems to jump all over the place. How can I rotate an image smoothly?

Thanks,
virtuosity
I think you''ll have to change the top left corner coords every frame. For example, if the new image is 10 pixels wider than the original one then you subtract 5 from the x coord.
Or use Java2D and apply a rotation transform before painting the image...
I am a Jedi, like my father before me

This topic is closed to new replies.

Advertisement