| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 932 人关注过本帖
标题:画时针问题
只看楼主 加入收藏
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
结帖率:83.33%
收藏
 问题点数:0 回复次数:3 
画时针问题
一个时针该怎么画?我看书上是这样写的:
       g.drawLine(center_x, center_y, second_x, second_y);
        
        g.drawLine(center_x, center_y - 1, minute_x, minute_y);
        g.drawLine(center_x - 1, center_y, minute_x, minute_y);
        
        g.drawLine(center_x, center_y - 1, hour_x, hour_y);
        g.drawLine(center_x - 1, center_y, hour_x, hour_y);
有明白为什么画分钟和秒钟时画了两条直线?
搜索更多相关主题的帖子: 时针 
2008-11-22 11:12
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
收藏
得分:0 
书上的一个applet程序,但运行时网页中只出现没有时针,分针和秒针的表,并且没有数字显示,不知为什么?
import java.awt.*;
import java.applet.Applet;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class ClockApplet extends Applet implements Runnable  //Applet支持线程
{
    private Thread athread;                                //线程
    private SimpleDateFormat sdateformat;                  //日期格式

    private int second_lastx=0, second_lasty=0;            //上次秒针值
    private int minute_lastx=0, minute_lasty=0;            //上次分针值
    private int hour_lastx=0, hour_lasty=0;                //上次时针值
   
    public void init()
    {
        this.setBackground(Color.white);
        this.athread = null;
        this.sdateformat = new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒 a EEEEE");
    }
   
    public void paint(Graphics g)                          //显示数字和图形时钟
    {
        Calendar rightnow = Calendar.getInstance();        //获取当前日期和时间
        int second = rightnow.get(Calendar.SECOND);        //当前秒
        int minute = rightnow.get(Calendar.MINUTE);        //分
        int hour = rightnow.get(Calendar.HOUR);            //时

        int center_x = this.getWidth()/2;                  //图形时钟的原点
        int center_y = 60;
                                                           //以下计算秒针、分针、时针位置
        int second_x = (int)(Math.cos(second*Math.PI/30 - Math.PI/2)*45 + center_x);
        int second_y = (int)(Math.sin(second*Math.PI/30 - Math.PI/2)*45 + center_y);
        
        int minute_x = (int)(Math.cos(minute * Math.PI/30 - Math.PI/2)*40 + center_x);
        int minute_y = (int)(Math.sin(minute * Math.PI/30 - Math.PI/2)*40 + center_y);
        
        int hour_x = (int)(Math.cos((hour*30+minute/2)*Math.PI/180-Math.PI/2)*30 + center_x);
        int hour_y = (int)(Math.sin((hour*30+minute/2)*Math.PI/180-Math.PI/2)*30 + center_y);
        
        g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
        g.setColor(Color.blue);
        g.drawOval(center_x-50, center_y-50, 100, 100);    //画圆和数字
        g.setColor(Color.darkGray);
        g.drawString("9", center_x-45, center_y+3);
        g.drawString("3", center_x+40, center_y+3);
        g.drawString("12", center_x-5, center_y-37);
        g.drawString("6",  center_x-3, center_y+45);
        
        //时间变化时,需要重新画各个指针,即先消除原有指针,然后画新指针
        g.setColor(this.getBackground());                  //用背景色画线,可以消除原来画的线
        if (second_x != second_lastx || second_y != second_lasty)    //秒针变化
        {
            g.drawLine(center_x, center_y, second_lastx, second_lasty);
        }
        
        if (minute_x != minute_lastx || minute_y != minute_lasty)    //分针变化
        {
            g.drawLine(center_x, center_y-1, minute_lastx, minute_lasty);
            g.drawLine(center_x-1, center_y, minute_lastx, minute_lasty);
        }
        
        if (hour_x != hour_lastx || hour_y != hour_lasty)            //时针变化
        {
            g.drawLine(center_x, center_y - 1, hour_lastx, hour_lasty);
            g.drawLine(center_x - 1, center_y, hour_lastx, hour_lasty);
        }
        
        g.setColor(Color.darkGray);                        //显示数字时钟
        g.drawString(sdateformat.format(rightnow), 0, 150);  
        
        
        g2.setStroke(new BasicStroke(2));
//画新指针
        g.drawLine(center_x, center_y, second_x, second_y);
        
        g.drawLine(center_x, center_y - 1, minute_x, minute_y);
        g.drawLine(center_x - 1, center_y, minute_x, minute_y);
        
        g.drawLine(center_x, center_y - 1, hour_x, hour_y);
        g.drawLine(center_x - 1, center_y, hour_x, hour_y);
        
        second_lastx = second_x;                           //保存指针位置
        second_lasty = second_y;
        minute_lastx = minute_x;
        minute_lasty = minute_y;
        hour_lastx = hour_x;
        hour_lasty = hour_y;
    }
   
    public void start()
    {
        if(athread == null)
        {
            athread = new Thread(this);
            athread.start();                     //启动线程
        }
    }
   
    public void stop()
    {
        if(athread != null)
        {
            athread.interrupt();                 //中断线程
            athread = null;
        }
    }
     
    public void run()                            //线程体,每隔一秒钟,刷新一次画面
    {
        while (athread != null)
        {
             repaint();                          //调用paint()方法重画时钟
             try
             {
                 athread.sleep(1000);
             }
             catch (InterruptedException e) {}
        }
    }
}
2008-11-22 11:14
liugy52
Rank: 1
等 级:新手上路
帖 子:89
专家分:0
注 册:2006-10-1
收藏
得分:0 
java.util.Date time = rightnow.getTime();
g.drawString(sdateformat.format(time), 0, 150);

sdateformat需要Data类型的参数

我在大家热情的帮助下进步,谢谢你们!
2008-11-22 12:14
zacom
Rank: 2
等 级:论坛游民
威 望:2
帖 子:381
专家分:15
注 册:2007-9-15
收藏
得分:0 
为了显示粗细

没有最好只有更好
2008-11-22 13:42
快速回复:画时针问题
数据加载中...
 
   



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

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