画时针问题
一个时针该怎么画?我看书上是这样写的: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);
有明白为什么画分钟和秒钟时画了两条直线?
搜索更多相关的解决方案:
时针
----------------解决方案--------------------------------------------------------
书上的一个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) {}
}
}
}
----------------解决方案--------------------------------------------------------
java.util.Date time = rightnow.getTime();
g.drawString(sdateformat.format(time), 0, 150);
sdateformat需要Data类型的参数
----------------解决方案--------------------------------------------------------
为了显示粗细
----------------解决方案--------------------------------------------------------