使用Java的时钟程序(JApplet)

时间:2012-06-04 04:53:10

标签: java applet user-input clock

我完全不知道我的时钟程序出错了。

我的程序应该接受用户输入的小时和分钟。然后,用户的输入将时针和分针设置在弹出窗口中绘制的圆形时钟上的适当位置。

我的程序似乎没有接受用户输入,而是接受来自ClockApplet.java中的paint方法的输入。我不太确定如何让我的程序接受用户的输入。任何建议/意见/帮助将不胜感激!

我的代码如下:

Clock.java

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Clock
{
public Clock()
{
    this.anHour=12;
    this.aMin=0;
}

public Clock(double hr, double min)
{
    this.anHour=hr;
    this.aMin=min;
}

public void draw(Graphics g)
{
    Graphics2D g2=(Graphics2D)g;

    Ellipse2D.Double clock_face = new Ellipse2D.Double(0, 0, 2 * RADIUS, 
                                    2 * RADIUS);
    ((Graphics2D) g).draw(clock_face);

    Point2D.Double center=new Point2D.Double(RADIUS, RADIUS);
    double angle=Math.PI /2 - 2 * Math.PI * aMin / MINUTES_PER_HOUR;
    Point2D.Double minutePoint=new Point2D.Double(RADIUS + MINUTE_HAND
                                * Math.cos(angle), RADIUS - MINUTE_HAND * Math.sin(angle));
    Line2D.Double minuteHand=new Line2D.Double(center, minutePoint);
    ((Graphics2D) g).draw(minuteHand);

    angle=Math.PI / 2 - 2 * Math.PI * (anHour * MINUTES_PER_HOUR +
            aMin) / (MINUTES_PER_HOUR * HOURS_PER_DAY);
    Point2D.Double hourPoint=new Point2D.Double(RADIUS + HOUR_HAND *
                                Math.cos(angle), RADIUS - HOUR_HAND * Math.sin(angle));
    Line2D.Double hourHand=new Line2D.Double(center, hourPoint);
    ((Graphics2D) g).draw(hourHand);

    Clock clock1=new Clock(anHour, aMin);
}

private double anHour;
private double aMin;

final double RADIUS = 100;
final double MINUTES_PER_HOUR = 60;
final double HOURS_PER_DAY = 12;
final double HOUR_HAND = 75;
final double MINUTE_HAND = 90;
}

ClockApplet.java

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

/**
 * This applet draws the clock shape.
 */

public class ClockApplet extends JApplet
{
    public ClockApplet()
    {
        String input;

    input=JOptionPane.showInputDialog(null, "Please enter the time (hh:mm):","Clock Applet", JOptionPane.QUESTION_MESSAGE);

    if((input==null) || (input.equals("")))
    {
        JOptionPane.showMessageDialog(null, "Invalid input", "Alert", JOptionPane.WARNING_MESSAGE);
        System.exit(0);
    }
    else
    {
        int colon1=input.indexOf(":");
        int colon2=input.lastIndexOf(":");

        double hours=findHour(colon1, input);
        double minutes=findMin(colon2, input);

        if(hours!=0 && minutes!=0)
        {
            Clock clock1=new Clock(hours, minutes);
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Invalid input", "Alert", JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        }
    }
}

public double findHour(int colon1, String input)
{
    String userHour=input.substring(colon1+1);
    double aHour=Double.parseDouble(userHour);
    double temp1=0.0;

    if(aHour>0 && aHour<=12)
    {
        temp1=aHour;
    }
    else
    {
        return 0;
    }

    return temp1;
}

public double findMin(int colon2, String input)
{
    String userMin=input.substring(colon2+1);
    double aMin=Double.parseDouble(userMin);
    double temp1=0.0;

    if(aMin>0 && aMin<=60)
    {
        temp1=aMin;
    }
    else
    {
        return 0;
    }

    return temp1;
}

public void paint(Graphics g)
{
    Graphics2D g2=(Graphics2D)g;

    Clock clock1=new Clock(10,10);

    clock1.draw(g2);

}

}

ClockApplet.html

<applet code="ClockApplet.class" width="50" height="50"> </applet>

2 个答案:

答案 0 :(得分:1)

您需要将接受用户输入的代码添加到life cycle的{​​{1}}方法中。所以将代码添加到applet的init()方法中,但我认为这里缺少。所以oberride init()方法并添加代码:

applet

答案 1 :(得分:1)

问题在于解析小时。如果时间根据提示指定为05:15,则会得到错误的子字符串。做这样的陈述:

System.out.println(/* the object I now have */);

..当它通过findHour()方法时,检查发生了什么。或者,使用调试器。