使用Java的时钟程序(JApplet的) [英] Clock program using Java (JApplet)
问题描述
我完全难倒的地方,我和我的时钟程序走错了。
我的程序应该接受小时和分钟用户输入。然后,用户的输入将在时针和分针设置为在弹出窗口中抽出圆形时钟的适当位置。
我的程序似乎并没有采取用户输入,而是它的输入需要从paint方法在 ClockApplet.java
。我不太知道如何让我的程序采取来自用户的输入来代替。任何意见/输入/帮助将不胜AP preciated!
我的code是如下:
Clock.java
进口java.awt.Graphics;
进口java.awt.Graphics2D中;
进口java.awt.geom.Ellipse2D;
进口java.awt.geom.Line2D中;
进口java.awt.geom.Point2D中;公共类时钟
{
公共时钟()
{
this.anHour = 12;
this.aMin = 0;
}公共时钟(双小时,双分)
{
this.anHour =小时;
this.aMin =分钟;
}公共无效抽奖(图形G)
{
Graphics2D的G2 =(Graphics2D的)克; Ellipse2D.Double clock_face =新Ellipse2D.Double(0,0,2 * Radius,
2 * RADIUS);
((Graphics2D的)G).draw(clock_face); Point2D.Double中心=新Point2D.Double(RADIUS,RADIUS);
双角= Math.PI / 2 - 2 * Math.PI *阿明/ MINUTES_PER_HOUR;
Point2D.Double minutePoint =新Point2D.Double(RADIUS + MINUTE_HAND
* Math.cos(角度),RADIUS - MINUTE_HAND * Math.sin(角度));
Line2D.Double minuteHand =新Line2D.Double(中心,minutePoint);
((Graphics2D的)G).draw(minuteHand); 角= Math.PI / 2 - 2 * Math.PI *(* anHour + MINUTES_PER_HOUR
阿明)/(MINUTES_PER_HOUR * HOURS_PER_DAY);
Point2D.Double hourPoint =新Point2D.Double(RADIUS + HOUR_HAND *
Math.cos(角度),RADIUS - HOUR_HAND * Math.sin(角度));
Line2D.Double hourHand =新Line2D.Double(中心,hourPoint);
((Graphics2D的)G).draw(hourHand); 时钟时钟1 =新的时钟(anHour,阿明);
}私人双anHour;
私人双阿明;最终双半径= 100;
最终双MINUTES_PER_HOUR = 60;
最终双HOURS_PER_DAY = 12;
最终双HOUR_HAND = 75;
最终双MINUTE_HAND = 90;
}
ClockApplet.java
进口java.awt.Graphics;
进口java.awt.Graphics2D中;进口javax.swing.JApplet中;
进口javax.swing.JOptionPane中;/ **
*此applet绘制时钟的形状。
* /公共类ClockApplet扩展JApplet的
{
公共ClockApplet()
{
字符串输入; 输入= JOptionPane.showInputDialog(NULL,请输入时间(HH:MM):,时钟小程序,JOptionPane.QUESTION_MESSAGE); 如果((输入== NULL)||(input.equals()))
{
JOptionPane.showMessageDialog(NULL,无效的输入,警报,JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
其他
{
INT colon1 = input.indexOf(:);
INT colon2 = input.lastIndexOf(:); 双小时= findHour(colon1,输入);
双分钟= findMin(colon2,输入); 如果(时间= 0&安培;!&安培;!分钟= 0)
{
时钟时钟1 =新的时钟(小时,分钟);
}
其他
{
JOptionPane.showMessageDialog(NULL,无效的输入,警报,JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
}公共双findHour(INT colon1,字符串输入)
{
串userHour = input.substring(colon1 + 1);
双AHOUR = Double.parseDouble(userHour);
双temp1中= 0.0; 如果(AHOUR大于0&放大器;&放大器; AHOUR&下; = 12)
{
temp1中= AHOUR;
}
其他
{
返回0;
} 返回temp1中;
}公共双findMin(INT colon2,字符串输入)
{
串userMin = input.substring(colon2 + 1);
双阿明= Double.parseDouble(userMin);
双temp1中= 0.0; 如果(AMIN大于0&放大器;&放大器; AMIN&下; = 60)
{
temp1中=阿明;
}
其他
{
返回0;
} 返回temp1中;
}公共无效漆(图形G)
{
Graphics2D的G2 =(Graphics2D的)克; 时钟时钟1 =新的时钟(10,10); clock1.draw(G2);}}
ClockApplet.html
<小程序code =ClockApplet.classWIDTH =50HEIGHT =50> < /小程序>
问题就出在一小时内的解析。如果时间被指定为 05:15
的提示,它得到了错误的字符串。 do语句是这样的:
的System.out.println(/ *对象我现在有* /);
..因为它通过了 findHour()
的方法,来检查发生了什么。另外,使用调试器。
I am completely stumped as to where I am going wrong with my clock program.
My program is supposed to accept user input for hours and minutes. The user's input would then set the hour-hand and the minute-hand to its appropriate position on the drawn out circular clock in a pop-up window.
My program doesn't seem to take in user input but instead it takes in input from the paint method in ClockApplet.java
. I'm not quite sure how to make my program take in input from the user instead. Any advice/input/help would be greatly appreciated!
My code is as follows:
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>
The problem lies in the parsing of the hour. If the time is specified as 05:15
as prompted, it gets the wrong substring. Do statements like this:
System.out.println(/* the object I now have */);
..as it passes through the findHour()
method, to check what is happening. Alternately, use a debugger.
这篇关于使用Java的时钟程序(JApplet的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!