正在做闹钟的程序,用Graphics2D画了三个线段分别做为闹钟的时,分,秒钟。
但是当这个线段的位置改变——围绕一个圆心在转时,(时,分,秒钟在走动)线段不是直的了,是一个折线了。
有什么方法解决。
把代码贴出来啊!不看看 怎么知道你哪里有问题呢!~
import javax.swing.JPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
public class NewJPanel2 extends JPanel implements ActionListener
{
public static final int SlineLen=115;
public static final int MlineLen=SlineLen-20;
public static final int HlineLen=MlineLen-20;
public static final int P=SlineLen;
public static final GetRunningPoint grp=GetRunningPoint.GetInstanceRunningPoint();//r
private static final GetCurrentTime gct=new GetCurrentTime();
private NewPoint[] TimePoint =gct.GetTimePoint();
private Line2D.Double Hline=new Line2D.Double(P,P,TimePoint[0].x,TimePoint[0].y);//r
private Line2D.Double Mline=new Line2D.Double(P,P,TimePoint[1].x,TimePoint[1].y);//r
private Line2D.Double Sline=new Line2D.Double(P,P,TimePoint[2].x,TimePoint[2].y);//r
private BasicStroke Hpen=new BasicStroke(4.0f);
private BasicStroke Mpen=new BasicStroke(2.0f);
private BasicStroke Spen=new BasicStroke();
private RingTime rt=new RingTime(9,38,0);//r
private Thread bs=new Thread(new BellSong());//r
private boolean Running=true;
private int[] ABL=gct.GetBL();//r
private TimeDialog timedialog[]=new TimeDialog[2];
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("设置闹铃时间"))
{
timedialog[0]=new TimeDialog("设置闹铃时间");
}
if(e.getActionCommand().equals("设置闹铃时间确定"))
{
String time[]=new String[3];
TextField[] a=timedialog[0].getTextField();
for(int i=0;i<3;i++)
time[i]=a[i].getText();
TimePoint=gct.GetTimePoint(time);
}
if(e.getActionCommand().equals("设置闹钟当前时间"))
{
timedialog[1]=new TimeDialog("设置闹钟当前时间");
}
if(e.getActionCommand().equals("设置闹钟当前时间确定"))
{ Running=false;//跳出死循环。
String time[]=new String[3];
TextField[] a=timedialog[1].getTextField();
for(int i=0;i<3;i++)
time[i]=a[i].getText();
TimePoint=gct.GetTimePoint(time);
Hline.setLine(P,P,TimePoint[0].x,TimePoint[0].y);
Mline.setLine(P,P,TimePoint[1].x,TimePoint[1].y);
Sline.setLine(P,P,TimePoint[2].x,TimePoint[2].y);
ABL=gct.GetBL();
this.repaint();
for(int i=0;i<3;i++)
System.out.println(ABL[i]);
Running=true;//请看这里的死循环,我没调用函数TimeRuning(),程序自动调用。问题在这里。
}
}
public void paint(Graphics g)
{ super.paint(g);
Graphics2D g2=(Graphics2D)g;
initTime(g2);
}
public void initTime(Graphics2D g2)
{
g2.setStroke(Hpen);
g2.draw(Hline);
g2.setStroke(Mpen);
g2.draw(Mline);
g2.setStroke(Spen);
g2.draw(Sline);
}
public void TimeRuning()
{
int x=0;int y=0;int z=0;
z=ABL[0]; y=ABL[1];x=ABL[2];
while(Running)
{
Hline.setLine(SlineLen,SlineLen,grp.HPoint[x].x,grp.HPoint[x].y);
Mline.setLine(SlineLen,SlineLen,grp.MPoint[y].x,grp.MPoint[y].y);
Sline.setLine(SlineLen,SlineLen,grp.SPoint[z].x,grp.SPoint[z].y);
this.repaint();
if (z == rt.ProcessRingTime()[0] && y == rt.ProcessRingTime()[1]
&& x == rt.ProcessRingTime()[2])
{
System.out.println("ColokingRing");
bs.start();
}
if(Running==false)
{
System.out.println("RunningFalse");
break;
}
try
{
Thread.sleep(1000);
}
catch (Exception e) {}
x++;
y++;
z++;
if(x==grp.HPoint.length)
x=0;
if(y==grp.MPoint.length)
y=0;
if(z==grp.SPoint.length)
z=0;
}
System.out.println("break2");
}
public void init()
{
setSize(2*SlineLen,2*SlineLen);
setLocation(Test.JF/2-SlineLen,Test.JF/2-SlineLen);
this.setBackground(Color.white);
setVisible(true);
}
public NewJPanel2()
{
init();
}
}
程序太大,无法上传。只有上传关键部分。