第5周作业-时钟动画程序

来源:互联网 发布:电大简述时间管理矩阵 编辑:程序博客网 时间:2024/05/16 00:56

《Java语言程序设计》P418 不动的时钟

             不动的时钟由三个类组成StillClock.java , Display.javaMessagePanel.java。

StillClock.java

import java.awt.*;import javax.swing.*;import java.util.*;public class StillClock extends JPanel {private int hour;private int minute;private int second;    public StillClock(){    setCurrentTime();    }    public StillClock(int hour,int minute,int second){    this.hour=hour;    this.minute=minute;    this.second=second;    }        public int getHour(){    return hour;    }    public int gethour(){    return hour;    }        public void setHour(int hour){    this.hour=hour;    repaint();    }        public int getMinute(){    return minute;    }        public void setMinute(int minute){    this.minute=minute;    repaint();    }        public int getSecond(){    return second;    }        public void setSecond(int second){    this.second=second;    repaint();    }        protected void paintComponent(Graphics g){    super.paintComponent(g);        int clockRadius=    (int)(Math.min(getWidth(), getHeight())*0.8*0.5);    int xCenter=getWidth()/2;    int yCenter=getHeight()/2;        g.setColor(Color.BLACK);    g.drawOval(xCenter-clockRadius,yCenter-clockRadius,    2*clockRadius,2*clockRadius);    g.drawString("12", xCenter-5, yCenter-clockRadius+12);    g.drawString("9", xCenter-clockRadius+3,yCenter+5 );    g.drawString("3", xCenter+clockRadius-10, yCenter+3);    g.drawString("6", xCenter-3, yCenter+clockRadius-3);        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);        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(second*(2*Math.PI/60)));    g.drawLine(xCenter,yCenter,xMinute,yMinute);        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.sin((hour%12+minute/60.0)*(2*Math.PI/12)));    g.setColor(Color.green);    g.drawLine(xCenter, yCenter, xHour, yHour);        }    public void setCurrentTime(){    Calendar calendar=new GregorianCalendar();        this.hour=calendar.get(Calendar.HOUR_OF_DAY);    this.minute=calendar.get(Calendar.MINUTE);    this.second=calendar.get(Calendar.SECOND);    }    public Dimension getPreferredSize(){    return new Dimension(200,200);    }}

本类是为时钟参数


Display.java


import java.awt.*;import javax.swing.*;public class DisplayClock extends JFrame{public DisplayClock(){ StillClock clock=new StillClock(); MessagePanel messagePanel=new MessagePanel(clock.getHour()+":" + clock.getMinute() +  ":" + clock.getSecond()  ); messagePanel. setCentered(true); messagePanel. setForeground(Color.blue); messagePanel. setFont(new Font("Courier",Font.BOLD,16));  add(clock); add(messagePanel,BorderLayout.SOUTH);}public static void main(String[]args){DisplayClock frame=new DisplayClock();frame.setTitle("DisplayClock"); frame.setSize(300,350);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}

MessagePanel.java

import java.awt.*;import javax.swing.*;import java.util.*;public class StillClock extends JPanel {private int hour;private int minute;private int second;    public StillClock(){    setCurrentTime();    }    public StillClock(int hour,int minute,int second){    this.hour=hour;    this.minute=minute;    this.second=second;    }        public int getHour(){    return hour;    }    public int gethour(){    return hour;    }        public void setHour(int hour){    this.hour=hour;    repaint();    }        public int getMinute(){    return minute;    }        public void setMinute(int minute){    this.minute=minute;    repaint();    }        public int getSecond(){    return second;    }        public void setSecond(int second){    this.second=second;    repaint();    }        protected void paintComponent(Graphics g){    super.paintComponent(g);        int clockRadius=    (int)(Math.min(getWidth(), getHeight())*0.8*0.5);    int xCenter=getWidth()/2;    int yCenter=getHeight()/2;        g.setColor(Color.BLACK);    g.drawOval(xCenter-clockRadius,yCenter-clockRadius,    2*clockRadius,2*clockRadius);    g.drawString("12", xCenter-5, yCenter-clockRadius+12);    g.drawString("9", xCenter-clockRadius+3,yCenter+5 );    g.drawString("3", xCenter+clockRadius-10, yCenter+3);    g.drawString("6", xCenter-3, yCenter+clockRadius-3);        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);        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(second*(2*Math.PI/60)));    g.drawLine(xCenter,yCenter,xMinute,yMinute);        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.sin((hour%12+minute/60.0)*(2*Math.PI/12)));    g.setColor(Color.green);    g.drawLine(xCenter, yCenter, xHour, yHour);        }    public void setCurrentTime(){    Calendar calendar=new GregorianCalendar();        this.hour=calendar.get(Calendar.HOUR_OF_DAY);    this.minute=calendar.get(Calendar.MINUTE);    this.second=calendar.get(Calendar.SECOND);    }    public Dimension getPreferredSize(){    return new Dimension(200,200);    }}

运行结果:

正如类名,只是会记录运行的那瞬间的时间,不会运动


0 0
原创粉丝点击