Article View: borland.public.jbuilder.java.swing
Article #8931Re: Rotate Image
From: John P. McGrath
Date: Sat, 01 Jul 2000 00:00
Date: Sat, 01 Jul 2000 00:00
63 lines
2659 bytes
2659 bytes
On Tue, 27 Jun 2000 11:41:54 +0200, JPBallet wrote:
> Sorry but this don't work with image. i hav a Debugraphics
> exception.
Steve's code looks pretty good to me. I am not sure why it would not
work for you. Perhaps there was a problem integrating it with your code.
The following is a test program I wrote a while back, slightly modified.
It is a complete program, so it should work for you. It is very similar
to Steve's code, except I think that the translate() call was a little
off.
This program displays an image scaled to the size of the frame, rotated
90�. Note that the usage of width and height looks a little strange.
That is because after rotation, the width and the height are switched.
I compiled this using JDK 1.3, but I think the only 1.3 dependant code is
the EXIT_ON_CLOSE constant. In fact, it will run on 1.2.2, but requires
1.3 to compile. But you can change "JFrame.EXIT_ON_CLOSE" to "3", and it
should compile with JDK 1.2.2.
Make sure you have a "Test.gif" in the directory when you run it.
import java.net.*;
import java.awt.*;
import javax.swing.*;
public class RotateTest
extends JPanel
{
private Image image;
public static void main( String[] args ) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 300, 300 );
frame.setLocation( 200, 200 );
frame.getContentPane().add( new RotateTest() );
frame.validate();
frame.show();
}
public RotateTest() {
URL url = getClass().getResource( "Test.gif" );
image = Toolkit.getDefaultToolkit().createImage( "Test.gif" );
}
public void paintComponent( Graphics g ) {
super.paintComponent( g );
Graphics2D g2d = (Graphics2D) g;
Dimension size = getSize();
g2d.rotate( Math.PI / 2 );
g2d.translate( 0, - size.width );
g.drawImage( image, 0, 0, size.height, size.width, this );
}
}
--
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID:
<MPG.13c7666614657863989a06@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<8j9gmh$d9q13@bornews.borland.com> <39586810.D1A762EE@cwcom.co.uk> <8j9s77$ifp7@bornews.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,ef6b50d79cf9fa21 X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: Rotate Image Date: 2000/07/01 Message-ID: <MPG.13c7666614657863989a06@forums.borland.com> X-Deja-AN: 641132951 References: <8j9gmh$d9q13@bornews.borland.com> <39586810.D1A762EE@cwcom.co.uk> <8j9s77$ifp7@bornews.borland.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing