• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] image rotation

This topic is 6404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement