当前位置: 代码迷 >> J2SE >> Java小程序时钟代码,请帮忙解释下代码!多谢
  详细解决方案

Java小程序时钟代码,请帮忙解释下代码!多谢

热度:226   发布时间:2016-04-24 17:56:34.0
Java小程序时钟代码,请帮忙解释下代码!谢谢!
由于有些类还没学好,所以对程序的绘图不怎么明白,可以帮忙讲解下或者加点注释吗?谢谢了!
import   java.awt.*;
import   java.awt.geom.Ellipse2D;
import   java.awt.geom.GeneralPath;
import   java.awt.geom.Line2D;
import   java.awt.geom.Rectangle2D;
import   java.util.Calendar;
import   java.util.Date;

import   javax.swing.BorderFactory;
import   javax.swing.JComponent;
import   javax.swing.JFrame;
import   javax.swing.UIManager;

public   class   Clock   extends   JComponent
{
private   static   final   Color   INTEGRAL_COLOR   =   new   Color(0,   128,   128);

private   int   radius;

private   Calendar   currentTime   =   Calendar.getInstance();

private   double   s   =   0.03;

public   Clock(int   radius)
{
this.radius   =   radius;
}

public   void   setCurrentTime(Date   time)         //设置当前时间
{
this.currentTime.setTime(time);
}

public   void   setCurrentTime(long   millis)
{
this.currentTime.setTimeInMillis(millis);
}

public   Dimension   getPreferredSize()
{
Insets   insets   =   getInsets();
int   r   =   (int)   (radius   ==   -1   ?   0   :   radius*(1+s))+1;
return   new   Dimension(r   *   2   +   insets.left   +   insets.right,r   *   2     +   insets.top   +   insets.bottom);     //返回一个指定宽、高的Dimension
}

protected   void   paintComponent(Graphics   g)
{
super.paintComponent(g);

Graphics2D   g2d   =   (Graphics2D)   g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,   RenderingHints.VALUE_ANTIALIAS_ON);

Insets   insets   =   getInsets();
int   wid   =   getWidth()   -   insets.left   -   insets.right;
int   hei   =   getHeight()   -   insets.top   -   insets.bottom;
int   r   =   (int)   ((Math.min(wid,   hei))   /   2   /   (1+s));

g2d.translate(insets.left   +   r   *   (1+s),   insets.top   +   r   *   (1+s));
g2d.scale(1,   -1);

for   (int   i   =   0;   i   <   60;   i++)   {
int   angle   =   90   -   i   *   6;
double   pos[]   =   calcPos(r,   angle);
paintMinuteDot(r,   g2d,   pos[0],   pos[1],   i   %   5   ==   0);
}

paintHourPointer(r,   g2d);
paintMinutePointer(r,   g2d);
paintSecondPointer(r,   g2d);
paintCenterPoint(g2d);

g2d.scale(1,   -1);
g2d.translate(-insets.left   -   r   *   (1+s),   -insets.top   -   r   *   (1+s));
}

private   void   paintCenterPoint(Graphics2D   g2d)
{
g2d.setColor(Color.BLUE);
Rectangle2D   rect   =   new   Rectangle2D.Double(-2,   -2,   4,   4);
g2d.fill(rect);
}

private   void   paintMinutePointer(int   r,   Graphics2D   g2d)
{
int   minute   =   currentTime.get(Calendar.MINUTE);
int   second   =   currentTime.get(Calendar.SECOND);
double   angle   =   90   -   (minute   +   second   /   60.0)   *   6;
  相关解决方案