Re: Why does AffineTransform not work on JTextPane?
Oliver Wong wrote:
"fiziwig" <fiziwig@yahoo.com> wrote in message
news:1155236644.612852.52050@i42g2000cwa.googlegroups.com...
I'll look into the screen cap approach. If I can figure out how to do a
screen cap, that is. ;-)
See
http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html#displaybmp
- Oliver
Thanks for all your help. After days and days of pulling my hair out,
the rotation finally works perfectly. Here's how I ended up doing it:
class TextPanel extends JTextPane {
// implements rotation for a JTextPane
private int rotation;
private int tx, ty;
private int wide, high;
private BufferedImage renderedText = null;
// valid rotation values are:
// 0 = no rotation
// 1 = rotation 90 degree clockwise
// 2 = rotation 180 degrees
// 3 = rotation 90 degrees counterclockwise
TextPanel() {
super();
rotation = 0;
tx = 0;
ty = 0;
}
public void setDefaultBounds( int x, int y, int width, int height)
{
high = height;
wide = width;
super.setBounds(x,y,width,height);
}
public void setRotation( int newRotation ) {
newRotation = newRotation % 4;
if ( rotation != newRotation ) {
switch (newRotation) {
case 0 : tx = 0; ty = 0; break;
case 1 : tx = 1; ty = 0; break;
case 2 : tx = 1; ty = 1; break;
case 3 : tx = 0; ty = 1; break;
}
if ( newRotation != 0 ) {
if ( renderedText==null) {
rotation = 0; // so that text is actually rendered
renderedText = new BufferedImage(wide, high,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2D = renderedText.createGraphics();
paint( g2D );
}
}
rotation = newRotation; // so the repaint will paint the
rendered image
if ((rotation%2)==0) {
setSize(wide,high);
} else {
setSize(high,wide);
}
repaint();
}
}
public int getRotation() { return rotation; }
public void paintComponent(Graphics g) {
if ( rotation == 0 ) {
super.paintComponent(g);
return;
}
Graphics2D g2 = (Graphics2D) g;
double angle = rotation * Math.PI/2;
AffineTransform tr = g2.getTransform();
if (rotation==2) {
tr.setToTranslation(wide*tx,high*ty);
} else {
tr.setToTranslation(high*tx,wide*ty);
}
tr.rotate(angle);
g2.drawImage(renderedText, tr, this);
}
}
--gary