質問

I'm working on a Java/Android program that takes a number input (for example, hour = 7 and minutes = 30 for 7:30AM), and then expresses it on an Analog clock. (hour hand pointing to 7 and minute hand pointing to 30 minutes)

How can this be done? Would I need a lot of image files? I have no idea how to start ... thank you so much.

Here's some code below that I wrote for Android on my Results Display page.

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ResultPage extends Activity {
    public static int hourMark;
    public static int hourMarkPM;
    public static int minuteMark;
    public static int time;
    public static String paddingMinute;   
    public static int age;
    public static String lifeClockDisplay;

    public static void calculation(int a){
        time = a * 18;
        minuteMark = (a * 18) % 60;
        hourMark = (time - minuteMark)/60;
        hourMarkPM = hourMark - 12;
        if (minuteMark < 10){
            paddingMinute = "0" + String.valueOf(minuteMark);
        }
        else{
            paddingMinute = String.valueOf(minuteMark);
        }
    }
    public static void resultInputCalc() 
    {   //AM
        if (hourMark >= 0 && hourMark < 12)
        {
            if (hourMark == 0)
            {
                lifeClockDisplay = "12:" + paddingMinute + "AM";
            }
            else 
            {
                lifeClockDisplay = hourMark + ":" + paddingMinute + "AM" ;
            }
        }
        //PM
        if (hourMark >= 12 && hourMark < 24)
        {
            lifeClockDisplay = hourMarkPM + ":" + paddingMinute + "PM" ;
        }
        //Midnight
        if (hourMark == 24)
        {
            lifeClockDisplay = hourMarkPM + ":" + paddingMinute + "AM" ;
        }
    }
    public static String comments() {
        if (hourMark == 0)
        {
        return "Are you a baby? Did you open this app by accident? ;) ";
        }
         // Very early morning
        else if (hourMark > 0 && hourMark < 6)
        { 
        return "Everyone else is pretty much sleeping."
                + "Utilize this time well by reading and studying! No need to be in any hurry.";
        }
         // Early morning 
        else if (hourMark >= 6 && hourMark < 9)
        {
          return "Morning has just started. It's a good time to plan out the rest of the day."
                + "You have your whole day ahead of you. No need to be in a hurry. Just wake up and do your thing.";
        }
         // Late morning
        else if (hourMark >= 9 && hourMark < 12)
        {
        return "It's still morning. You have time to plan out your afternoon and night. You have a plenty of time left. ";
        }
         // Noon
        else if (hourMark == 12)
        {
          return "It's lunch time. When you look back at the morning, what did you learn? How can you make afternoon and night better?";
        }
         // Afternoon
        else if (hourMark > 12 && hourMark <= 18) 
        {
        return "This is time that everyone else is probably working. What will you choose to work on? How will you plan out your night?";
        }
         // Night
        else if (hourMark > 18 && hourMark < 24){
        return "How do you want to spend the rest of the night? Aren't there still many things you want to do?";
        }
         // Midnight
        else {
        return "How do you want to spend the rest of the night? Aren't there still many things you want to do?";
        }    
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);

        TextView resultIntroView = (TextView) findViewById(R.id.result_intro_text);
        resultIntroView.setText("Your current age is " + getIntent().getExtras().getString("theAge") + " years-old and " + "your life clock is pointing to ...");

        TextView intTestView = (TextView) findViewById(R.id.clock_display_view);
        age = Integer.parseInt(getIntent().getExtras().getString("theAge")); //here is the user-input for age saved
        calculation(age);
        resultInputCalc();
        intTestView.setText(lifeClockDisplay);      
        TextView commentsView = (TextView) findViewById(R.id.result_comments);
        commentsView.setText(comments());

    }

}
役に立ちましたか?

解決

This is a bit broad. Have you tried anything already?

You don't need image files, since you can use Java's JPanel paintComponent() to take care of the graphics.

Here is how I would implement the minute hand, from here you can work out the hour hand by yourself. No code, you can find everything you need here http://docs.oracle.com/javase/7/docs/api/index.html

  • 1 hour is made of 60 minutes, hence I will find out how much angle the hand should span per minute: spanPerMinute = 360 / 60 = 6
  • The amount of degrees spanned by the hand will then be minuteHandSpan = inputMinutes * spanPerMinute
  • Java graphics have the 0° placed at the 9 o'clock position, so my effective graphic span would be javaMinuteHandSpan = minuteHandSpan + 90

Same goes for the hour hand, and you can add some math so that it moves partially between hours depending on how much of that hour has passed (e.g. hand is between 1 and 2 at 1:30PM).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top