当前位置: 代码迷 >> Java相关 >> [求助]大家都来看看
  详细解决方案

[求助]大家都来看看

热度:345   发布时间:2006-06-17 09:09:56.0
[求助]大家都来看看
我想实现一个自定义的闹钟,一点就自己开始走,

我就是给闹钟的 分秒时算的时候,老是把分秒时的针的长度画不好,
画的钟那个院的半径是120;
drawLine(200,200,x,y) ,
这个XY总是算不好;
随能给点好的思路!!

另外还需要问问怎么能 简单 是把刚画的东西,马上差掉,

谢谢大家帮忙了
搜索更多相关的解决方案: 闹钟  

----------------解决方案--------------------------------------------------------

import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class Clock extends Applet implements Runnable
{
Thread thisThread;
Color faceColor, borderColor, minuteColor, hourColor, secondColor;
Calendar d;
public void init()
{
//read in the colors for each of the hands and for the face/border
faceColor = readColor(getParameter("faceCol"));
borderColor = readColor(getParameter("borderCol"));
minuteColor = readColor(getParameter("minuteCol"));
hourColor = readColor(getParameter("hourCol"));
secondColor = readColor(getParameter("secondCol"));
}

// This method creates a color based on a string.
// The string is assumed to be "red, green, blue" where each
// of the colors is represented by its integer equivalent.
public Color readColor(String aColor)
{
if(aColor == null)
return Color.black;
int r, g, b;

//break the string apart into each number
StringTokenizer st = new StringTokenizer(aColor, ",");

try
{
r = Integer.valueOf(st.nextToken()).intValue();
g = Integer.valueOf(st.nextToken()).intValue();
b = Integer.valueOf(st.nextToken()).intValue();
return new Color(r,g,b);
}
catch(Exception e)
{
System.out.println("An exception occurred trying to convert a parameter to a color:" + e);
return Color.black;
}
}
public String getParameter(String name)
{
String ST;
if(inApplet)
return super.getParameter(name);
//if you are not in an applet you default all of the values
if(name == "hourcol")
return "255,00,00";
if(name == "minuteCol")
return "00,255,00";
if(name == "secondCol")
return "00,00,255";
if(name == "borderCol")
return "255,255,255";
if(name == "faceCol")
return "125,125,125";
return null;
}
public void start()
{
thisThread = new Thread(this);
thisThread.start();
}

public void run()
{
while(true)
{
repaint();
try
{
thisThread.sleep(1000);
}catch(Exception e){}
}
}

public void update(Graphics g)
{
paint(g);
if(inApplet)
{
int uhr = d.get(Calendar.HOUR_OF_DAY);
int minute = d.get(Calendar.MINUTE);
int sec = d.get(Calendar.SECOND);
String currentTime = new String(uhr + ":" + minute + ":" + sec);
showStatus(currentTime);
}
}

public void paint(Graphics g)
{
//fill clock face
g.setColor(faceColor);
g.fillOval(0,0,100,100);
g.setColor(borderColor);
g.drawOval(0,0,100,100);

//get the current time
d = Calendar.getInstance();
//draw the minute hand
g.setColor(minuteColor);
double angle = (((double)(90 - d.get(Calendar.MINUTE)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50),50+(int)(Math.cos(angle)*50));
//draw the hour hand
g.setColor(hourColor);
angle = ((((double)18 - d.get(Calendar.HOUR_OF_DAY)+(double)d.get(Calendar.MINUTE)/60))/12)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*40), 50+(int)(Math.cos(angle)*40));
//draw the second hand
g.setColor(secondColor);
angle = (((double)(90 - d.get(Calendar.SECOND)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50), 50+(int)(Math.cos(angle)*50));
}
static boolean inApplet = true;
public static void main(String args[])
{
//set a boolean flag to show if you are in an applet or not
inApplet = false;

//create a frame to place our application in
//you can change the string value to show your desired label
//for the frame
Frame myFrame = new Frame("Clock as an Application");
myFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
//create a clock instance
Clock myApp = new Clock();

//add the current application to the frame
myFrame.add("Center", myApp);

//resize the frame to the desired size and make it visible
myFrame.setSize(100,130);
myFrame.show();

//run the methods the browser normally would
myApp.init();
myApp.start();
}
}


----------------解决方案--------------------------------------------------------

这个问题,比较复杂有没有简单一点的啊,画图这一块搞不懂


----------------解决方案--------------------------------------------------------
楼上的漂亮,虽然简单,但是它真走。呵呵,可惜我不是搞这个的,否则一定要研究研究
----------------解决方案--------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class Clock extends JPanel implements Runnable{
private JLabel jl;
private DateFormat df;
public Clock(){
jl=new JLabel();
jl.setHorizontalAlignment(JLabel.CENTER);
df=DateFormat.getDateTimeInstance();
new Thread(this).start();
this.setLayout(new BorderLayout());
this.add(jl,BorderLayout.SOUTH);
}
public void run(){
while(true){
try{
Thread.sleep(1000);
}
catch(InterruptedException ie){
ie.printStackTrace();
}
jl.setText(df.format(new Date()));
repaint();
}

}
public void paintComponent(Graphics g){
super.paintComponent(g);
Calendar cal=Calendar.getInstance();
//得到当前的时间信息
int hour=cal.get(Calendar.HOUR);
int minute=cal.get(Calendar.MINUTE);
int second=cal.get(Calendar.SECOND);
//得到当前的面板的大小信息
int width=this.getWidth();
int height=this.getHeight();
//钟的那个圆盘取两个之中小的那个
int small=width<height?width:height;
int diameter=(int)(small*0.8);
int radius=diameter/2;
//确定中心点
Point center=new Point(width/2,height/2);
//确定时针,分钟,秒针的长度
int secondLength=(int)(radius*0.8);
int minuteLength=(int)(secondLength*0.8);
int hourLength=(int)(minuteLength*0.8);
//确定时针,分针,秒针的另一端坐标
int secondX=center.x+(int)(secondLength*Math.sin(second*2*Math.PI/60.0));
int secondY=center.y-(int)(secondLength*Math.cos(second*2*Math.PI/60.0));
int minuteX=center.x+(int)(minuteLength*Math.sin(minute*2*Math.PI/60.0));
int minuteY=center.y-(int)(minuteLength*Math.cos(minute*2*Math.PI/60.0));
int hourX=center.x+(int)(hourLength*Math.sin((minute/60.0+hour)*Math.PI/6.0));
int hourY=center.y-(int)(hourLength*Math.cos((minute/60.0+hour)*Math.PI/6.0));
Graphics2D g2d=(Graphics2D)g;
//画表盘和刻度
g.drawOval(center.x-radius,center.y-radius,diameter,diameter);
for(int i=0;i<60;i++){
int x2=center.x+(int)(radius*Math.sin(i*2*Math.PI/60.0));
int y2=center.y-(int)(radius*Math.cos(i*2*Math.PI/60.0));
if(i%5==0){
int x1=center.x+(int)((secondLength+1)*Math.sin(i*2*Math.PI/60.0));
int y1=center.y-(int)((secondLength+1)*Math.cos(i*2*Math.PI/60));
g2d.setStroke(new BasicStroke(2.5f));
g2d.drawLine(x1,y1,x2,y2);
}
else{
int x1=center.x+(int)((secondLength+10)*Math.sin(i*2*Math.PI/60.0));
int y1=center.y-(int)((secondLength+10)*Math.cos(i*2*Math.PI/60));
g2d.setStroke(new BasicStroke(0.8f));
g2d.drawLine(x1,y1,x2,y2);
}
}
//画时针,分针,秒针

g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(3.0f));
g2d.drawLine(center.x,center.y,hourX,hourY);
g2d.setColor(Color.BLUE);
g2d.setStroke(new BasicStroke(1.5f));
g2d.drawLine(center.x,center.y,minuteX,minuteY);
g2d.setColor(Color.MAGENTA);
g2d.setStroke(new BasicStroke(1.0f));
g2d.drawLine(center.x,center.y,secondX,secondY);



}
public static void main(String args[]){
JFrame jf=new JFrame("时钟");
jf.getContentPane().add(new Clock(),BorderLayout.CENTER);
jf.setBounds(300,300,300,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

----------------解决方案--------------------------------------------------------
画指针转动的 图 可以 用
Graphics2D 类的rotate( )方法

boolean move=true;//其他方法控制

g2d.translate( 200, 200 );
for(;move;)
{
g2d.rotate( 2*Math.PI/60.0 );
//加清楚上条线的函数
g2d.draw( new Line2D.Double( 0, 0,60,0 );
}
----------------解决方案--------------------------------------------------------
很佩服大家写的出这么好的程序啊
----------------解决方案--------------------------------------------------------
很感谢大家的帮助,,真心谢谢了!!JAVA是个好东西!值得我们这样努力
----------------解决方案--------------------------------------------------------
也希望你越学越好
----------------解决方案--------------------------------------------------------
楼主能学到东西就是最好的结局.
----------------解决方案--------------------------------------------------------
  相关解决方案