宿題スレPart55 60 <24時間 時計>


宿題スレPart55 >>60  <24時間 時計>

	//
	//FullDayClock.java
	//<applet code = "FullDayClock.class" width=400 height =400>
	//
	import java.applet.Applet;
	import java.awt.Button;
	import java.awt.Color;
	import java.awt.Container;
	import java.awt.FlowLayout;
	import java.awt.Graphics;
	import java.awt.GridLayout;
	import java.awt.Image;
	import java.awt.Label;
	import java.awt.Point;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import java.awt.event.MouseEvent;
	import java.awt.event.MouseMotionListener;
	import java.text.DecimalFormat;
	import java.text.SimpleDateFormat;
	import java.util.Calendar;
	import java.util.Date;
	import java.util.GregorianCalendar;
	
	import javax.swing.JFrame;
	import javax.swing.JPanel;
	
	public class FullDayClock extends Applet implements Runnable, ActionListener, MouseMotionListener {
	    Timer timer = new Timer(80, Color.YELLOW);
	    Hour hour = new Hour(100, Color.BLACK);
	    Minute min = new Minute(150, Color.BLUE);
	    Second sec = new Second(160, Color.RED);
	    ClockHand[] hands = { timer, hour, min, sec };
	    
	    int dt = 1000;
	    Thread th;
	    
	    Button b1 = new Button("中断");
	    Button b2 = new Button("再開");
	    DecimalFormat df = new DecimalFormat("00");   
	    SimpleDateFormat sf = new SimpleDateFormat("H:mm:ss");
	    Label lbl  = new Label("Timer:");
	    Label lblTimer = new Label();
	    Label lblCurrentTime = new Label("            ");
	    boolean pause = false;
	    Graphics og;
	    Image img;
	    
	    public void init() {
	        add(lblCurrentTime);
	        add(b1);
	        add(b2);
	        add(lbl);
	        add(lblTimer);
	        b1.addActionListener(this);
	        b2.addActionListener(this);
	        addMouseMotionListener(this);
	        timer.setTime(12,0);
	        lblTimer.setText(df.format(timer.getHour()) + ":" + df.format(timer.getMinute()));
	    }
	
	    public void start() {
	        th = new Thread(this);
	        th.start();
	    }
	
	    public void run() {
	        while (!pause) {
	            repaint();
	            lblCurrentTime.setText(sf.format(hour.getCalendar().getTime()));
	            timer.checkAlarmTime();
	            try {
	                Thread.sleep(dt);
	            } catch (InterruptedException e) {
	            }
	        }
	    }
	
	    	
	    public void actionPerformed(ActionEvent e) {
	        Object o = e.getSource();
	        if (o.equals(b1))
	            pause = true;
	        else if (o.equals(b2)) {
	            pause = false;
	            start();
	        } 
	    }
	
	    public void paint(Graphics g) {
	        if(og ==null){
	            img = createImage(getWidth(),getHeight());
	            og=img.getGraphics();
	        }
	        og.setColor(Color.WHITE);
	        og.fillRect(0, 0, getWidth(), getHeight());
	
	        og.setColor(Color.BLACK);
	        drawMarker(og, 24, 170, 180);
	        drawMarker(og, 60, 165, 168);
	        drawMarker(og, 144, 84, 84);
	        
	        for (int i = 0; i < hands.length; i++) {
	            og.setColor(hands[i].getColor());
	            og.drawLine(getWidth() / 2, getHeight() / 2, 
	                    getWidth() / 2 + (int) (hands[i].getPoint().getX()), 
	                    getHeight() / 2 + (int) (hands[i].getPoint().getY()));
	        }
	        
	        g.drawImage(img,0,0,this);
	    }
	    
	    public void update(Graphics g){
	        paint(g);    
	    }
	    
	    /**
	     * 目盛りを描く。
	     * 
	     * */
	    public void drawMarker(Graphics g, int step, int in, int out) {
	        for (int i = 0; i < step; i++)
	            g.drawLine(getWidth() / 2 + (int) (in * Math.sin(Math.PI * 2.0 * (double) i / (double) step)), 
	                    getHeight() / 2 - (int) (in * Math.cos(Math.PI * 2.0 * (double) i / (double) step)), 
	                    getWidth() / 2 + (int) (out * Math.sin(Math.PI * 2.0 * (double) i / (double) step)), 
	                    getHeight() / 2 - (int) (out * Math.cos(Math.PI * 2.0 * (double) i / (double) step)));
	
	    }
	
	
	    public void mouseDragged(MouseEvent arg0) {
	        timer.calcPoint((double) (arg0.getX() - getWidth() / 2), (double) (arg0.getY() - getHeight() / 2));
	        lblTimer.setText(df.format(timer.getHour()) + ":" + df.format(timer.getMinute()));
	        repaint();
	    }
	
	    public void mouseMoved(MouseEvent arg0) { }
	}
	
	/*
	 * ClockHand.class
	 * 時計の針を著わす抽象クラス。
	 * 針の方向は、中心座標を(0,0)として、
	 *  0時の針は画面座標系で(0,-1)
	 *  6時の針(12時間時計では3時の針)は画面座標系で(1,0)
	 * ・・・
	 * とする。
	 **/
	abstract class ClockHand {
		private Color color; //色
		private int  length; //長さ
		private static Calendar cal = new GregorianCalendar();
		
		/**
		 * 現在時刻を保持したCalendarクラスを返す。 
		 **/
		public Calendar getCalendar(){
			cal.setTime(new Date());
			return cal;
		}
		
		public ClockHand(int len,Color col){
			length = len;
			color =col;
		}
		
		/**
		 * 針の長さを返す
		 * */	
		public int getLength(){
			return length;
		}
		
		/**
		 * 針の方向を返す。
		 * */
		abstract public Point getPoint();
		
		public Color getColor(){
			return color;
		}
		
	}
	
	/**
	 * Timer.class
	 * タイマー。
	 * 指定した時刻になるとウィンドウを表示する。
	 * 設定できる時刻は10分刻みである。
	 * */
	class Timer extends ClockHand{
		private Point point = new Point();
		
		//タイマーの設定時刻を表す値。24時間を10分刻み 0〜143の値をとる。
		private int value=0;
		
		//アラーム表示中かどうか
		private boolean alarming = false;
		
		//タイマーの指す針の方向を格納するテーブル
		double [] tableX = new double[6*24];
		double [] tableY = new double[6*24];
	
		DecimalFormat df = new DecimalFormat("00");
		
		public Timer(int len, Color col) {
			super(len, col);
			for (int i = 0; i < 144; i++) {
			    tableX[i] = Math.sin(Math.PI * 2.0 * (double) (i) / 144.0);
			    tableY[i] = -Math.cos(Math.PI * 2.0 * (double) (i) / 144.0); 
			}
		}
	
		public Point getPoint() {
			return point;
		}
		/**
		 * アプレット領域の中心を基準にしたマウスの座標から
		 * 針の方向を計算し移動させる。(時刻の設定は行わない)
		 * */
		public void movePoint(double x,double y){
			if(x==0&&y==0) return;
			double zz = Math.sqrt(x * x + y * y);
	        double l = (double) (getLength());
	        point.setLocation(x * l / zz, y * l / zz);
		}
		
		//時間を返す
		public int getHour(){
		    return value/6;
		}
		
		//分を返す
		public int getMinute(){
		    return value%6*10;
		}
		//タイマーの時刻を設定する
		public void setTime(int hour,int minute){
		    value = hour*6+(minute/10)*10;
	        double l = (double) (getLength());
	        point.setLocation(tableX[value] * l, tableY[value] * l);
		}
		
		/**
		 * アプレット領域の中心を基準にしたマウスの座標から
		 * 針の方向を計算する。
		 * さらに針の向きに対応する時刻を設定する。	
		 * */
		public void calcPoint(double x, double y){
		    alarming = false;
			if (x == 0 && y == 0)  return;
	        double zz = Math.sqrt(x * x + y * y);
	        double l = (double) (getLength());
	        int n = search(tableY, y / zz);
	
	        if (x > 0) {
	            value = n;
	        } else {
	            value = 71 + (72 - n);
	        }
	        point.setLocation(tableX[value] * l, tableY[value] * l);
	
		}
		
	
		/**
		 * 与えられた値に一番近いテーブルのインデックスを返す。
		 * @param tbl 針の向きを格納したテーブル
		 * @param y 比較対象の値
		 * @return テーブルのインデックス
		 */
		private int search(double[] tbl,double y){
			if (tbl[0] >= y)   return 0;
	        for (int i = 0; i < tbl.length/2 - 1; i++)
	            if (tbl[i] <= y && y < tbl[i + 1]) return i;
	        if (tbl[tbl.length/2-1] <= y) return tbl.length/2 - 1;
	        return 0;
	
		}
		
		/**
		 * タイマーが作動すべき時刻かどうかチェック。
		 * アラーム時刻の場合、ダイアログを表示。
		 * */
		public void checkAlarmTime(){
		    if(alarming) return;
	        Calendar cal = getCalendar();
	        int h = cal.get(Calendar.HOUR_OF_DAY);
	        int m = cal.get(Calendar.MINUTE);
	        
	        if (getHour()==h && getMinute()==m &&!alarming){
	            showDialog();
	        }
		    
		}
		
		public int getValue(){
			return value;
		}
		
	   	public void showDialog(){
	   	    alarming = true;
	        final JFrame dialog = new JFrame("Alarm");
	        Container c = dialog.getContentPane();
	        c.setLayout(new GridLayout(2, 1));
	        JPanel p1 = new JPanel();
	        JPanel p2 = new JPanel();
	
	        Label lbl1 = new Label("アラーム");
	        Label lbl2= new Label(df.format(getHour()) + ":" + df.format(getMinute()));
	        p1.setLayout(new FlowLayout());
	        p1.add(lbl1);
	        p1.add(lbl2);
	        Button btn = new Button("OK");
	        btn.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent ae) {
	                dialog.setVisible(false);
	            }
	        });
	        p2.add(btn);
	        c.add(p1);
	        c.add(p2);
	
	        Button dummy=new Button();
	        dialog.setSize(200, 200);
	        dialog.setLocationRelativeTo(dummy);
	        dialog.setResizable(false);
	        dialog.setVisible(true);
	    }
	}
	
	/*
	 * Hour.class
	 * 時針を表すクラス
	 * */
	class Hour extends ClockHand{
	
		private Point point = new Point();
	
		public Hour(int len,Color col){
			super(len,col);
		}
	
	
		public Point getPoint() {
			Calendar cal = getCalendar();
			double val  = (cal.get(Calendar.HOUR_OF_DAY) + cal.get((Calendar.MINUTE))/60.0) /24.0;
			double x = getLength()*Math.sin(val*Math.PI*2.0);
			double y = -getLength()*Math.cos(val*Math.PI*2.0);
	
			point.setLocation(x,y);
			return point;
		}
	}
	
	/*
	 * Minute.class
	 * 分針を表すクラス
	 * */
	class Minute extends ClockHand{
		private Point point = new Point();
		
		public Minute(int len,Color col){
			super(len,col);
		}
		
		public Point getPoint() {
			Calendar cal = getCalendar();
			double x = getLength()*Math.sin((double)(cal.get(Calendar.MINUTE) )/60.0*Math.PI*2.0);
			double y = -getLength()*Math.cos((double)(cal.get(Calendar.MINUTE) )/60.0*Math.PI*2.0);
	
			point.setLocation(x,y);
			return point;
		}
		
	}
	
	/*
	 * Second.class
	 * 秒針を表すクラス
	 * */
	class Second extends ClockHand {
	    Point point =new Point();
	    public Second(int len, Color col) {
	        super(len, col);
	    }
	
	    public Point getPoint() {
	        Calendar cal = getCalendar();
	
	        double x = getLength() * Math.sin(Math.PI * 2 * (double) (cal.get(Calendar.SECOND)) / 60.0);
	        double y = -getLength() * Math.cos(Math.PI * 2 * (double) (cal.get(Calendar.SECOND)) / 60.0);
	   
	        point.setLocation(x, y);
	        return point;
	    }
	}

CONTENTS

最新の20件

2020-11-14 2005-12-06 2006-11-04 2012-07-15 2009-06-19 2011-03-03 2006-12-13 2007-11-05 2014-07-22 2014-07-19 2014-07-09 2014-01-14 2012-09-03 2012-03-28

今日の20件

人気の30件

  • counter: 3216
  • today: 1
  • yesterday: 1
  • online: 1