Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы JavaClock.java

Оглавление

Назад

// =======================================
// JavaClock.java
//
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov
// =======================================
import java.applet.Applet;
import java.awt.*;
import java.util.*;

public class JavaClock extends Applet
{
  ClockUpdate m_ClockUpdate = null;

  public String getAppletInfo()
  {
    return "Name: JavaClock";
  }

  public void paint(Graphics g)
  {
    int yStart = 20;
    int yCurrent = yStart;
    String szDate;
    
    FontMetrics fm = g.getFontMetrics();
    
    Date dt = new Date();
    
    g.setColor(Color.red);
    g.drawString("toString()", 10, yCurrent);
    yCurrent += fm.getHeight();
    g.setColor(Color.black);

    szDate = dt.toString();
    g.drawString(szDate, 10, yCurrent);
    yCurrent += fm.getHeight();
    yCurrent += fm.getHeight();
    
    g.setColor(Color.red);
    g.drawString("toLocaleString()",
     10, yCurrent);
    yCurrent += fm.getHeight();
    g.setColor(Color.black);
    
    szDate = dt.toLocaleString();
    g.drawString(szDate, 10, yCurrent);
    yCurrent += fm.getHeight();
    yCurrent += fm.getHeight();
    
    g.setColor(Color.red);
    g.drawString("getTimezoneOffset()",
       10, yCurrent);
    yCurrent += fm.getHeight();
    g.setColor(Color.black);
    
    int off = dt.getTimezoneOffset();
    szDate = "Time offset, minutes: " + off;
    g.drawString(szDate, 10, yCurrent);
    yCurrent += fm.getHeight();
    yCurrent += fm.getHeight();
    
    g.setColor(Color.red);
    g.drawString("Custom", 10, yCurrent);
    yCurrent += fm.getHeight();
    g.setColor(Color.black);

    szDate = "Year: " + dt.getYear() +
      ", Month: " + dt.getMonth() +
      ", Date: " + dt.getDate() +
      ", Day: " + dt.getDay();
    g.drawString(szDate, 10, yCurrent);
    yCurrent += fm.getHeight();
    
    szDate = "Hours: " + dt.getHours() +
      ", Minutes: " + dt.getMinutes() +
      ", Seconds: " + dt.getSeconds();
    g.drawString(szDate, 10, yCurrent);
    yCurrent += fm.getHeight();
    
    yCurrent = yStart;
  }

  public void start()
  {
    if (m_ClockUpdate == null)
    {
      m_ClockUpdate = new ClockUpdate(this);
      m_ClockUpdate.start();
    }
  }
	
  public void stop()
  {
    if (m_ClockUpdate != null)
    {
      m_ClockUpdate.stop();
      m_ClockUpdate = null;
    }
  }
}

class ClockUpdate extends Thread
{
  Applet App;

  public ClockUpdate(Applet Appl)
  {
    App = Appl;
  }

  public void run()
  {
    while (true)
    {
      App.repaint();
      
      try
      {
	Thread.sleep(1000);
      }
      catch (InterruptedException e)
      {
	stop();
      }
    }
  }
}

Назад

[Назад]