简体   繁体   中英

Set an analog clock with an initial time using Java

For my programming class I'm working on a clock. The clock has to be set at an initial time, which I cannot figure out how to do. The clock I'm currently working with just uses the system time. I've tried setting a time using cal.set but then it just freezes on what I want to be the initial time and time doesn't progress. How can I edit this to allow me to have a set initial time, but have the clock still work? package Clock;

package Clock;

import java.applet.Applet;
import java.awt.Color;
import java.lang.Object;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Component extends Applet implements Runnable {
private static final long serialVersionUID = 1L;

public static String name = "My Clock";

public static int size = 600;

public static boolean isRunning = false;

public static Graphics g;

public static Image screen;

public Numbers number;

public static JFrame frame;

public static void main(String [] args) {
    Component component = new Component();

    frame = new JFrame();
    frame.add(component);
    frame.setSize(size+6, size + 28);
    frame.setTitle(name);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    component.start();
}

public void start() {
    requestFocus();

    number = new Numbers();

    isRunning = true;
    Thread th = new Thread(this);
    th.start();
}

public void run() {
    screen = createVolatileImage(size, size);
    while (isRunning) {
        tick();
        render(g);
        try {
            Thread.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public void tick() {

}

public double time;
public int anim;
public int anim2;
public int anim3;
public int anim4;
public int center = size/2;
public int radius = (size-40)/2;

public void render(Graphics g) {
    // Drawing to image

    screen = createImage(size, size);
    g = screen.getGraphics();

    //Drawing the background

    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(0, 0, size, size);

    // Drawing the frame(outside circle)

    g.setColor(Color.black);
    g.fillOval(5, 5, size - 10, size - 10);
    g.setColor(Color.white);
    //g.setColor(new Color(new Random().nextInt(255). new Random().nextInt(255), new Random().nextInt(255)));
    //g.drawOval(10, 10, size - 20, size - 20);
    g.fillOval(20, 20, size - 40, size - 40);

    number.render(g);

    // Math and Drawing for Lines

    for (int i =  0; i < 60; i++) {
        radius = size - 40;
        anim = center + (int) ((Math.sin(i % 60.0 / 60 * Math.PI * 2) * (radius / 2)));
        anim2 = center - (int) ((Math.cos(i % 60.0 / 60 * Math.PI * 2) * (radius / 2)));
        radius = size - 60;
        anim3 = center + (int) ((Math.sin(i % 60.0 / 60 * Math.PI * 2) * (radius / 2)));
        anim4 = center - (int) ((Math.cos(i % 60.0 / 60 * Math.PI * 2) * (radius / 2)));
        g.drawLine(anim, anim2, anim3, anim4);
    }

    // Math for hour hand

    radius = size - 140;
    // time = System.currentTimeMillis() % 3600000 / 3600000 * Math.PI;
    int t = (int) (System.currentTimeMillis() + 17300000) + 3600000+ 3600000 + 3600000 + 3600000 + 3600000 + 3600000 + 3600000 + 3600000;
    anim = center
            + (int) ((Math.sin(t % 43200000.0
                    / 43200000 * Math.PI * 2) * (radius / 2))) + 7;
    anim2 = center
            - (int) ((Math.cos(t % 43200000.0
                    / 43200000 * Math.PI * 2) * (radius / 2))) + 7;

    // Drawing the hour hand

    g.setColor(Color.black);
    g.fillOval(center - 8, center - 8, 16, 16);
    g.drawLine(center, center, anim, anim2);
    g.drawLine(center + 1, center, anim + 1, anim2);
    g.drawLine(center, center + 1, anim, anim2 + 1);
    g.drawLine(center - 1, center, anim - 1, anim2);
    g.drawLine(center, center - 1, anim, anim2 - 1);
    g.drawLine(center + 1, center + 1, anim, anim2);
    g.drawLine(center + 1, center - 1, anim, anim2);
    g.drawLine(center - 1, center + 1, anim, anim2);
    g.drawLine(center - 1, center - 1, anim, anim2);


    // Math for minute hand

    radius = size - 90;
    // time = System.currentTimeMillis() % 3600000 / 3600000 * Math.PI;
    anim = center
            + (int) ((Math.sin(System.currentTimeMillis() % 3600000.0
                    / 3600000 * Math.PI * 2) * radius / 2));
    anim2 = center
            - (int) ((Math.cos(System.currentTimeMillis() % 3600000.0
                    / 3600000 * Math.PI * 2) * radius / 2));

    // Drawing the minute hand
    g.setColor(Color.black);
    g.drawLine(center, center, anim, anim2);
    g.drawLine(center + 1, center, anim + 1, anim2);
    g.drawLine(center, center + 1, anim, anim2 + 1);
    g.drawLine(center - 1, center, anim - 1, anim2);
    g.drawLine(center, center - 1, anim, anim2 - 1);

    //Math for second hand

    DateFormat dateFormat = new SimpleDateFormat("ss");
    Calendar cal = Calendar.getInstance();
    String s = dateFormat.format(cal.getTime());

    radius = size - 70;
    // time = System.currentTimeMillis() % 60000 / 60000 * Math.PI;
    anim = center
            + (int) ((Math.sin(Integer.parseInt(s) % 60.0 / 60 * Math.PI
                    * 2) * (radius / 2)));
    anim2 = center
            - (int) ((Math.cos(Integer.parseInt(s) % 60.0 / 60 * Math.PI
                    * 2) * (radius / 2)));

    // Drawing the second hand

    g.setColor(Color.red);
    g.drawLine(center, center, anim, anim2);
    g.drawLine(center + 1, center, anim + 1, anim2);
    g.drawLine(center, center + 1, anim, anim2 + 1);
    g.drawLine(center - 1, center, anim - 1, anim2);
    g.drawLine(center, center - 1, anim, anim2 - 1);

    // Center circle

    g.fillOval(center - 5, center - 5, 10, 10);
    g.setColor(Color.black);
    g.fillOval(center - 2, center - 2, 4, 4);

    // g.setColor(new Color(new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255)));
    // g.fillRect(0, 0, getWidth(), getHeight());

    // Drawing to screen

    g = getGraphics();
    g.drawImage(screen, 0, 0, size, size, this);
    g.dispose();
}

}

and

package Clock;

import java.applet.Applet;
import java.awt.Color;
import java.lang.Object;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Numbers extends JPanel {
    private static final long serialVersionUID=1L;

    public int size = Component.size;
    public int center = size/2;

    public void setFont(Font font) {
     super.setFont(font);
     repaint();
    }
    public String getDay() {
        DateFormat dateFormat=new SimpleDateFormat("dd");
        Calendar cal = Calendar.getInstance();
        String s = dateFormat.format(cal.getTime());
        int day = Integer.parseInt(s);
        String d = day + "";
            // sets ordinal indicator
            switch(day) {
                   case 1:
                   case 21:
                   case 31:
                   d += "st";
                   break;
                   case 2:
                   case 22:
                   d += "nd";
                   break;
                   case 3:
                   case 23:
                   d += "rd";
                   break;
                   default:
                   d += "th";
            }
            return d;
    }
  public String getHour() {
      DateFormat dateFormat = new SimpleDateFormat("HH");
      Calendar cal = Calendar.getInstance();
      String s = dateFormat.format(cal.getTime());
      int hour = Integer.parseInt(s) -1;

      String n = hour + "";
      if(hour < 10) {
          n=hour+"";
      }
      return n;
  }

  public String getMonth() {
      DateFormat dateFormat = new SimpleDateFormat("MM");
      Calendar cal = Calendar.getInstance();
      String s = dateFormat.format(cal.getTime());
      int month = Integer.parseInt(s);

      // sets month name to number
      String m = "";
      switch (month)
      {
          case 1:
            m= "January";
            break;
          case 2:
            m= "February";
            break;
          case 3:
            m= "March";
            break;
          case 4:
            m= "April";
            break;
          case 5:
            m= "May";
            break;
          case 6:
            m= "June";
            break;
          case 7:
            m= "July";
            break;
          case 8:
            m= "August";
            break;
          case 9:
            m= "September";
            break;
          case 10:
            m= "October";
            break;
          case 11:
            m= "November";
            break;
          case 12:
            m= "December";
      }
      return m;
  }

  public void render(Graphics g) {
      g.setColor(Color.black);

      DateFormat dateFormat = new SimpleDateFormat(":mm:ss");
      Calendar cal = Calendar.getInstance();
      String s = dateFormat.format(cal.getTime());
      int n = center - ((s.length() *13)/2);
      //265
      g.setFont(new Font("Arial", 1, 20));
      s = (Integer.parseInt(getHour(), 10) % 12 + 1) + "" + dateFormat.format(cal.getTime());
      n = center - (s.length() * 10 / 2);
      g.setColor(Color.DARK_GRAY);
      g.fillRoundRect(250, 348, 100, 30, 6, 6);
      g.setColor(Color.LIGHT_GRAY);
      g.fillRoundRect(252, 350, 96, 26, 6, 6);
      g.setColor(Color.BLACK);
      g.drawString("TIME", 275, 345);
      g.drawString("DATE", 275, 225);
      g.drawString("AM", 255, 150);
      g.drawString("PM", 315, 150);
      g.drawString(s, n, 370);
      int p = Integer.parseInt(getHour(), 10);
      if(p < 11 || p == 24) {
          g.fillOval(265, 160, 10, 10);
          g.drawOval(325, 160, 10, 10);
      } else {
          g.drawOval(265, 160, 10, 10);
          g.fillOval(325, 160, 10, 10);
      }

      dateFormat = new SimpleDateFormat("yyyy");
      cal = Calendar.getInstance();
      s = getMonth() + " " + getDay() + ", " + dateFormat.format(cal.getTime());
      n = center - (int) ((s.length() * 10.25) / 2);

      g.setColor(Color.DARK_GRAY);
      g.fillRoundRect(200, 228, 200, 30, 6, 6);
      g.setColor(Color.LIGHT_GRAY);
      g.fillRoundRect(202, 230, 196, 26, 6, 6);
      g.setColor(Color.BLACK);
      g.drawString(s, n, 250);
      s = Component.name;
      n=center - (int)((s.length()*10)/2);
      g.drawString(s, n , 450);

      g.setFont(new Font("Arial", 1, 30));
      int radius = size - 100;
      for(int i = 0; i < 12; i++) {
          double anim = (int) ((Math.sin((i+1) % 12.0 / 12 * Math.PI * 2) * (radius / 2)));
          double anim2 = (int) ((Math.cos((i+1) % 12.0 / 12 * Math.PI * 2) * (radius / 2)));
          if(i >= 9){
              anim -= 10;
          }
          g.drawString((i+1) + "", center + (int) anim - 6, center - (int) anim2 + 12);
        }
    }
}

Lot's of issues -- where to even begin?

  • You've named a class Component , a name that will easily cause conflicts with a key Java core GUI class, java.awt.Component. Rename it to something else.
  • This same class extends Applet but is not being used as an Applet. Rather it is being used as a component to be added to a JFrame -- this makes no sense, trying to add one top-level window into another, and would require some justification as to why you're doing it in such a strange fasion. Why not extend JPanel or JComponent something else that makes more sense?
  • You're using a Graphics field and drawing to it, something that risks NullPointerException. Instead Google and read the Swing drawing tutorials and follow their lead -- draw passively within the paintComponent method of a JPanel. There are other important details to understand which the tutorials will show and tell you.
  • You're drawing with a Graphics object obtained by calling getGraphics() on a component, something that will lead to unstable drawings and possible NullPointerExceptions. Again read the tutorials on how to do this correctly.
  • You're making Swing calls in a background thread, something that can lead to intermittent difficult to debug threading errors. Use a Swing Timer to make things easier on yourself. Google the tutorial for the gory details.
  • You appear to be creating a JPanel, Numbers, but aren't adding it to the GUI (that I can tell), but rather are trying to render it in a strange way -- why, I have no idea. Don't do this. Again do graphics as per the Swing drawing tutorials. Here's the links:
  • ..... more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM