This is based on this example and makes use of JXLayer to physically rotate the label component the way you want it.
It also saves you from having to do any custom painting of your own.
I've used a similar technique to rotate JTabbedPane
tabs so they run vertically down the side of the JTabbedPane
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jdesktop.jxlayer.JXLayer;
import org.pbjar.jxlayer.demo.TransformUtils;
import org.pbjar.jxlayer.plaf.ext.transform.DefaultTransformModel;
public class RotateClock {
public static void main(String[] args) {
new RotateClock();
}
public RotateClock() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ExamplePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ExamplePane extends JPanel {
private JSlider slider;
private Clock clock;
private DefaultTransformModel transformModel;
public ExamplePane() {
setLayout(new BorderLayout());
slider = new JSlider(0, 360);
slider.setValue(0);
slider.setSnapToTicks(true);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(10);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
transformModel.setRotation(Math.toRadians(slider.getValue()));
}
});
clock = new Clock();
transformModel = new DefaultTransformModel();
transformModel.setRotation(Math.toRadians(0));
transformModel.setScaleToPreferredSize(true);
JXLayer<JComponent> rotatePane = TransformUtils.createTransformJXLayer(clock, transformModel);
add(slider, BorderLayout.NORTH);
add(rotatePane);
}
}
public class Clock extends JLabel {
private String pattern;
private Timer timer;
public Clock(String pattern) {
this.pattern = pattern;
setText(new SimpleDateFormat(pattern).format(new Date()));
createTimer();
timer.start();
}
public Clock() {
this("hh:mm:ss a");
}
private void createTimer() {
timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setText(new SimpleDateFormat(pattern).format(new Date()));
}
});
timer.setInitialDelay(0);
}
}
}
This deals with, amongst other things, oddities with mouse events and the preferred size of the component.
Unfortunately, the code required to perform the transformation (Piet's examples) is no longer available on the net
I've put all the source code of JXLayer (version 3) and Piet's examples into a single zip and I would suggest, if you are interested, you grab a copy and store it some where safe.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…