Three odd problems with one piece of code
I'm rotating a JTextPane and three odd things happen. First of all, the text pane flashes briefly and then disappears, or partially disappears. Second, it's location on the screen is strange as it rotates, and third, the clipping is odd.
What is strange is that this same general code works fine to rotate a JLabel with an image in it.
Any clues to what I'm doing srong?
Thanks,
--gary
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
publicclass Rotateextends JPanel{
private TextPanel textPane;
private JPanel parent;
public Rotate(){
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JToolBar toolBar = buildToolbar();
add(toolBar);
parent =new JPanel();
add(parent);
parent.setBackground( Color.white);
parent.setPreferredSize(new Dimension(640, 480));
// Create a text pane.
textPane =new TextPanel();
StyledDocument doc = textPane.getStyledDocument();
try{
doc.insertString(doc.getLength(),"This is some sample text.\nIt can be Rotated.",null);
}
catch (BadLocationException ble){
System.err.println("Couldn't insert initial text into text pane.");
}
textPane.setBounds(0, 400, 240, 120);
Border myBorder = BorderFactory.createLineBorder( Color.red );
textPane.setBorder(myBorder);
parent.add(textPane);
}
private JToolBar buildToolbar(){
JToolBar toolBar =new JToolBar();
toolBar.setRollover(true );
toolBar.setFloatable(false );
JButton rotateButton =new JButton("Rotate");
rotateButton.setToolTipText("Rotate text editing pane" );
rotateButton.addActionListener(new ActionListener(){
publicvoid actionPerformed( ActionEvent e ){
textPane.setRotation(textPane.getRotation()+1);
}
});
toolBar.add( rotateButton );
return toolBar;
}
privatestaticvoid createAndShowGUI(){
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame =new JFrame("Rotate");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane =new Rotate();
newContentPane.setOpaque(true);//content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
}
class TextPanelextends JTextPane{
// implements rotation for a JTextPane
privateint rotation;
privateint tx, ty;
// vaild rotation values are:
// 0 = no rotation
// 1 = rotation 90 degree clockwise
// 2 = rotation 180 degrees
// 3 = rotation 90 degrees counterclockwise
TextPanel(){
rotation = 0;
tx = 0;
ty = 0;
}
publicvoid setRotation(int newRotation ){
newRotation = newRotation % 4;
int change = Math.abs(rotation-newRotation);
if ((change&0x01)==1){
setSize(getHeight(),getWidth());
}
rotation = newRotation;
switch (rotation){
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;
}
repaint();
}
publicint getRotation(){return rotation;}
publicvoid paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
double angle = rotation * Math.PI/2;
AffineTransform tr = g2.getTransform();
int w = getWidth()-20;
int h = getHeight()-20;
tr.setToTranslation(w*tx,h*ty);
tr.rotate(angle);
g2.setTransform(tr);
super.paintComponent(g);
}
}

