Engang for længe siden lavede jeg det samme. Jeg havde nok samme lærebog!!!!
Jeg kan huske, jeg brugte mange grimme ord, da jeg skulle have uret til at tilpasse sig framens størrelse.
Jeg gjorde ikke så meget i kommentarer dengang, så du må selv finde ud af, hvad der foregår.
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.Date;
-
- public class ClockModel implements ActionListener
- {
- private int hour, min, sec;
- private boolean militaryTime = true;
- private ClockDigitalViewer digital = new ClockDigitalViewer();
- private ClockAnalogViewer analog = new ClockAnalogViewer();
-
- public ClockModel()
- {
- Timer timer = new Timer(1000, this);
- timer.start();
- setCurrentTime();
- }
-
- public void setMilitaryTime(boolean militaryTime)
- {
- this.militaryTime = militaryTime;
- }
-
- public void setClock(int hour, int min, int sec)
- {
- this.hour = hour;
- this.min = min;
- this.sec = sec;
- }
- public void setCurrentTime()
- {
- Date date = new Date();
- hour = date.getHours();
- min = date.getMinutes();
- sec = date.getSeconds();
- }
- public void actionPerformed(ActionEvent e)
- {
- sec++;
-
- if(sec>59)
- {
- sec=0;
- min++;
- }
- if(min>59)
- {
- min=0;
- hour++;
- }
- if(hour>24)
- {
- hour=0;
- }
- digital.setTime(hour, min, sec, militaryTime);
- analog.setTime(hour, min, sec, militaryTime);
- }
- }
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
-
- public class ClockController extends JFrame implements ActionListener
- {
- public static void main(String args [])
- {
- JFrame frame = new ClockController();
- }
-
- private ClockModel model = new ClockModel();
- private JButton hours, change, current;
- private JLabel label[] = new JLabel[5];
- private JTextField hour, min, sec;
- private boolean militaryTime = true;
-
- public ClockController()
- {
- label[0] = new JLabel("12 or 24 hours", SwingConstants.RIGHT);
- label[1] = new JLabel("Hours", SwingConstants.RIGHT);
- label[2] = new JLabel("Minutes", SwingConstants.RIGHT);
- label[3] = new JLabel("Seconds", SwingConstants.RIGHT);
-
- hours = new JButton("12");
- hours.addActionListener(this);
- change = new JButton("Change time");
- change.addActionListener(this);
- current = new JButton("Set current time");
- current.addActionListener(this);
- hour = new JTextField(3);
- min = new JTextField(3);
- sec = new JTextField(3);
-
- JPanel center = new JPanel(new GridLayout(3,2,5,5));
- center.add(label[1]);
- center.add(hour);
- center.add(label[2]);
- center.add(min);
- center.add(label[3]);
- center.add(sec);
- add(center, BorderLayout.CENTER);
-
- JPanel south = new JPanel(new GridLayout(2,2,5,5));
- south.add(label[0]);
- south.add(hours);
- south.add(change);
- south.add(current);
- add(south, BorderLayout.SOUTH);
-
- setSize(300,160);
- setVisible(true);
- setTitle("Controller");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLocation(600,300);
- }
-
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==hours)
- {
- if(militaryTime)
- {
- hours.setText("24");
- militaryTime=false;
- model.setMilitaryTime(militaryTime);
- }
- else
- {
- hours.setText("12");
- militaryTime=true;
- model.setMilitaryTime(militaryTime);
- }
- }
- else if(e.getSource()==change)
- {
- try
- {
- model.setClock(Integer.parseInt(hour.getText()), Integer.parseInt(min.getText()),
- Integer.parseInt(sec.getText())-1);
- }
- catch(Exception ex){}
- }
- else if(e.getSource()==current)
- {
- model.setCurrentTime();
- }
- }
- }
-
- import javax.swing.*;
- import java.awt.*;
-
- public class ClockDigitalViewer extends JFrame
- {
- private JTextField display;
-
- public ClockDigitalViewer()
- {
- display = new JTextField("00:00:00");
- add(display, BorderLayout.CENTER);
-
- setTitle("Digital viewer");
- setSize(220,120);
-
- display.setFont(new Font("", Font.BOLD, 50));
- display.setEditable(false);
- setLocation(100,500);
- setVisible(true);
- setResizable(false);
- }
-
- public void setTime(int hour, int min, int sec, boolean militaryTime)
- {
- int tempHour = 0;
- if(!militaryTime && hour>12)
- {
- tempHour = hour%12;
- }
- else if(militaryTime && hour==24)
- {
- tempHour = 0;
- }
- else tempHour = hour;
-
- StringBuffer str = new StringBuffer();
- if(tempHour<10)
- {
- str.append(0);
- }
- str.append(tempHour);
- str.append(':');
- if(min<10)
- {
- str.append(0);
- }
- str.append(min);
- str.append(':');
- if(sec<10)
- {
- str.append(0);
- }
- str.append(sec);
-
- display.setText(str.toString());
- }
- }
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.geom.*;
-
- public class ClockAnalogViewer extends JFrame
- {
- private static final int WIDTH = 300;
- private static final int HEIGHT = 330;
-
- private double hour, min, sec;
-
- public ClockAnalogViewer()
- {
- setTitle("Analog viewer");
- setSize(WIDTH,HEIGHT);
-
- setLocation(100,100);
-
- ClockPanel panel = new ClockPanel();
- add(panel,BorderLayout.CENTER);
-
- setVisible(true);
- }
-
- public void setTime(int hour, int min, int sec, boolean militaryTime)
- {
- this.hour = hour+min/60.0;
- this.min = min+sec/60.0;
- this.sec = sec;
- repaint();
- }
-
- class ClockPanel extends JPanel
- {
- private int xKor, yKor;
- private Point lineStart, lineEnd;
- private int width, height;
-
- public void paintComponent(Graphics g)
- {
- Graphics2D g2 = (Graphics2D)g;
-
- width = getWidth();
- height = getHeight();
-
- g2.drawOval(width/2-((width-width/50)/2),height/2-((height-height/50)/2),width-width/50,height-height/50);
- g2.drawOval(width/2-2,height/2-2,4,4);
-
- for (int i=0; i<=360; i+=30)
- {
- xKor = (int)Math.round((width/2+(Math.sin(Math.PI*2*(i/360.0)))*((width-width/50)/2)));
- yKor = (int)Math.round((height/2-(Math.cos(Math.PI*2*(i/360.0)))*((height-height/50)/2)));
- lineStart = new Point(xKor, yKor);
-
- xKor = (int)Math.round((width/2+(Math.sin(Math.PI*2*(i/360.0)))*((width-width/10)/2)));
- yKor = (int)Math.round((height/2-(Math.cos(Math.PI*2*(i/360.0)))*((height-height/10)/2)));
- lineEnd = new Point(xKor, yKor);
-
- g2.draw(new Line2D.Double(lineStart,lineEnd));
- }
- lineStart = new Point(width/2, height/2);
- xKor = (int)Math.round((width/2+(Math.sin(Math.PI*2*(min/60)))*((width-width/8)/2)));
- yKor = (int)Math.round((height/2-(Math.cos(Math.PI*2*(min/60)))*((height-height/8)/2)));
- lineEnd = new Point(xKor, yKor);
- g2.draw(new Line2D.Double(lineStart,lineEnd));
-
- xKor = (int)Math.round((width/2+(Math.sin(Math.PI*2*(hour/12)))*((width-width/2)/2)));
- yKor = (int)Math.round((height/2-(Math.cos(Math.PI*2*(hour/12)))*((height-height/2)/2)));
- lineEnd = new Point(xKor, yKor);
- g2.draw(new Line2D.Double(lineStart,lineEnd));
-
- xKor = (int)Math.round((width/2+(Math.sin(Math.PI*2*(sec/60)))*((width-width/8)/2)));
- yKor = (int)Math.round((height/2-(Math.cos(Math.PI*2*(sec/60)))*((height-height/8)/2)));
- lineEnd = new Point(xKor, yKor);
- g2.setColor(Color.RED);
- g2.draw(new Line2D.Double(lineStart,lineEnd));
- g2.setColor(Color.BLACK);
- }
- }
- }