| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3137 人关注过本帖
标题:java模拟时钟(一个庞大的问题)
只看楼主 加入收藏
lousang
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2016-12-27
结帖率:80%
收藏
已结贴  问题点数:18 回复次数:8 
java模拟时钟(一个庞大的问题)
程序代码:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Clock extends JComponent implements Runnable{
    Thread thread;
    
    Calendar currentTime;//当前时间
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    
    public Clock() {
        setBounds(0,0,300,400);
        setBackground(Color.PINK);
        
        
        thread = new Thread();
        thread.start();
    }
    
    
    public void PaintClock(Graphics g) {
        g.setColor(Color.black);//设置黑色来画时钟
        drawHourAndMinutePointer(2*Math.PI*hours/12,30,g);//画时针
        drawHourAndMinutePointer(2*Math.PI*minutes/60,40,g);//画分针
        drawSecondPointer(2*Math.PI*seconds/12,50,g);//画秒针
        drawNumber(g);
        g.drawOval(0, 50, 150, 150);//画左边圆
    }
    
    void drawSecondPointer(double angle, int radius, Graphics g) {
        angle -= 0.5*Math.PI;//减去90度,因坐标与时钟差90度
        int x = (int)(radius*Math.cos(angle));//计算针头的X坐标
        int y = (int)(radius*Math.sin(angle));//计算针头的Y坐标
        g.drawLine(75, 200, 75+x, 200+y);//画秒针
    }

    void drawHourAndMinutePointer(double angle, int radius, Graphics g) {
        angle -= 0.5*Math.PI;//减去90度,因坐标与时钟差90度
        int x = (int)(radius*Math.cos(angle));//计算针头的X坐标
        int y = (int)(radius*Math.sin(angle));//计算针头的Y坐标
        angle += 2*Math.PI/3;//将angle调整为第一针尾的角度
        int x2 = (int)(5*Math.cos(angle));//计算第一针尾点的X坐标
        int y2 = (int)(5*Math.sin(angle));//计算第一针尾点的Y坐标
        angle += 2*Math.PI/3;//将angle调整为第二针尾的角度
        int x3 = (int)(5*Math.cos(angle));//计算第二针尾点的X坐标
        int y3 = (int)(5*Math.sin(angle));//计算第二针尾点的Y坐标
        g.drawLine(75+x2, 200+y2, 75+x, 200+y);//画左翼
        g.drawLine(75+x3, 200+y3, 75+x, 200+y);//画右翼
        g.drawLine(75+x2, 200+y2, 75+x3, 200+y3);//画针尾
    }

    void drawNumber(Graphics g) {
        for(int i = 1; i <= 12; i++) {
            drawnumber(g,(float) (Math.PI / 6)*i,i);
        }
    }
    void drawnumber(Graphics g,double angle,int num) {
            float sine = (float) Math.sin(angle);
            float cosine = (float) Math.cos(angle);

            int x = (int) (40 * sine);
            int y = (int) (40 * cosine);
            g.drawString("" + num, x + 75, 200 - y);
    }
    
    public void run() {
        currentTime = Calendar.getInstance();
        hours = currentTime.get(Calendar.HOUR);
        minutes = currentTime.get(Calendar.MINUTE);
        seconds = currentTime.get(Calendar.SECOND);
        repaint();
        }
}


搜索更多相关主题的帖子: java import int Math 坐标 
2018-06-08 22:17
lousang
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2016-12-27
收藏
得分:0 
程序代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JComponent;
import javax.swing.JOptionPane;

public class Clock1 extends JComponent implements Runnable,ActionListener{
    Thread thread1;
    
    Calendar currentTime;//当前时间
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    int year = 0;
    int month = 0;
    int date = 0;
    int week = 0;
    String timeString = "";//用于以数字方式显示时间的字符串
    String dateString = "";//用于以数字方式显示日期的字符串
    
    public Clock1(){
        setLayout(null);
        setBackground(Color.BLUE);
        thread1 = new Thread();
        thread1.start();
    }
    
    public void run() {
        currentTime = Calendar.getInstance();//返回当前系统时间
        hours = currentTime.get(Calendar.HOUR_OF_DAY);
        minutes = currentTime.get(Calendar.MINUTE);
        seconds = currentTime.get(Calendar.SECOND);
        year = currentTime.get(Calendar.YEAR);
        month = currentTime.get(Calendar.MONTH);
        date = currentTime.get(Calendar.DATE);
        week = currentTime.get(Calendar.DAY_OF_WEEK);
        timeString = ((hours > 9)?"":"0")+hours+":"//构造表示当前时间的字符串
                    +((minutes>9)?"":"0")+minutes+":"
                    +((seconds>9)?"":"0")+seconds+"\n";
        dateString = year+"/"+month+"/"+date+"/"+"星期"+week;//构造表示当前日期的字符串
        repaint();//重绘屏幕
        try {
            Thread.sleep(1000);//等待一分钟
        }
        catch(InterruptedException e) {
        }//不对异常处理
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setHour();
        setMinute();
        setSecond();
        setYear();
        setMonth();
        setDate();
        setWeek();
    }

    private void setHour() {
        String str = JOptionPane.showInputDialog(null,"请输入当前小时(24):");
        hours = Integer.parseInt(str);
    }
    
    private void setMinute() {
        String str = JOptionPane.showInputDialog(null,"请输入当前分钟:");
        minutes = Integer.parseInt(str);
    }
    
    private void setSecond() {
        String str = JOptionPane.showInputDialog(null,"请输入当前秒钟:");
        seconds = Integer.parseInt(str);
    }
    
    private void setYear() {
        String str = JOptionPane.showInputDialog(null,"请输入年份:");
        year = Integer.parseInt(str);
    }
    
    private void setMonth() {
        String str = JOptionPane.showInputDialog(null,"请输入月份:");
        month = Integer.parseInt(str);
    }
    
    private void setDate() {
        String str = JOptionPane.showInputDialog(null,"请输入日期:");
        date = Integer.parseInt(str);
    }
    
    private void setWeek() {
        String str = JOptionPane.showInputDialog(null,"请输入星期:");
        week = Integer.parseInt(str);
    }
    
    public String toString() {
        Font bigBoldFont = new Font("SansSerif",Font.BOLD,20);//创建字体尺寸
        setFont(bigBoldFont);//设置字体尺寸
        return timeString+dateString;
    }
    
}
2018-06-08 22:17
lousang
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2016-12-27
收藏
得分:0 
程序代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Clock2 extends JPanel implements Runnable,ActionListener {
    JButton start,reset,stop;
    JPanel panel2;
    JLabel label2;
    Thread thread2;
    
    int hours = 0;
    int minutes = 0;
    double seconds = 0;
    
    public Clock2() {
        setBackground(Color.BLUE);
        setPreferredSize(new Dimension(300, 300));
        setLayout(null);
        
        start = new JButton("start");
        start.setFont(new Font("SansSerif",Font.BOLD,20));
        start.addActionListener(this);
        reset = new JButton("reset");
        reset.setFont(new Font("SansSerif",Font.BOLD,20));
        reset.addActionListener(this);
        stop = new JButton("stop");
        stop.setFont(new Font("SansSerif",Font.BOLD,20));
        stop.addActionListener(this);
        
        label2 = new JLabel("00:00:00:00");
        label2.setBounds(225,250,150,100);
        label2.setFont(new Font("SansSerif",Font.BOLD,20));
        add(label2);
        
        panel2 = new JPanel();
        panel2.setBackground(Color.gray);
        panel2.setBounds(0, 150, 300, 50);
        add(panel2);
        panel2.add(start);
        panel2.add(reset);
        panel2.add(stop);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getActionCommand().equals("start")){
            Start();
        }
        else if(e.getActionCommand().equals("stop")){
            Stop();
        }
        else{
            reset();
        }
    }

    private void Stop() {
        // TODO Auto-generated method stub
        thread2.stop();
    }

    private void reset() {
        // TODO Auto-generated method stub
        this.minutes = 0;
        this.seconds = 0;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            try{
                seconds+=0.0018d;
                Thread.sleep(1);
                if(seconds>59d){
                    seconds=0d;
                    minutes=minutes+1;
                    System.out.print(minutes);
                }
                add(label2);
            }
            catch(Exception e){}
        }
    }

    private void Start() {
        // TODO Auto-generated method stub
        thread2 = new Thread();
        thread2.start();
    }
}
2018-06-08 22:18
lousang
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2016-12-27
收藏
得分:0 
程序代码:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;


public class RunClock extends JFrame {
    public static final int WIDTH = 600;//主框架宽度
    public static final int HEIGHT = 400;//主框架高度
    JFrame mainFrame;
    
    JMenu editMenu;//菜单栏
    JMenuItem editItem1;//小时
    JMenuItem editItem2;//分钟
    JMenuItem editItem3;//秒钟
    JMenuItem editItem4;//年份
    JMenuItem editItem5;//月份
    JMenuItem editItem6;//日期
    JMenuItem editItem7;//日期
    
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    int year = 0;
    int month = 0;
    int date = 0;
    int week = 0;
    
    public void showPaint() {//显示窗口
        
        mainFrame = new JFrame();
        mainFrame.setTitle("时钟");//设置窗口标题
        mainFrame.setSize(WIDTH, HEIGHT);//设置窗口的高度和宽度
        mainFrame.setBackground(Color.GRAY);//设置框架背景为灰色
        mainFrame.setResizable(false);//设置程序框架大小不可改变
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置用户关闭框架时的响应动作
            
        JMenuBar menuBar = new JMenuBar();//菜单栏
        mainFrame.setJMenuBar(menuBar);
        editMenu = new JMenu("设置");
        editItem1 = new JMenuItem("小时(H)",'H');
        editItem2 = new JMenuItem("分钟(M)",'M');
        editItem3 = new JMenuItem("秒钟(S)",'S');
        editItem4 = new JMenuItem("年份(Y)",'y');
        editItem5 = new JMenuItem("月份(M)",'M');
        editItem6 = new JMenuItem("日期(D)",'D');
        editItem7 = new JMenuItem("星期(W)",'W');
        menuBar.add(editMenu);
        editMenu.add(editItem1);
        editMenu.add(editItem2);
        editMenu.add(editItem3);
        editMenu.add(editItem4);
        editMenu.add(editItem5);
        editMenu.add(editItem6);
        editMenu.add(editItem7);
        
        editItem1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setHour();
            }

            private void setHour() {
                String str = JOptionPane.showInputDialog(null,"请输入当前小时(24小时制):");
                hours = Integer.parseInt(str);
            }
        });
        
        editItem2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setMinute();
            }

            private void setMinute() {
                String str = JOptionPane.showInputDialog(null,"请输入当前分钟:");
                minutes = Integer.parseInt(str);
            }
        });
        
        editItem3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setSecond();
            }

            private void setSecond() {
                String str = JOptionPane.showInputDialog(null,"请输入当前秒钟:");
                seconds = Integer.parseInt(str);
            }
        });
        
        editItem4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setYear();
            }

            private void setYear() {
                String str = JOptionPane.showInputDialog(null,"请输入年份:");
                year = Integer.parseInt(str);
            }
        });
        
        editItem5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setMonth();
            }

            private void setMonth() {
                String str = JOptionPane.showInputDialog(null,"请输入月份:");
                month = Integer.parseInt(str);
            }
        });
        
        editItem6.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setDate();
            }

            private void setDate() {
                String str = JOptionPane.showInputDialog(null,"请输入日期:");
                date = Integer.parseInt(str);
            }
        });
        
        editItem7.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setWeek();
            }

            private void setWeek() {
                String str = JOptionPane.showInputDialog(null,"请输入星期:");
                week = Integer.parseInt(str);
            }
        });
        
        Container container = mainFrame.getContentPane();
        container.setLayout(null);
        Clock clock = new Clock();//模拟时钟
        Clock1 clock1 = new Clock1();//数字时钟
        Clock2 clock2 = new Clock2();//秒表
        container.add(clock);
        clock.setBounds(0, 0, 300, 400);
        container.add(clock1);
        clock1.setBounds(225,250,150,100);
        container.add(clock2);
        clock2.setBounds(225, 50, 150, 100);
        
        mainFrame.setVisible(true);//显示主框架
    }
        
    public static void main(String args[]) {
        RunClock testMyClock = new RunClock();
        testMyClock.showPaint();
    }

}
2018-06-08 22:18
lousang
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2016-12-27
收藏
得分:0 
为什么我不能显示时钟呢
2018-06-08 22:19
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:6 
所有的都不显示吗

假如人生没有梦想,和咸鱼有什么区别!
2018-06-09 23:21
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:6 
程序代码:
public class Clock extends JComponent {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public Clock() {
        setBounds(0,0,300,400);
        setBackground(Color.PINK);
    }
    
    public void paint(Graphics g) {
        LocalTime time = LocalTime.now();
        g.setColor(Color.black);//设置黑色来画时钟
        drawHourAndMinutePointer(2*Math.PI*time.getHour()/12,30,g);//画时针
        drawHourAndMinutePointer(2*Math.PI*time.getMinute()/60,40,g);//画分针
        drawSecondPointer(2*Math.PI*time.getSecond()/12,50,g);//画秒针
        drawNumber(g);
        g.drawOval(0, 125, 150, 150);//画左边圆
    }
    
    void drawSecondPointer(double angle, int radius, Graphics g) {
        angle -= 0.5*Math.PI;//减去90度,因坐标与时钟差90度
        int x = (int)(radius*Math.cos(angle));//计算针头的X坐标
        int y = (int)(radius*Math.sin(angle));//计算针头的Y坐标
        g.drawLine(75, 200, 75+x, 200+y);//画秒针
    }

    void drawHourAndMinutePointer(double angle, int radius, Graphics g) {
        angle -= 0.5*Math.PI;//减去90度,因坐标与时钟差90度
        int x = (int)(radius*Math.cos(angle));//计算针头的X坐标
        int y = (int)(radius*Math.sin(angle));//计算针头的Y坐标
        angle += 2*Math.PI/3;//将angle调整为第一针尾的角度
        int x2 = (int)(5*Math.cos(angle));//计算第一针尾点的X坐标
        int y2 = (int)(5*Math.sin(angle));//计算第一针尾点的Y坐标
        angle += 2*Math.PI/3;//将angle调整为第二针尾的角度
        int x3 = (int)(5*Math.cos(angle));//计算第二针尾点的X坐标
        int y3 = (int)(5*Math.sin(angle));//计算第二针尾点的Y坐标
        g.drawLine(75+x2, 200+y2, 75+x, 200+y);//画左翼
        g.drawLine(75+x3, 200+y3, 75+x, 200+y);//画右翼
        g.drawLine(75+x2, 200+y2, 75+x3, 200+y3);//画针尾
    }

    void drawNumber(Graphics g) {
        for(int i = 1; i <= 12; i++) {
            drawnumber(g,(float) (Math.PI / 6)*i,i);
        }
    }
    void drawnumber(Graphics g,double angle,int num) {
        float sine = (float) Math.sin(angle);
        float cosine = (float) Math.cos(angle);
        
        int x = (int) (40 * sine);
        int y = (int) (40 * cosine);
        g.drawString("" + num, x + 75, 200 - y);
    }
}

剑栈风樯各苦辛,别时冰雪到时春
2018-06-10 00:03
举火烽火燎天
Rank: 1
等 级:新手上路
帖 子:2
专家分:6
注 册:2018-6-12
收藏
得分:6 
学到了,感谢楼主。。
2018-06-12 18:57
快速回复:java模拟时钟(一个庞大的问题)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.081674 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved