import java.awt.*; import java.util.Calendar; import java.util.Date; import java.applet.Applet; public class Time extends Applet implements Runnable { Graphics off_g; Image off; Thread th; Date date; Calendar ca; String s; String year; String day; Font font1,font2; FontMetrics fm; double ra1,ra2,ra3; int w, h; int ha_w; Image img; int img_w; int img_h; MediaTracker mt; Color bgcolor; Color bgcolor2,bgcolor3; Color hour, minu, sec; int param1; /*********init********/ public void init(){ w = getSize().width-1; h = getSize().height-1; off = createImage(w+1,h+1); off_g = off.getGraphics(); th = null; mt = new MediaTracker(this); font1 = new Font("Dialog",Font.PLAIN,20); font2 = new Font("Dialog",Font.PLAIN,10); fm = getFontMetrics(font1); ra1 = ra2 = ra3 = 0; ha_w = 3; img_w = 10; img_h = 10; /*----パラメータ取得-----*/ if (!"".equals(getParameter("image"))){ img = getImage(getCodeBase(),getParameter("image")); mt.addImage(img,0); } else { img = null; mt.removeImage(img,0); } bgcolor = getColor(getParameter("bgcolor")); bgcolor2 = getColor(getParameter("bgcolor2")); bgcolor3 = getColor(getParameter("bgcolor3")); hour = getColor(getParameter("hour")); minu = getColor(getParameter("minu")); sec = getColor(getParameter("sec")); param1 = Integer.parseInt(getParameter("param1")); } /*********start*******/ public void start(){ if (th == null){ th = new Thread(this); th.start(); } } /*********run********/ public void run() { while (th != null){ if (mt.checkID(0)){ repaint(); } else { try{ mt.waitForID(0); }catch(InterruptedException e){} } try{ th.sleep(200); } catch (InterruptedException e){} } } /********paint*******/ public void paint(Graphics g){ if (mt.isErrorID(0)){ off_g.setColor(Color.white); off_g.fillRect(0,0,w+1,h+1); off_g.setFont(font2); off_g.setColor(Color.black); off_g.drawString("Load Error...",10,10); } else if (mt.checkID(0)){ img_w = img.getWidth(null); img_h = img.getHeight(null); off_g.setColor(bgcolor); off_g.fillRect(0,0,w+1,h+1); if (img != null) off_g.drawImage(img,(w/2)-(img_w/2),(h/2)-(img_h/2),this); ca = Calendar.getInstance(); ra1 = (Math.PI/30)*ca.get(ca.SECOND) - Math.PI/2; ra2 = (Math.PI/30)*ca.get(ca.MINUTE) - Math.PI/2; ra3 = (Math.PI/6)*ca.get(ca.HOUR) - Math.PI/2 + ((Math.PI*2)/(12*60))*ca.get(ca.MINUTE); draw_clock(ra1,ra2,ra3);//アナログ時計を描く year = Integer.toString(ca.get(ca.YEAR)); s = (ca.get(ca.MONTH) + 1) + "/" + ca.get(ca.DATE); day = getDay(ca.get(ca.DAY_OF_WEEK));//曜日を文字列にする /* off_g.setColor(Color.white); fm = off_g.getFontMetrics(); //off_g.drawString(year,(w/2)-(fm.stringWidth(year)/2),30); off_g.setFont(font1); fm = off_g.getFontMetrics(); off_g.drawString(s,(w/2)-(fm.stringWidth(s)/2),40); */ } else { off_g.setColor(Color.white); off_g.fillRect(0,0,w+1,h+1); off_g.setFont(font2); off_g.setColor(Color.black); off_g.drawString("Now Loading...",10,10); } g.drawImage(off,0,0,this); } /********draw_clock******/ void draw_clock(double ra1, double ra2, double ra3){ int clock_x[] = new int[3]; int clock_y[] = new int[3]; clock_x[0] = clock_x[1] = clock_x[2] = 0; clock_y[0] = clock_y[1] = clock_y[2] = 0; int length = 0; double ra = 0; if (param1 == 1){ off_g.setColor(bgcolor2); off_g.drawOval(0,0,w,h);//時計の枠 } off_g.setColor(bgcolor3); for (int i = 0; i <= 11; i++){//時間の点 off_g.fillOval((int)((w/2)*((double)9/10)* Math.sin((Math.PI/6)*i))+(w/2)-3, (int)((w/2)*((double)9/10)* Math.cos((Math.PI/6)*i))+(h/2)-3,6,6); } for (int i = 1; i <= 3; i++){ switch (i){ case 1://短針 off_g.setColor(hour); length = (int)(Math.rint((w/2) * ((double)1/2))); ra = ra3; break; case 2: length = (int)(Math.rint((w/2) * ((double)4/5))); ra = ra2; off_g.setColor(minu); break; case 3: length = (int)(Math.rint((w/2) * ((double)9/10))); ra = ra1; off_g.setColor(sec); break; } clock_x[0] = (w/2)+(int)Math.rint(length*Math.cos(ra)); clock_y[0] = (h/2)+(int)Math.rint(length*Math.sin(ra)); clock_x[1] = (w/2)+(int)Math.rint(ha_w*Math.cos(ra-(Math.PI/2))); clock_y[1] = (h/2)+(int)Math.rint(ha_w*Math.sin(ra-(Math.PI/2))); clock_x[2] = (w/2)+(int)Math.rint(ha_w*Math.cos(ra+(Math.PI/2))); clock_y[2] = (h/2)+(int)Math.rint(ha_w*Math.sin(ra+(Math.PI/2))); if (i == 3){ off_g.drawLine(w/2,h/2,clock_x[0],clock_y[0]); clock_x[0] = (w/2)+(int)Math.rint(13*-Math.cos(ra)); clock_y[0] = (h/2)+(int)Math.rint(13*-Math.sin(ra)); off_g.drawLine(w/2,h/2,clock_x[0],clock_y[0]); } else { off_g.fillPolygon(clock_x,clock_y,3); off_g.setColor(bgcolor2); off_g.drawPolygon(clock_x,clock_y,3); } } off_g.setColor(bgcolor2); off_g.fillOval((w/2)-4,(h/2)-4,8,8); } /*********getDay***********/ String getDay(int day_num){ String day; switch (day_num){ case 1: day = "Sun"; break; case 2: day = "Mon"; break; case 3: day = "Tue"; break; case 4: day = "Wed"; break; case 5: day = "Thu"; break; case 6: day = "Fri"; break; default: day = "Sat"; } return day; } /******getColor*****/ Color getColor(String s){ int index = 0; int index2 = 0; int r,g,b; r = g = b = 0; s = s.trim(); index2= s.indexOf(","); String c = s.substring(index,index2); r = Integer.parseInt(c); index = index2 + 1; index2 = s.indexOf(",",index); c = s.substring(index,index2); g = Integer.parseInt(c); index = index2 + 1; index2 = s.length(); c = s.substring(index,index2); b = Integer.parseInt(c); return new Color(r,g,b); } /*******update********/ public void update(Graphics g){ paint(g); } /*******stop********/ public void stop(){ if (th != null) th = null; } }