world clock *trouble with radiobuttons

trouble adding radionbuttons in the clock

Page 1 of 1

6 Replies - 2819 Views - Last Post: 07 December 2007 - 02:20 AM Rate Topic: -----

#1 r10lover10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-November 07

world clock *trouble with radiobuttons

Post icon  Posted 04 December 2007 - 10:33 PM

Hi I am suppose to add a few radio buttons in the clock and make them options of different locale in the world. When the radio button is clicked, the clock will change to the time of that location.
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.

Is This A Good Question/Topic? 0
  • +

Replies To: world clock *trouble with radiobuttons

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: world clock *trouble with radiobuttons

Posted 05 December 2007 - 06:23 PM

you have the panel commented out:
//add(radioPanel, BorderLayout.NORTH);
the panel cannot be seen if the panel is not added.
Was This Post Helpful? 0
  • +
  • -

#3 r10lover10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-November 07

Re: world clock *trouble with radiobuttons

Posted 05 December 2007 - 09:43 PM

I removed the comment and it still didn't show any radio button.
My appology for the messy comment. I tried many methods on adding the radio button but they all failed. It is my habbit to comment out those possible working code.

thanks for checking it out.
Is there any other way of adding radio button that will work for the clock?
Was This Post Helpful? 0
  • +
  • -

#4 revathi17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-December 07

Re: world clock *trouble with radiobuttons

Posted 06 December 2007 - 02:51 AM

You have to add the radio buttons to the panel in WorldClock.java. you have added the buttons only to the group, you need to add them to the panel too like this:

add(b1);
add(b2);


-R
Was This Post Helpful? 0
  • +
  • -

#5 r10lover10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-November 07

Re: world clock *trouble with radiobuttons

Posted 06 December 2007 - 07:52 AM

	JPanel radioPanel = new JPanel(new GridLayout(0, 1));
	group.add(b1);
	group.add(b2);


Here is the part where they were added in the group. Can you show me the excact code of adding them in the panel? I am not sure which panel to add and how to do so.
Was This Post Helpful? 0
  • +
  • -

#6 revathi17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-December 07

Re: world clock *trouble with radiobuttons

Posted 06 December 2007 - 11:52 PM

	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(b1,BorderLayout.CENTER);
		  add(b2, BorderLayout.CENTER);
		  add(panel,BorderLayout.SOUTH);



you have to add the buttons separately to the panel even if you added it to the group. something like the above. I am not sure why you have an empty 'panel'. If you want you can add the buttons to that panel and leave these 2 lines
add(b1,BorderLayout.CENTER);
add(b2, BorderLayout.CENTER);

Hope this helps..
Was This Post Helpful? 0
  • +
  • -

#7 r10lover10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-November 07

Re: world clock *trouble with radiobuttons

Posted 07 December 2007 - 02:20 AM

thanks revathi17.
the code was able to generates some panels, but they never appear until i maximize my clock. When i restored the default size of my clock, those panels will flood the analog clock and covered it.
I mess around with the code but still couldn't get the output i want.

My idea is to add like 4 radiobuttons on top of the clock and be able to change timezone while clicking on the specific radionbutton.

help would be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1