I tried many ways to add radio buttons in it but they failed.
I also used the radio buttons code in tutorial and the program compiled. The problem is no radio buttons were shown in the clock.
here is the code of world clock:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.*;
public class WorldClock extends JPanel {
private int hour;
private int minute;
private int second;
//private ButtonHandler handler = new ButtonHandler();
private GradientPaint gradient =
new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow, true);
private JPanel panel;
private JButton EST;
public TimeZone timeZone = TimeZone.getTimeZone("America/Chicago");
public Locale locale = new Locale("US"); //Locale.GREECE;
public Calendar calendar;
/** Construct a default clock with the current time*/
public WorldClock() {
setCurrentTime();
}
/** Construct a clock with specified hour, minute, and second */
public WorldClock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone;
}
/** Return hour */
public int getHour() {
return hour;
}
/** Set a new hour */
public void setHour(int hour) {
this.hour = hour;
repaint();
}
/** Return minute */
public int getMinute() {
return minute;
}
/** Set a new minute */
public void setMinute(int minute) {
this.minute = minute;
repaint();
}
/** Return second */
public int getSecond() {
return second;
}
/** Set a new second */
public void setSecond(int second) {
this.second = second;
repaint();
}
/** Draw the clock */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
//drawGradientCircle(g2d);
// Initialize clock parameters
int clockRadius = (int) (getWidth() / 2 * 0.8 );
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
// Create an action for each radio button
Action action1 = new AbstractAction("RadioButton Label1") {
// This method is called whenever the radio button is pressed,
// even if it is already selected; this method is not called
// if the radio button was selected programmatically
public void actionPerformed(ActionEvent evt) {
// Perform action
}
};
Action action2 = new AbstractAction("RadioButton Label2") {
// See above
public void actionPerformed(ActionEvent evt) {
// Perform action
}
};
// Create the radio buttons using the actions
JRadioButton b1 = new JRadioButton(action1);
JRadioButton b2 = new JRadioButton(action2);
// Associate the two buttons with a button group
ButtonGroup group = new ButtonGroup();
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
group.add(b1);
group.add(b2);
//JPanel radioPanel = new JPanel();
panel = new JPanel( new FlowLayout());
EST = new JButton("EST");
//EST.addActionListener(handler);
//radioPanel.add(EST);
//add(radioPanel, BorderLayout.NORTH);
add(panel,BorderLayout.SOUTH);
// Draw circle
g2d.setStroke(new BasicStroke(4.0f));
g2d.setPaint(gradient);
g2d.fillOval(xCenter - clockRadius, yCenter - clockRadius,
2 * clockRadius, 2 * clockRadius);
g.setColor(Color.yellow);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
2 * clockRadius, 2 * clockRadius);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("12", xCenter - 11, yCenter - clockRadius + 23);
g.drawString("9", xCenter - clockRadius + 3, yCenter + 8);
g.drawString("3", xCenter + clockRadius - 17, yCenter + 8);
g.drawString("6", xCenter - 7, yCenter + clockRadius - 3);
// Draw second hand
int sLength = (int)(clockRadius * 0.8);
int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);
// Draw minute hand
int mLength = (int)(clockRadius * 0.65);
int xMinute = (int)(xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60)));
int yMinute = (int)(yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);
// Draw hour hand
int hLength = (int)(clockRadius * 0.5);
int xHour = (int)(xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.green);
g.drawLine(xCenter, yCenter, xHour, yHour);
}
public void setCurrentTime() {
// Construct a calendar for the current date and time
calendar = new GregorianCalendar(timeZone, locale);
// Set current hour, minute and second
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
}
}
here is the code for making it digital:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class WorldDigital extends WorldClock {
public WorldDigital() {
setTimeZone(timeZone);
setCurrentTime();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.magenta);
g.setFont(new Font("Courier",Font.BOLD,20));
// Display digit time on the label
DateFormat formatter = DateFormat.getDateTimeInstance
(DateFormat.SHORT, DateFormat.LONG, locale);
formatter.setTimeZone(timeZone);
String text = formatter.format(calendar.getTime());
g.drawString(text,60,400);
}
}
here is the clock animation code:
import java.awt.event.*;
import javax.swing.*;
public class WorldDigitalAnimation extends WorldDigital {
public WorldDigitalAnimation() {
// Create a timer with delay 1000 ms
Timer timer = new Timer(1000, new TimerListener());
timer.start();
}
private class TimerListener implements ActionListener {
/** Handle the action event */
public void actionPerformed(ActionEvent e) {
// Set new time and repaint the clock to display current time
setCurrentTime();
repaint();
}
}
/** Main method */
public static void main(String[] args) {
JFrame frame = new JFrame("ClockAnimation");
WorldDigitalAnimation clock = new WorldDigitalAnimation();
frame.add(clock);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 450);
frame.setVisible(true);
}
}
help would be greatly appreciated.

New Topic/Question
Reply




MultiQuote




|