精华内容
下载资源
问答
  • java钟表实例

    2019-07-09 17:21:05
    java钟表实例对于java中钟表的开发,许多初学者很感兴趣,先分析如下:应用技术:java多线程,java图像加载,java双缓冲技术多媒体:用于指针走动,字幕显示。双缓冲:用于消除图像的闪烁。参考代码:package pic...
    java钟表实例
    对于java中钟表的开发,许多初学者很感兴趣,先分析如下:
    应用技术:java多线程,java图像加载,java双缓冲技术
    多媒体:用于指针走动,字幕显示。
    双缓冲:用于消除图像的闪烁。
    参考代码:
    package picture;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.*;
    import java.util.Date;
    import java.io.*;
    import java.awt.geom.*;
    import java.awt.Color;
    import java.awt.image.*;

    public class Clock extends Applet implements MouseListener, Runnable,
    ActionListener {
    Image image, image1;
    Toolkit tool;
    JPanel panel;
    Graphics gg;
    int width, height, width1, height1;
    Thread thread, thread2;
    MediaTracker m;
    double angel1 = 0, angel2 = 0;
    int xsec, ysec, xsec2, ysec2;
    int c = 0;
    int xmin, ymin, xmin2, ymin2;
    int xhou, yhou, xhou2, yhou2;
    int xstr = 235, ystr = 253;
    int y = ystr;
    JButton button1 = new JButton();
    JButton button2 = new JButton();
    JButton button3 = new JButton();
    JButton button4 = new JButton();
    AudioClip music;
    Color colorh = Color.GREEN, colorm = Color.BLACK, colors = Color.YELLOW;
    String[] string = new String[5];
    int kk = 0;
    public void init() {
    this.setLayout(new BorderLayout());
    this.setBackground(Color.BLACK);
    tool = getToolkit();
    image = tool.getImage("image.PNG");
    m = new MediaTracker(this);
    m.addImage(image, 0);
    try {
    m.waitForID(0);
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    width1 = image.getWidth(this);
    height1 = image.getHeight(this);
    width = this.WIDTH;
    height = this.HEIGHT;
    // music =Applet.newAudioClip(new URL("D:\JAVA\Picture\钟声.wav")) ;
    // music.loop();
    this.addMouseListener(this);
    button1.setText("时针颜色");
    button1.addActionListener(this);
    button2.setText("分针颜色");
    button2.addActionListener(this);
    button3.setText("秒针颜色");
    button3.addActionListener(this);
    button4.setText("选择皮肤");
    button4.addActionListener(this);
    button1.setBackground(Color.BLACK);
    button1.setForeground(Color.WHITE);
    button2.setBackground(Color.BLACK);
    button2.setForeground(Color.WHITE);
    button3.setBackground(Color.BLACK);
    button3.setForeground(Color.WHITE);
    button4.setBackground(Color.BLACK);
    button4.setForeground(Color.WHITE);
    JPanel panel = new JPanel();
    panel.setBackground(Color.BLACK);
    panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    this.setLayout(new BorderLayout());
    this.add(panel, BorderLayout.SOUTH);
    string[0] = "时间就像海绵里水";
    string[1] = "只要挤总会有的";
    string[2] = "珍惜身边每一个人";
    string[3] = "因为随着岁月流逝,";
    string[4] = "他们将......";
    image1 = createImage(getWidth(), getHeight() - 35);
    gg = image1.getGraphics();

    }

    public void start() {

    if (thread == null) {
    thread = new Thread(this);
    thread.start();
    }
    if (thread2 == null) {
    thread2 = new Thread(new thread2());
    thread2.start();
    }
    }

    public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(image1, 0, 0, this);
    Date date = new Date();
    int hour = date.getHours();
    int min = date.getMinutes();
    int sec = date.getSeconds();
    String m = Integer.toString(hour) + ":" + Integer.toString(min) + ":" +
    Integer.toString(sec);
    double ans = (Math.PI / 30) * sec;
    double anm = (3.1415926535897931D / 30) * min;
    double anh = ((hour + min / 60.0) * (2 * Math.PI / 12));
    xsec2 = 172 + (int) (45 * Math.sin(ans));
    ysec2 = 165 - (int) (45 * Math.cos(ans));
    xsec = 172 - (int) (13 * Math.sin(ans));
    ysec = 165 + (int) (13 * Math.cos(ans));
    xmin2 = 172 + (int) (40 * Math.sin(anm));
    ymin2 = 165 - (int) (40 * Math.cos(anm));
    xmin = 172 - (int) (10 * Math.sin(anm));
    ymin = 165 + (int) (10 * Math.cos(anm));
    xhou = 172 + (int) (30 * Math.sin(anh));
    yhou = 165 - (int) (30 * Math.cos(anh));
    xhou2 = 172 - (int) (10 * Math.sin(anh));
    yhou2 = 165 + (int) (10 * Math.cos(anh));
    g.setColor(colors); //秒针
    g.drawLine(xsec, ysec, xsec2, ysec2);
    g.setColor(colorm); //分针
    g.drawLine(xmin, ymin, xmin2, ymin2);
    g.setColor(colorh); //时针
    g.drawLine(xhou2, yhou2, xhou, yhou);
    g.setColor(Color.RED);
    g.fillOval(169, 162, 9, 9);
    g.drawString(m, 150, 100);
    g.setColor(Color.WHITE);
    g.drawString(string[0], xstr, y);
    g.drawString(string[1], xstr, y + 20);
    g.drawString(string[2], xstr, y + 40);
    g.drawString(string[3], xstr, y + 60);
    g.drawString(string[4], xstr, y + 80);

    }

    public void update(Graphics g) {

    paint(g);

    }


    public void run() {
    while (true) {
    try {
    thread.sleep(1000);
    gg.setColor(Color.WHITE);
    gg.fillRect(0, 0, width, height);
    gg.drawImage(image, 110, 100, width1, height1, this);

    repaint();
    } catch (Exception e) {
    System.out.print(e.getMessage());
    }
    }
    }


    private class thread2 implements Runnable {
    public void run() {
    while (true) {
    try {
    thread2.sleep(100);
    } catch (Exception e) {}

    y--;
    if (y <= 5) {
    y = ystr;
    }
    repaint();
    }

    }
    }


    public void mouseClicked(MouseEvent e) {
    System.out.print(e.getX());
    System.out.print(e.getY());
    }

    public void mouseExited(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {}

    public void fileSelect() {
    JFileChooser choose = new JFileChooser();
    choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
    choose.setCurrentDirectory(new File("."));
    choose.setFileFilter(new javax.swing.filechooser.FileFilter() {
    public boolean accept(File file) {
    String name = file.getName().toLowerCase();
    return name.endsWith(".gif")
    || name.endsWith(".jpg")
    || name.endsWith(".jpeg")
    || file.isDirectory();
    }

    public String getDescription() {
    return "图片文件";
    }
    }
    );
    int result = choose.showOpenDialog(this);
    String name = null;
    if (result == JFileChooser.APPROVE_OPTION) {
    name = choose.getSelectedFile().getAbsolutePath();
    }
    image = tool.getImage(name);
    m.addImage(image, 0);
    try {
    m.waitForAll();
    } catch (Exception e) {
    System.out.print(e.getMessage());
    }
    }

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button1 || e.getSource() == button2 ||
    e.getSource() == button3) {
    JColorChooser choose = new JColorChooser();
    Color c = choose.showDialog(this, "颜色选取", Color.BLACK);
    if (c == null) {
    c = Color.BLACK;
    }
    if (e.getSource() == button1) {
    colorh = c;
    }
    if (e.getSource() == button2) {
    colorm = c;
    }
    if (e.getSource() == button3) {
    colors = c;
    }
    }
    if (e.getSource() == button4) {
    fileSelect();
    }
    }
    }
    建立工程后,可以更改钟表皮肤,图片只有自己找了。

    来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-129979/,如需转载,请注明出处,否则将追究法律责任。

    转载于:http://blog.itpub.net/374079/viewspace-129979/

    展开全文
  • java钟表时针分针夹角计算器

    千次阅读 2017-10-06 22:44:57
    java钟表时针分针夹角计算器
    package Cal;
    
    import java.util.Scanner;
    
    public class Clock_angle {
        private float hour;
        private float minute;
        //通过set函数设置时针与分针
        public void set(float hour, float minute){
            this.hour = hour;
            this.minute = minute;
        }
        //get函数计算并输出结果
        public void get(){
            //将时钟的一圈分为60小格,每小格为一分钟,对应的角度为6度
            //时针偏移格数:七点三十分时,时针恰好介于7与8之间,则时针偏移了2.5格
            float angle = 0;
            if (hour * 5 > minute) {//时针 > 分针
                if (hour*5 - minute < 30) {//时针 - 分针格数 = 锐角
                    angle = (((hour * 5) - minute) - ((minute / 60) * 5)) * 6;
                    //角度 = (时针 - 分针格数 - 时针偏移格数)* 6(一格为6度)
                }else {//时针 - 分针格数 = 钝角
                    angle = 360 - (((hour * 5) - minute) + ((minute / 60) * 5)) * 6;
                    //角度 = 360 - (时针 - 分针 + 时针偏移格数)* 6
                }
            } else if (hour * 5 <= minute) {//分针 >= 时针
                if (minute - hour*5 < 30) {//分针 - 时针格数 = 锐角
                    angle = ((minute - hour * 5) - ((minute / 60) * 5)) * 6;
                    //角度 = (分针 - 时针 - 时针偏移格数)* 6
                }else {//分针 - 时针格数 = 钝角
                    angle = 360 - ((minute - hour * 5) + ((minute / 60) * 5)) * 6;
                    //角度 = 360 - (分针 - 时针 + 时针偏移格数)* 6
                }
            }
            System.out.println("angle :" + " " + angle);
        }
        public static void main(String[] args) {
            //创建对象time
            Clock_angle time = new Clock_angle();
            Scanner in = new Scanner(System.in);
            //输入小时
            System.out.println("input hour :");
            float hour = in.nextInt();
            //输入分钟
            System.out.println("input minute :");
            float minute = in.nextInt();
            //将时间传入get函数
            time.set(hour, minute);
            //计算并输出结果
            time.get();
        }
    }
    
    展开全文
  • java钟表时钟显示时间

    2011-04-20 22:32:10
    表盘式时钟,并显import java.awt.*; import java.awt.event.*; import javax.swing.*; import sun.util.calendar.Gregorian; import java.util.Calendar; import java.util.GregorianCalendar; public class ...
  • 一个钟表程序,非常漂亮,供初学者使用,希望对大家有点帮忙。
  • Java GUI钟表

    2019-02-28 21:58:49
    Java GUI编程代码简单实现钟表,时分秒三指针旋转,获取系统时间实时刷新指针位置。
  • Java 挂钟 钟表 程序

    2010-09-25 15:59:32
    Java 挂钟 钟表 程序Java 挂钟 钟表 程序Java 挂钟 钟表 程序Java 挂钟 钟表 程序
  • Java实现钟表

    2019-08-27 10:12:42
    Clock.java package clock; import java.util.Scanner; public class Clock { private Display hour = new Display(24); private Display minute = new Display(60); private Display second = new Display(60)...

    Clock.java

    package clock;
    
    import java.util.Scanner;
    
    public class Clock {
    	private Display hour = new Display(24);
    	private Display minute = new Display(60);
    	private Display second = new Display(60);
    	
    	public Clock(int hour, int minute, int second) {
    		this.hour.value = hour;
    		this.minute.value = minute;
    		this.second.value = second;
    	}
    	
    	public void start() {
    		while(true) {
    			second.increace();
    			if(second.getValue() == 0) {
    				minute.increace();
    				if(minute.getValue() == 0) {
    					hour.increace();
    				}
    			}
    			System.out.printf("%02d:%02d:%02d\n",hour.getValue(), minute.getValue(), second.getValue());
    			
    			//延时运行
    			try {
    				Thread.sleep(1000);
    			}catch(Exception e) {}
    		}
    	}
    
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		System.out.print("输入当前时间(时 分 秒):");
    		Clock clock = new Clock(in.nextInt(), in.nextInt(), in.nextInt());
            clock.start();
            in.close();
    	}
    }
    

    Display.java

    package clock;
    
    public class Display {
    	int value = 0;
    	private int limit = 0;
    	
    	public Display(int limit) {       //构造函数的作用是初始化
    		this.limit = limit;
    	}
    	
    	public void increace() {
    		value++;
    		if( value == limit ) {
    			value = 0;
    		}
    	}
    	
    	public int getValue() {
    		return value;
    	}
    
    	public static void main(String[] args) {
    		Display d = new Display(24);
    		for( ;; ) {
    			d.increace();
    			System.out.println(d.value);
    		}
    	}
    }
    
    展开全文
  • 钟表实现 java

    2009-04-16 22:00:55
    钟表 java实现 界面模仿钟表,优美。
  • 使用java编写的钟表程序,实现一个走动的钟表
  • 运用java设计简单的钟表,执行文件,打开即可运行
  • java语言实现的钟表

    2011-12-08 19:23:20
    一个用java编写的钟表希望能给初学java的人一点鼓励
  • java 编程 钟表 线程

    2014-05-23 14:34:06
    import java.awt.BorderLayout; import javax.swing.JFrame; public class Clock extends JFrame{  public Clock() {  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  thi
    import java.awt.BorderLayout;

    import javax.swing.JFrame;


    public class Clock extends JFrame{

        public Clock() {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(Global.FRAMEWIDTH , Global.FRAMEHEIGHT);
            this.setTitle("My Clock");
            this.setLocationRelativeTo(null);
            this.add(new ClockPanel() , BorderLayout.CENTER);
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            new Clock().setVisible(true);
        }

    }


    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.util.Calendar;
    import java.util.GregorianCalendar;

    import javax.swing.JPanel;

    public class ClockPanel extends JPanel{
        private GregorianCalendar now ;
        private int hour , minute , second ;
        public ClockPanel() {
            now = new GregorianCalendar();
            hour = now.get(Calendar.HOUR);
            minute = now.get(Calendar.MINUTE);
            second = now.get(Calendar.SECOND);
            // 线程启动
            getNowTimeThread();
        }
       
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);   
            g2.setStroke(new BasicStroke(2f));
            // 绘制表盘
            clockCircle(g2);
           
            // 绘制时针
            drawHour(g2);
            // 绘制分针
            drawMinute(g2);
            // 绘制秒针
            clockSecond(g2);
           
            // 绘制表盘的中心点
            g2.setColor(Color.RED);
            Ellipse2D circle = new Ellipse2D.Double(Global.CX-4, Global.CY-4, 8, 8);
            g2.fill(circle);
           
        }


     private void clockCircle(Graphics2D g) {
            super.paintComponent(g);
            // 表盘一圈360度,每间隔step画一个点
            for(int i = 0 ;i < 360; i+= Global.STEP) {
                double x , y;
                x = Global.CX + (Global.CLOCKR) * Math.cos(Math.toRadians(i - 90));
                y = Global.CY + (Global.CLOCKR) * Math.sin(Math.toRadians(i - 90));
                Ellipse2D circle ;
                if(i % (360/12) == 0) {
                    g.setColor(Color.RED);
                    circle = new Ellipse2D.Double(x-3, y-3, 6, 6);
                }
                else {               
                    g.setColor(Color.BLUE);
                    circle = new Ellipse2D.Double(x-2, y-2, 4, 4);
                }
                g.fill(circle);
            }
        }

    // 绘制秒针
        private void clockSecond(Graphics2D g) {
            // 起点坐标
            double x0 , y0;
            x0 = Global.CX + 15 * Math.cos(Math.toRadians(Global.STEP * second + 90));
            y0 = Global.CY + 15 * Math.sin(Math.toRadians(Global.STEP * second + 90));
           
            // 终点坐标
            double x , y;
            x = Global.CX + Global.SECONDR * Math.cos(Math.toRadians(Global.STEP * second - 90));
            y = Global.CY + Global.SECONDR * Math.sin(Math.toRadians(Global.STEP * second - 90));
            Line2D line = new Line2D.Double(x0, y0, x, y);
            g.setStroke(new BasicStroke(3f));
            g.setColor(Color.BLUE);
            g.draw(line);
        }


    //绘制分针
        public void drawMinute(Graphics2D g) {
           
            int x1 = (int) (Global.CX + Global.MINUTER*Math.cos(Math.toRadians(minute*6-90)));
            int x2 = (int) (Global.CX + 5*Math.cos(Math.toRadians(minute*6)));       
            int x3 = (int) (Global.CX + 10*Math.cos(Math.toRadians(minute*6-270)));;       
            int x4 = (int) (Global.CX + 5*Math.cos(Math.toRadians(minute*6-180)));
           
            int y1 = (int) (Global.CY + Global.MINUTER*Math.sin(Math.toRadians(minute*6-90)));
            int y2 = (int) (Global.CY + 7*Math.sin(Math.toRadians(minute*6)));
            int y3 = (int) (Global.CY + 10*Math.sin(Math.toRadians(minute*6-270)));
            int y4 = (int) (Global.CY + 7*Math.sin(Math.toRadians(minute*6-180)));
            int[] xPoints = {x1 , x2 , x3 , x4};
            int[] yPoints = {y1 , y2 , y3 , y4};
            g.setColor(Color.LIGHT_GRAY);
            g.fillPolygon(xPoints, yPoints, 4);       
            g.setColor(Color.BLACK);
            g.drawPolygon(xPoints, yPoints, 4);       
        }


    // 绘制时针
        public void drawHour(Graphics2D g) {
            int x1 =  (int) (Global.CX + Global.HINUTER*Math.cos(Math.toRadians(hour*30-90 + minute * 6/12)));
            int x2 =  (int) (Global.CX + 5*Math.cos(Math.toRadians(hour*30 + minute * 6/12)));
           
            int x3 = (int) (Global.CX + 10*Math.cos(Math.toRadians(hour*30-270 + minute * 6/12)));;      
            int x4 =  (int) (Global.CX + 5*Math.cos(Math.toRadians(hour*30-180 + minute * 6/12)));
           
            int y1 = (int) (Global.CY + Global.HINUTER*Math.sin(Math.toRadians(hour*30-90 + minute * 6/12)));
            int y2 = (int) (Global.CY + 7*Math.sin(Math.toRadians(hour*30 + minute * 6/12)));
            int y3 = (int) (Global.CY + 10*Math.sin(Math.toRadians(hour*30-270 + minute * 6/12)));
            int y4 = (int) (Global.CY + 7*Math.sin(Math.toRadians(hour*30-180 + minute * 6/12)));
            int[] xPoints = {x1 , x2 , x3 , x4};
            int[] yPoints = {y1 , y2 , y3 , y4};
            g.setColor(Color.LIGHT_GRAY);
            g.fillPolygon(xPoints, yPoints, 4);
            g.setColor(Color.BLACK);
            g.drawPolygon(xPoints, yPoints, 4);       
        }
       
        // 时钟时分秒变化的线程
        public void getNowTimeThread() {
            new Thread() {
                public void run() {
                    while(true) {
                        second++;
                        if(second > 59) {
                            minute += 1;
                            second = 0;
                        }
                        if(minute > 59) {
                            hour += 1;
                            minute = 0;
                        }
                        if(hour > 24) {
                            hour = 0;
                        }
                        repaint();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }                   
                    }
                }
            }.start();
        }
    }


    public class Global {
        // 设置一下影响窗体大小和时钟大小相关参数
        public static final int MARGINTOP = 30;
        public static final int MARGINLEFT = 30;
        public static final int CLOCKR = 150;
        public static final int SECONDR = CLOCKR - 10;
        public static final int MINUTER = CLOCKR - 10 - 15;
        public static final int HINUTER = CLOCKR - 10 - 15 * 2;
       
       
        public static final int STEP = 360 / 60;
        public static final int CX = MARGINLEFT + CLOCKR;
        public static final int CY = MARGINTOP  + CLOCKR;
       
        public static final int FRAMEWIDTH = CX * 2 + 8;
        public static final int FRAMEHEIGHT = CY * 2 + 30;
    }

    展开全文
  • 基于Java钟表(含代码和报告)
  • Java圆形时钟,钟表源码,带表盘的圆形电子钟,运行后随Windows时间一起走动,Java时间相关操作的知识点,在本源码中都会有所涉及,对初学Java来说,有很不错的参考价值。
  • java awt 模拟钟表

    2010-10-29 18:41:59
    import java.awt.BasicStroke; import java.awt.geom.Ellipse2D; import java.text.SimpleDateFormat; import java.util.Locale; public class run extends javax.swing.JFrame { // 定义一Graphics变量,...
  • 一个Java写的钟表程序

    2017-10-15 09:39:13
    钟表包含了时钟,分钟,秒钟的计算。 去了锯齿,加上了日历。有完整的代码及运程程序
  • ... import java.awt.*; import java.applet.*;...import java.util.*;...//操作运用多线程实现一个钟表的类 public class TextClock extends Applet implements Runnable { Thread HOUR_THREAD = null;// 表示...
  • 使用多线程的方式实现钟时分秒针的移动,首创!
  • 简单的java 程序 适合 初学者 涉及java绘图 及一些坐标计算
  • Java下编写的钟表(源代码可直接下载使用)
  • bells 这是一个npm模块,可帮助计算响铃计划。 安装 npm install --save bells发布后保存npm install --save bells 特征 太棒了 存储特定期间的名称,不一定是数字或顺序 获取当前期间 在下一个钟声响起时获取 ...
  • 【实例简介】JAVA做的一个钟表日历提醒等功能的系统。很简单的应用希望对你有帮助【实例截图】【核心代码】3901295e-9086-4662-ae0c-8c3922f0a8b9└── Demo├── 1841_9531.jpg├── Arrange.class├── ...
  • 我用了一个Display类的两个对象表示小时和分钟 ,另一个Clock类的对象去实现交互,但结果不对,代码如图[img=https://img-bbs.csdn.net/upload/202004/14/1586833892_723136.png][/img][img=...
  • Java小练习1(钟表

    千次阅读 2013-05-22 14:23:51
    这两天用Java写了一个小程序,一个会自动走的闹钟。 下面说一下具体的思路,有错误的也有正确的。 错误的思路: 1)以0点0分0秒线为基准,提取系统时间,让时针分针秒针转动相应的角度,得出真正的时间。...
  • java-clock 简单实现给jpanel加一个本地钟表图片并在上面画出表针显示时间来练习多线程java
  • 钟表效果 var today; var hour; var fen; var s; var todayAction; function todayTimeAction () { today = new Date(); hour = today.getHours(); fen = today.getMinutes(); s = today....
  • 学习Java有一段时间了,响应老师的号召,做了这一个动态钟表程序。其中,StillClock.java主要实现钟表的外观,ClockAnimation.java使钟表与系统时间始终保持同步。实现结果如下所示: 下面是具体实现部分:    ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,242
精华内容 896
关键字:

java钟表

java 订阅