Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/applets/Clock/Clock.java
38829 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.applet.Applet;42import java.awt.Color;43import java.awt.Font;44import java.awt.Graphics;45import java.text.SimpleDateFormat;46import java.util.Date;47import java.util.Locale;484950/**51* Time!52*53* @author Rachel Gollub54* @author Daniel Peek replaced circle drawing calculation, few more changes55*/56@SuppressWarnings("serial")57public class Clock extends Applet implements Runnable {5859private volatile Thread timer; // The thread that displays clock60private int lastxs, lastys, lastxm,61lastym, lastxh, lastyh; // Dimensions used to draw hands62private SimpleDateFormat formatter; // Formats the date displayed63private String lastdate; // String to hold date displayed64private Font clockFaceFont; // Font for number display on clock65private Date currentDate; // Used to get date to display66private Color handColor; // Color of main hands and dial67private Color numberColor; // Color of second hand and numbers68private int xcenter = 80, ycenter = 55; // Center position6970@Override71public void init() {72lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;73formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy",74Locale.getDefault());75currentDate = new Date();76lastdate = formatter.format(currentDate);77clockFaceFont = new Font("Serif", Font.PLAIN, 14);78handColor = Color.blue;79numberColor = Color.darkGray;8081try {82setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),8316)));84} catch (NullPointerException e) {85} catch (NumberFormatException e) {86}87try {88handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),8916));90} catch (NullPointerException e) {91} catch (NumberFormatException e) {92}93try {94numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),9516));96} catch (NullPointerException e) {97} catch (NumberFormatException e) {98}99resize(300, 300); // Set clock window size100}101102/**103* Paint is the main part of the program104*/105@Override106public void update(Graphics g) {107int xh, yh, xm, ym, xs, ys;108int s = 0, m = 10, h = 10;109String today;110111currentDate = new Date();112113formatter.applyPattern("s");114try {115s = Integer.parseInt(formatter.format(currentDate));116} catch (NumberFormatException n) {117s = 0;118}119formatter.applyPattern("m");120try {121m = Integer.parseInt(formatter.format(currentDate));122} catch (NumberFormatException n) {123m = 10;124}125formatter.applyPattern("h");126try {127h = Integer.parseInt(formatter.format(currentDate));128} catch (NumberFormatException n) {129h = 10;130}131132// Set position of the ends of the hands133xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);134ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);135xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);136ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);137xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2)138* 30139+ xcenter);140yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2)141* 30142+ ycenter);143144// Get the date to print at the bottom145formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");146today = formatter.format(currentDate);147148g.setFont(clockFaceFont);149// Erase if necessary150g.setColor(getBackground());151if (xs != lastxs || ys != lastys) {152g.drawLine(xcenter, ycenter, lastxs, lastys);153g.drawString(lastdate, 5, 125);154}155if (xm != lastxm || ym != lastym) {156g.drawLine(xcenter, ycenter - 1, lastxm, lastym);157g.drawLine(xcenter - 1, ycenter, lastxm, lastym);158}159if (xh != lastxh || yh != lastyh) {160g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);161g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);162}163164// Draw date and hands165g.setColor(numberColor);166g.drawString(today, 5, 125);167g.drawLine(xcenter, ycenter, xs, ys);168g.setColor(handColor);169g.drawLine(xcenter, ycenter - 1, xm, ym);170g.drawLine(xcenter - 1, ycenter, xm, ym);171g.drawLine(xcenter, ycenter - 1, xh, yh);172g.drawLine(xcenter - 1, ycenter, xh, yh);173lastxs = xs;174lastys = ys;175lastxm = xm;176lastym = ym;177lastxh = xh;178lastyh = yh;179lastdate = today;180currentDate = null;181}182183@Override184public void paint(Graphics g) {185g.setFont(clockFaceFont);186// Draw the circle and numbers187g.setColor(handColor);188g.drawArc(xcenter - 50, ycenter - 50, 100, 100, 0, 360);189g.setColor(numberColor);190g.drawString("9", xcenter - 45, ycenter + 3);191g.drawString("3", xcenter + 40, ycenter + 3);192g.drawString("12", xcenter - 5, ycenter - 37);193g.drawString("6", xcenter - 3, ycenter + 45);194195// Draw date and hands196g.setColor(numberColor);197g.drawString(lastdate, 5, 125);198g.drawLine(xcenter, ycenter, lastxs, lastys);199g.setColor(handColor);200g.drawLine(xcenter, ycenter - 1, lastxm, lastym);201g.drawLine(xcenter - 1, ycenter, lastxm, lastym);202g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);203g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);204}205206@Override207public void start() {208timer = new Thread(this);209timer.start();210}211212@Override213public void stop() {214timer = null;215}216217@Override218@SuppressWarnings("SleepWhileHoldingLock")219public void run() {220Thread me = Thread.currentThread();221while (timer == me) {222try {223Thread.sleep(100);224} catch (InterruptedException e) {225}226repaint();227}228}229230@Override231public String getAppletInfo() {232return "Title: A Clock \n"233+ "Author: Rachel Gollub, 1995 \n"234+ "An analog clock.";235}236237@Override238public String[][] getParameterInfo() {239String[][] info = {240{ "bgcolor", "hexadecimal RGB number",241"The background color. Default is the color of your browser." },242{ "fgcolor1", "hexadecimal RGB number",243"The color of the hands and dial. Default is blue." },244{ "fgcolor2", "hexadecimal RGB number",245"The color of the second hand and numbers. Default is dark gray." }246};247return info;248}249}250251252