This is my current code:
package Calendar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Clock extends JLabel {
private String pattern;
private Timer timer;
public Clock(String pattern){
this.pattern = pattern;
createTimer();
timer.start();
}
public Clock(){
pattern = "hh:mm:ss a";
createTimer();
timer.start();
}
private void createTimer(){
timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setText(new SimpleDateFormat(pattern).format(new Date()));
}
});
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
frame.setContentPane(contentPane);
Clock digitalClock = new Clock();
contentPane.add(digitalClock);
frame.setVisible(true);
}
}
and I cannot for the life of me figure out how to get this rotated. I looked on several other forums but there was no body else with this issue.
As far as I can tell there is no way to rotate either a JPanel or JFrame so am I out of luck?
This is what it looks like right now:
And I would like to get it so the text is rotated 90 degrees. basically vertical.
EDIT: SOLUTION:
package Calendar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.Timer;
import java.awt.*;
import java.awt.geom.*;
public class Clock extends JLabel {
private String pattern;
private Timer timer;
public Clock ()
{
super();
pattern = "hh:mm:ss a";
createTimer();
timer.start();
}
public Clock(String pattern)
{
super (pattern);
this.pattern = pattern;
createTimer();
timer.start();
}
private void createTimer(){
timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setText(new SimpleDateFormat(pattern).format(new Date()));
}
});
}
public void paint (Graphics g)
{
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g;
AffineTransform flipTrans = new AffineTransform();
double widthD = (double) getWidth();
flipTrans.setToRotation(-Math.PI / 2.0, getWidth() / 2.0, getHeight() / 2.0);
g2.setTransform(flipTrans);
super.paint(g);
} else {
super.paint(g);
}
}
}