java秒针的简单介绍

博主:adminadmin 2022-12-20 17:54:09 73

本篇文章给大家谈谈java秒针,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用JAVA编了一个计时器,想用wait()方法让秒针停止,为什么点击暂停后整个对话框都没反应了,关也关不住

你调wait()方法的时候,main线程就已经等待了。除非有其他线程去唤醒它,它自己是无法唤醒自己的。你调试的话可以发现,mi.wait();就已经卡着不动了,如果不需要多线程的话,建议sleep或者BufferedReader等待。

java eclipse 时分秒针角度

这个,最好是拿个钟对着。。。。

程序中讲了,秒针角度,一个刻度是6度。总共60秒,走过了几秒,就是多少度。减号后面让更精确。

分针就没有。

小时中,也是小时的刻度,再判断大概走了多少分钟,减号后面那是点到小时的刻度的中间某个点来。

JAVA画时钟代码

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 ClockPointer extends JFrame{

        int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y, 

        ss,mm, hh, old_m, old_h, ang;

        final double RAD = Math.PI/180;

    public ClockPointer(){

        super("Java时钟");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Image image = getToolkit().getImage("clock.gif");

        setIconImage(image);

        setSize(400,400);

        setBackground(Color.white);

        //setLocation(300,150);

        this.setLocationRelativeTo(null);

        setResizable(true);

        int delay = 1000;

        //创建一个监听事件

        setVisible(true);

        ActionListener drawClock = new ActionListener(){

            public void actionPerformed(ActionEvent evt){

                repaint();

            }

        };

        //创建一个时间计数器,每一秒触发一次

        new Timer(delay, drawClock).start();

    }

    

    java.text.SimpleDateFormat fmTime = new java.text.SimpleDateFormat("HH:mm:ss");

    //绘制图形

    public void paint(Graphics g){

        super.paint(g);

        g.setFont(null);

        Graphics2D g2D = (Graphics2D)g;

        

        Insets insets = getInsets();

        int L = insets.left/2, T = insets.top/2;

        h = getSize().height;

        g.setColor(Color.white);

        //画圆

        g2D.setStroke(new BasicStroke(2.0f));

        g.setColor(Color.gray);

        g.drawOval(L+40, T+40, h-80, h-80);

        r = h/2 - 40;

        x0 = 40 + r - 5 + L;

        y0 = 40 + r - 5 - T;

        ang = 60;

        //绘制时钟上的12个字

        for(int i = 1;i = 12;i ++){

            x = (int)((r+10)*Math.cos(RAD*ang)+x0);

            y = (int)((r+10)*Math.sin(RAD*ang)+y0);

            g.setColor(Color.black);

            g.drawString(""+i, x, h-y);

            ang -=30;

        }

        //获得现在的时间

        Calendar now = new GregorianCalendar();

        int nowh = now.get(Calendar.HOUR_OF_DAY);

        int nowm = now.get(Calendar.MINUTE);

        int nows = now.get(Calendar.SECOND);

        

        String st=fmTime.format(now.getTime());

        //在窗体上显示时间

        g.setColor(Color.pink);

        g.fillRect(L, T, 50, 28);

        g.setColor(Color.blue);

        g.drawString(st,L+2,T+26);

        

        //计算时间与度数的关系

        ss = 90 - nows*6;

        mm = 90 - nowm*6;

        hh = 90 - nowh*30 - nowm/2;

        x0 = r+40+L;

        y0 = r+40+T;

        g2D.setStroke(new BasicStroke(1.2f));

        //擦除秒针

        //if(olds_x  0){

        //    g.setColor(getBackground());

        //    // g.setColor(Color.gray);

        //    g.drawLine(x0, y0, olds_x, h-olds_y); // (?)

        //}

        //绘制秒针

        x = (int)(r*0.9*Math.cos(RAD*ss))+x0;

        y = (int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;

        g.setColor(Color.yellow);

        g.drawLine(x0, y0, x, h-y);

        olds_x = x;

        olds_y = y;

        g2D.setStroke(new BasicStroke(2.2f));

        //擦除分针

        //if(old_m!=mm){

        //    g.setColor(getBackground());

        //    g.drawLine(x0,y0,oldm_x,h-oldm_y);

        //}

        //绘制分针

        x = (int)(r*0.7*Math.cos(RAD*mm))+x0;

        y = (int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;

        g.setColor(Color.green);

        g.drawLine(x0,y0,x,h-y);

        oldm_x = x;

        oldm_y = y;

        old_m = mm;

        g2D.setStroke(new BasicStroke(3.2f));

        //擦除时针

        //if(old_h!=hh){

        //    g.setColor(getBackground());

        //    g.drawLine(x0,y0,oldh_x,h-oldh_y);

        //}

        //绘制时针

        x = (int)(r*0.5*Math.cos(RAD*hh))+x0;

        y = (int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;

        g.setColor(Color.red);

        g.drawLine(x0,y0,x,h-y);

        oldh_x = x;

        oldh_y = y;

        old_h = hh;

    }

    public static void main(String[] args){

        new ClockPointer();

    }

}

//整理一下

用java编程逐行输出 钟表一天内三指针重合时间,精确的秒

long base=new GregorianCalendar(0,0,0,0,0,0).getTime().getTime(); 定义一个0毫秒的基数,为了后面使用format做准备。

DateFormat df=new SimpleDateFormat("HH:mm:ss"); 设置格式化的样式

for循环是安装一天时间长度的总共有60*60*12*2秒(这个因为要精确到秒),在每个秒长度时间上,分别获得时针、分钟和秒针所在表盘的位置(表盘整个圆是60秒)。

然会判断三个针位置是否是一样的,如果一样就输入该时间。

Java 设计一个clock类 要求实现时针,分针,秒针

/*

* @(#)Clock.java 1.16 06/02/22

*

* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.

*

* Redistribution and use in source and binary forms, with or without

* modification, are permitted provided that the following conditions are met:

*

* -Redistribution of source code must retain the above copyright notice, this

* list of conditions and the following disclaimer.

*

* -Redistribution in binary form must reproduce the above copyright notice,

* this list of conditions and the following disclaimer in the documentation

* and/or other materials provided with the distribution.

*

* Neither the name of Sun Microsystems, Inc. or the names of contributors may

* be used to endorse or promote products derived from this software without

* specific prior written permission.

*

* This software is provided "AS IS," without a warranty of any kind. ALL

* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING

* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE

* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")

* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE

* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS

* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST

* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,

* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY

* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,

* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

*

* You acknowledge that this software is not designed, licensed or intended

* for use in the design, construction, operation or maintenance of any

* nuclear facility.

*/

/*

* @(#)Clock.java 1.16 06/02/22

*/

import java.util.*;

import java.awt.*;

import java.applet.*;

import java.text.*;

/**

* Time!

*

* @author Rachel Gollub

* @modified Daniel Peek replaced circle drawing calculation, few more changes

*/

public class Clock extends Applet implements Runnable {

private volatile Thread timer; // The thread that displays clock

private int lastxs, lastys, lastxm,

lastym, lastxh, lastyh; // Dimensions used to draw hands

private SimpleDateFormat formatter; // Formats the date displayed

private String lastdate; // String to hold date displayed

private Font clockFaceFont; // Font for number display on clock

private Date currentDate; // Used to get date to display

private Color handColor; // Color of main hands and dial

private Color numberColor; // Color of second hand and numbers

private int xcenter = 80, ycenter = 55; // Center position

public void init() {

int x,y;

lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;

formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy",

Locale.getDefault());

currentDate = new Date();

lastdate = formatter.format(currentDate);

clockFaceFont = new Font("Serif", Font.PLAIN, 14);

handColor = Color.blue;

numberColor = Color.darkGray;

try {

setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),

16)));

} catch (NullPointerException e) {

} catch (NumberFormatException e) {

}

try {

handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),

16));

} catch (NullPointerException e) {

} catch (NumberFormatException e) {

}

try {

numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),

16));

} catch (NullPointerException e) {

} catch (NumberFormatException e) {

}

resize(300,300); // Set clock window size

}

// Paint is the main part of the program

public void update(Graphics g) {

int xh, yh, xm, ym, xs, ys;

int s = 0, m = 10, h = 10;

String today;

currentDate = new Date();

formatter.applyPattern("s");

try {

s = Integer.parseInt(formatter.format(currentDate));

} catch (NumberFormatException n) {

s = 0;

}

formatter.applyPattern("m");

try {

m = Integer.parseInt(formatter.format(currentDate));

} catch (NumberFormatException n) {

m = 10;

}

formatter.applyPattern("h");

try {

h = Integer.parseInt(formatter.format(currentDate));

} catch (NumberFormatException n) {

h = 10;

}

// Set position of the ends of the hands

xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);

ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);

xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);

ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);

xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30

+ xcenter);

yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30

+ ycenter);

// Get the date to print at the bottom

formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");

today = formatter.format(currentDate);

g.setFont(clockFaceFont);

// Erase if necessary

g.setColor(getBackground());

if (xs != lastxs || ys != lastys) {

g.drawLine(xcenter, ycenter, lastxs, lastys);

g.drawString(lastdate, 5, 125);

}

if (xm != lastxm || ym != lastym) {

g.drawLine(xcenter, ycenter-1, lastxm, lastym);

g.drawLine(xcenter-1, ycenter, lastxm, lastym);

}

if (xh != lastxh || yh != lastyh) {

g.drawLine(xcenter, ycenter-1, lastxh, lastyh);

g.drawLine(xcenter-1, ycenter, lastxh, lastyh);

}

// Draw date and hands

g.setColor(numberColor);

g.drawString(today, 5, 125);

g.drawLine(xcenter, ycenter, xs, ys);

g.setColor(handColor);

g.drawLine(xcenter, ycenter-1, xm, ym);

g.drawLine(xcenter-1, ycenter, xm, ym);

g.drawLine(xcenter, ycenter-1, xh, yh);

g.drawLine(xcenter-1, ycenter, xh, yh);

lastxs = xs; lastys = ys;

lastxm = xm; lastym = ym;

lastxh = xh; lastyh = yh;

lastdate = today;

currentDate = null;

}

public void paint(Graphics g) {

g.setFont(clockFaceFont);

// Draw the circle and numbers

g.setColor(handColor);

g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);

g.setColor(numberColor);

g.drawString("9", xcenter-45, ycenter+3);

g.drawString("3", xcenter+40, ycenter+3);

g.drawString("12", xcenter-5, ycenter-37);

g.drawString("6", xcenter-3, ycenter+45);

// Draw date and hands

g.setColor(numberColor);

g.drawString(lastdate, 5, 125);

g.drawLine(xcenter, ycenter, lastxs, lastys);

g.setColor(handColor);

g.drawLine(xcenter, ycenter-1, lastxm, lastym);

g.drawLine(xcenter-1, ycenter, lastxm, lastym);

g.drawLine(xcenter, ycenter-1, lastxh, lastyh);

g.drawLine(xcenter-1, ycenter, lastxh, lastyh);

}

public void start() {

timer = new Thread(this);

timer.start();

}

public void stop() {

timer = null;

}

public void run() {

Thread me = Thread.currentThread();

while (timer == me) {

try {

Thread.currentThread().sleep(100);

} catch (InterruptedException e) {

}

repaint();

}

}

public String getAppletInfo() {

return "Title: A Clock \n"

+ "Author: Rachel Gollub, 1995 \n"

+ "An analog clock.";

}

public String[][] getParameterInfo() {

String[][] info = {

{"bgcolor", "hexadecimal RGB number",

"The background color. Default is the color of your browser."},

{"fgcolor1", "hexadecimal RGB number",

"The color of the hands and dial. Default is blue."},

{"fgcolor2", "hexadecimal RGB number",

"The color of the second hand and numbers. Default is dark gray."}

};

return info;

}

}

applet 编译用个网页加载一下

HTML

HEAD

TITLEA Clock (1.6)/TITLE

/HEAD

BODY

h1A Clock (1.6)/h1

hr

applet code="Clock.class" width=300 height=300

alt="Your browser understands the APPLET tag but isn't running the applet, for some reason."

Your browser is completely ignoring the APPLET tag!

/applet

p

The clock applet now has three parameters; the background

color (bgcolor), the main foreground color (the hands and

dial) (fgcolor1) and the secondary foreground color (the

seconds hand and numbers) (fgcolor2). These three parameters

are hexadecimal RGB numbers (like the ones used for the body

bgcolor tag in HTML). For example:

p

applet code="Clock.class" width=300 height=300br

param name=bgcolor value="000000"br

param name=fgcolor1 value="ff0000"br

param name=fgcolor2 value="ff00ff"br

/appletp

would give you a black background, a red dial and hands, and purple numbers.

p

For those who don't convert to hexadecimal easily, here are some common

values:

ul

liblack = 000000

liblue = 0000ff

licyan = 00ffff

lidarkGray = 404040

ligray = 808080

ligreen = 00ff00

lilightGray = c0c0c0

limagenta = ff00ff

liorange = ffc800

lipink = ffafaf

lired = ff0000

liwhite = ffffff

liyellow = ffff00

/ul

hr

a href="Clock.java"The source/a.

/BODY

/HTML

JAVA中在绘制走动的时钟界面,如何去掉秒针前一次的状态。

你可以用底色在原来位置画一条线,就清除掉了,但是有可能会把其他的数字和分针之类的给清除掉。

所以可以用几张image分层显示,一层显示数字和圆圈,一层时针,一层分针,一层秒针。

重绘的话可以清空所有内容,用底色填充整个区域,然后再按先后次序绘制。

java秒针的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java秒针的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-20,除非注明,否则均为首码项目网原创文章,转载请注明出处。