Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1526058
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: Java

2012-04-28 11:31:54

index.html

点击(此处)折叠或打开

  1. <HTML>
  2. <HEAD>
  3. <TITLE>
  4. HTML Test Page
  5. </TITLE>
  6. </HEAD>
  7. Java实用教程
  8. <BODY>
  9. <APPLET
  10. CODEBASE = "."
  11. CODE = "billsClock.class"
  12. NAME = "TestApplet"
  13. WIDTH = 400
  14. HEIGHT = 300
  15. HSPACE = 0
  16. VSPACE = 0
  17. ALIGN = middle
  18. >
  19. </APPLET>
  20. </BODY>
  21. </HTML>

billsClock.java

点击(此处)折叠或打开

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4. import java.net.*;
  5. class hms extends Date
  6. {
  7.     //Note that localOffset is hours difference from GMT
  8.     //west of Greenwich meridian is positive, east is negative.
  9.     //i.e. New York City (Eastern Standard Time) is +5
  10.     // Eastern Daylight Time is +4
  11.     public hms(double localOffset)
  12.     {
  13.         super();
  14.         long tzOffset=getTimezoneOffset()*60L*1000L;
  15.         setTime(getTime() + tzOffset - (long)(localOffset*3600.0*1000.0));
  16.     }
  17.     public hms()
  18.     {
  19.         super();
  20.     }
  21.     public double get_hours()
  22.     {
  23.         return (double)super.getHours()+(double)getMinutes()/60.0;
  24.     }
  25. }


  26. abstract class clockHand
  27. {
  28.     protected int ba***[], baseY[];
  29.     protected int transX[],transY[];
  30.     protected int numberOfPoints;
  31.     public clockHand(int originX, int originY, int length,int thickness,int points)
  32.     {
  33.         ba***= new int[points]; baseY=new int[points];
  34.         transX= new int[points]; transY=new int[points];
  35.         initiallizePoints(originX,originY,length,thickness);
  36.         numberOfPoints=points;
  37.     }
  38.     abstract protected void initiallizePoints( int originX,
  39.     int originY,
  40.     int length,
  41.     int thickness);
  42.     abstract public void draw(Color color, double angle, Graphics g);
  43.     protected void transform(double angle)
  44.     {
  45.         for(int i=0;i<numberOfPoints;i++)
  46.         {
  47.             transX[i]=(int)( (ba***[0]-ba***[i]) * Math.cos(angle) -
  48.             (baseY[0]-baseY[i]) * Math.sin(angle) +
  49.             ba***[0]);
  50.             transY[i]=(int)( (ba***[0]-ba***[i]) * Math.sin(angle) +
  51.             (baseY[0]-baseY[i]) * Math.cos(angle) +
  52.             baseY[0]);
  53.         }
  54.     }
  55. }


  56. class sweepHand extends clockHand
  57. {
  58.     public sweepHand(int originX,int originY, int length, int points)
  59.     {
  60.         super(originX,originY,length,0,points);
  61.     }
  62.     protected void initiallizePoints(int originX,int originY, int length, int unused)
  63.     {
  64.         unused=unused; //We don't use the thickness parameter in this class
  65.         //This comes from habit to prevent compiler warning
  66.         //concerning unused arguments.
  67.         ba***[0]=originX; baseY[0]=originY;
  68.         ba***[1]=originX; baseY[1]=originY-length/5;
  69.         ba***[2]=originX; baseY[2]=originY+length;
  70.     }
  71.     public void draw(Color color, double angle, Graphics g)
  72.     {
  73.         transform(angle);
  74.         g.setColor(color);
  75.         g.drawLine(transX[1],transY[1],transX[2],transY[2]);
  76.     }
  77. }


  78. class hmHand extends clockHand
  79. {
  80.     public hmHand(int originX,int originY, int length,int thickness, int points)
  81.     {
  82.         super(originX,originY,length,thickness,points);
  83.     }
  84.     protected void initiallizePoints( int originX,
  85.     int originY,
  86.     int length,
  87.     int thickness)
  88.     {
  89.         ba***[0]=originX;
  90.         baseY[0]=originY;
  91.         ba***[1]=ba***[0]-thickness/2;
  92.         baseY[1]=baseY[0]+thickness/2;
  93.         ba***[2]=ba***[1];
  94.         baseY[2]=baseY[0]+length- thickness;
  95.         ba***[3]=ba***[0];
  96.         baseY[3]=baseY[0]+length;
  97.         ba***[4]=ba***[0]+thickness/2;
  98.         baseY[4]=baseY[2];
  99.         ba***[5]=ba***[4];
  100.         baseY[5]=baseY[1];
  101.     }
  102.     public void draw(Color color,double angle, Graphics g)
  103.     {
  104.         transform(angle);
  105.         g.setColor(color);
  106.         g.fillPolygon(transX,transY,numberOfPoints);
  107.     }
  108. }


  109. public class billsClock extends Applet implements Runnable
  110. {
  111.     //some DEFINE'd constants
  112.     static final int BACKGROUND=0; //Background image index
  113.     static final int LOGO=1; //Logo image index
  114.     static final String JAVEX="GODSON"; //Default text on clock face
  115.     static final double MINSEC=0.104719755; //Radians per minute or second
  116.     static final double HOUR=0.523598776; //Radians per hour
  117.     Thread clockThread = null;
  118.     //User options, see getParameterInfo(), below.
  119.     int width = 100;
  120.     int height = 100;
  121.     Color bgColor = new Color(0,0,0);
  122.     Color faceColor = new Color(0,0,0);
  123.     Color sweepColor = new Color(255,0,0);
  124.     Color minuteColor = new Color (192,192,192);
  125.     Color hourColor = new Color (255,255,255);
  126.     Color textColor = new Color (255,255,255);
  127.     Color caseColor = new Color (0,0,0);
  128.     Color trimColor = new Color (192,192,192);
  129.     String logoString=null;
  130.     Image images[] = new Image[2]; //Array to hold optional images
  131.     boolean isPainted=false; //Force painting on first update, if not painted
  132.     //Center point of clock
  133.     int x1,y1;
  134.     //Array of points for triangular icon at 12:00
  135.     int xPoints[]=new int[3], yPoints[]=new int[3];
  136.     //Class to hold time, with method to return (double)(hours + minutes/60)
  137.     hms cur_time;
  138.     //The clock's seconds, minutes, and hours hands.
  139.     sweepHand sweep;
  140.     hmHand minuteHand,
  141.     hourHand;
  142.     //The last parameters used to draw the hands.
  143.     double lastHour;
  144.     int lastMinute,lastSecond;
  145.     //The font used for text and date.
  146.     Font font;
  147.     //Offscreen image and device context, for buffered output.
  148.     Image offScrImage;
  149.     Graphics offScrGC;
  150.     // Use to test background image, if any.
  151.     MediaTracker tracker;
  152.     int minDimension; // Ensure a round clock if applet is not square.
  153.     int originX; // Upper left corner of a square enclosing the clock
  154.     int originY; // with respect to applet area.
  155.     double tzDifference=0;
  156.     boolean localOnly=false;
  157.     //Users' parameters - self-explanatory?
  158.     public String[][] getParameterInfo()
  159.     {
  160.         String[][] info =
  161.         {
  162.             {
  163.                 "width", "int", "width of the applet, in pixels"
  164.             },
  165.             {
  166.                 "height", "int", "height of the applet, in pixels"
  167.             },
  168.             {
  169.                 "bgColor", "string", "hex color triplet of the background, i.e. 000000 for black "
  170.             },
  171.             {
  172.                 "faceColor", "string", "hex color triplet of clock face, i.e. 000000 for black "
  173.             },
  174.             {
  175.                 "sweepColor", "string", "hex color triplet of seconds hand, i.e. FF0000 for red "
  176.             },
  177.             {
  178.                 "minuteColor", "string", "hex color triplet of minutes hand, i.e. C0C0C0 for lt.gray "
  179.             },
  180.             {
  181.                 "hourColor", "string", "hex color triplet of hours hand, i.e. FFFFFF for white "
  182.             },
  183.             {
  184.                 "textColor", "string", "hex color triplet of numbers, etc., i.e. FFFFFF for white "
  185.             },
  186.             {
  187.                 "caseColor", "string", "hex color triplet of case, i.e. 000000 for black "
  188.             },
  189.             {
  190.                 "trimColor", "string", "hex color triplet of case outliners, i.e. C0C0C0 for lt.gray "
  191.             },
  192.             {
  193.                 "bgImageURL", "string", "URL of background image, if any "
  194.             },
  195.             {
  196.                 "logoString", "string", "Name to display on watch face "
  197.             },
  198.             {
  199.                 "logoImageURL","string", "URL of logo image to display on watch face "
  200.             },
  201.             {
  202.                 "timezone", "real", "Timezone difference from GMT (decimal hours,+ West/- East)<0>"
  203.             },
  204.             {
  205.                 "localonly", "int", "Non-zero will cause clock to display current local time <0>"
  206.             }
  207.         };
  208.         return info;
  209.     }
  210.     //Applet name, author and info lines
  211.     public String getAppletInfo()
  212.     {
  213.         return "billsClock 1.04 (C) 1996 by Bill Giel";
  214.     }
  215.     void showURLerror(Exception e)
  216.     {
  217.         String errorMsg = "JAVEX URL error: "+e;
  218.         showStatus(errorMsg);
  219.         System.err.println(errorMsg);
  220.     }
  221.     // This lets us create clocks of various sizes, but with the same
  222.     // proportions.
  223.     private int size(int percent)
  224.     {
  225.         return (int)((double)percent/100.0 * (double)minDimension);
  226.     }
  227.     public void init()
  228.     {
  229.         URL imagesURL[] = new URL[2];
  230.         String szImagesURL[] = new String[2];
  231.         tracker = new MediaTracker(this);
  232.         String paramString = getParameter( "WIDTH" );
  233.         if( paramString != null )
  234.         width = Integer.valueOf(paramString).intValue();
  235.         paramString = getParameter( "HEIGHT" );
  236.         if( paramString != null )
  237.         height = Integer.valueOf(paramString).intValue();
  238.         paramString = getParameter( "TIMEZONE" );
  239.         if( paramString != null )
  240.         tzDifference = Double.valueOf(paramString).doubleValue();
  241.         paramString = getParameter( "LOCALONLY" );
  242.         if( paramString != null && Integer.valueOf(paramString).intValue() != 0)
  243.         {
  244.             localOnly=true;
  245.             tzDifference=0.;
  246.         }
  247.         paramString = getParameter( "BGCOLOR");
  248.         if( paramString != null )
  249.         bgColor=parseColorString(paramString);
  250.         paramString = getParameter( "FACECOLOR");
  251.         if( paramString != null )
  252.         faceColor=parseColorString(paramString);
  253.         paramString = getParameter( "SWEEPCOLOR");
  254.         if( paramString != null )
  255.         sweepColor=parseColorString(paramString);
  256.         paramString = getParameter( "MINUTECOLOR");
  257.         if( paramString != null )
  258.         minuteColor=parseColorString(paramString);
  259.         paramString = getParameter( "HOURCOLOR");
  260.         if( paramString != null )
  261.         hourColor=parseColorString(paramString);
  262.         paramString = getParameter( "TEXTCOLOR");
  263.         if( paramString != null )
  264.         textColor=parseColorString(paramString);
  265.         paramString = getParameter( "CASECOLOR");
  266.         if( paramString != null )
  267.         caseColor=parseColorString(paramString);
  268.         paramString = getParameter( "TRIMCOLOR");
  269.         if( paramString != null )
  270.         trimColor=parseColorString(paramString);
  271.         logoString = getParameter( "LOGOSTRING");
  272.         if( logoString == null )
  273.         logoString=JAVEX;
  274.         else if(logoString.length() > 8)
  275.         logoString= logoString.substring(0,8); //Max 8 characters!
  276.         szImagesURL[BACKGROUND] = getParameter("BGIMAGEURL");
  277.         szImagesURL[LOGO] = getParameter("LOGOIMAGEURL");
  278.         for(int i=0; i<2; i++)
  279.         {
  280.             if(szImagesURL[i] != null)
  281.             {
  282.                 try
  283.                 {
  284.                     imagesURL[i]=new URL(getCodeBase(),szImagesURL[i]);
  285.                 }
  286.                 catch (MalformedURLException e)
  287.                 {
  288.                     showURLerror(e);
  289.                     imagesURL[i]=null;
  290.                     images[i]=null;
  291.                 }
  292.                 if(imagesURL[i] != null)
  293.                 {
  294.                     showStatus("Javex loading image: " + imagesURL[i].toString());
  295.                     images[i]=getImage(imagesURL[i]);
  296.                     if(images[i] != null)
  297.                     tracker.addImage(images[i],i);
  298.                     showStatus("");
  299.                 }
  300.                 if(images[i] != null)
  301.                 try
  302.                 {
  303.                     tracker.waitForID(i);
  304.                 }
  305.                 catch (InterruptedException e)
  306.                 {
  307.                     images[i]=null;
  308.                 }
  309.             }
  310.             else images[i]=null;
  311.         }
  312.         cur_time=(localOnly)? new hms() : new hms(tzDifference);
  313.         lastHour=-1.0;
  314.         lastMinute=-1;
  315.         lastSecond=-1;
  316.         x1=width/2;
  317.         y1=height/2;
  318.         minDimension= Math.min(width, height);
  319.         originX=(width-minDimension)/2;
  320.         originY=(height-minDimension)/2;
  321.         xPoints[1]=x1-size(3); xPoints[2]=x1+size(3); xPoints[0]=x1;
  322.         yPoints[1]=y1-size(38);yPoints[2]=y1-size(38); yPoints[0]=y1-size(27);
  323.         sweep=new sweepHand(x1,y1,size(40),3);
  324.         minuteHand=new hmHand(x1,y1,size(40),size(6),6);
  325.         hourHand=new hmHand(x1,y1,size(25),size(8),6);
  326.         font=new Font("TXT",Font.BOLD,size(10));
  327.         offScrImage = createImage(width,height);
  328.         offScrGC = offScrImage.getGraphics();
  329.         System.out.println(getAppletInfo());
  330.     }
  331.     public void start()
  332.     {
  333.         if(clockThread == null)
  334.         {
  335.             clockThread = new Thread(this);
  336.             clockThread.start();
  337.         }
  338.     }
  339.     public void stop()
  340.     {
  341.         if(null != clockThread)
  342.         {
  343.             clockThread.stop();
  344.             clockThread=null;
  345.         }
  346.     }
  347.     private void drawHands(Graphics g)
  348.     {
  349.         double angle;
  350.         int i,j;
  351.         int x,y;
  352.         angle=MINSEC * lastSecond;
  353.         sweep.draw(faceColor, angle, g);
  354.         if(cur_time.getMinutes() != lastMinute)
  355.         {
  356.             minuteHand.draw(faceColor,MINSEC*lastMinute,g);
  357.             if(cur_time.get_hours() != lastHour)
  358.             hourHand.draw(faceColor,HOUR*lastHour,g);
  359.         }
  360.         g.setColor(textColor);
  361.         g.fillRect(originX+size(12),y1-size(2),size(10),size(4));
  362.         g.fillRect(x1-size(2),originY + minDimension-size(22),size(4),size(10));
  363.         g.fillPolygon( xPoints, yPoints, 3);
  364.         for(i=1;i<12;i+=3)
  365.         for(j=i;j<i+2;j++)
  366.         {
  367.             x=(int)(x1+Math.sin(HOUR*j)*size(35));
  368.             y=(int)(y1-Math.cos(HOUR*j)*size(35));
  369.             g.fillOval(x-size(3),y-size(3),size(6),size(6));
  370.         }
  371.         //Set the font and get font info...
  372.         g.setFont(font);
  373.         FontMetrics fm=g.getFontMetrics();
  374.         //Paint our logo...
  375.         g.drawString(logoString,x1-fm.stringWidth(logoString)/2,y1-size(12));
  376.         //Get the day of the month...
  377.         String day=Integer.toString(cur_time.getDate(),10);
  378.         //Paint it...
  379.         g.drawString( day,
  380.         originX + minDimension-size(14)-fm.stringWidth(day),
  381.         y1+size(5));
  382.         //and put a box around it.
  383.         g.drawRect( originX + minDimension-size(14)-fm.stringWidth(day)-size(2),
  384.         y1-size(5)-size(2),
  385.         fm.stringWidth(day)+size(4),
  386.         size(10)+size(4));
  387.         if(images[LOGO] != null)
  388.         {
  389.             x = originX + (minDimension-images[LOGO].getWidth(this))/2;
  390.             y = y1 + (minDimension/2 - size(22) - images[LOGO].getHeight(this))/2;
  391.             if(x > 0 && y > 0)
  392.             offScrGC.drawImage(images[LOGO], x, y, this);
  393.         }
  394.         lastHour=cur_time.get_hours();
  395.         hourHand.draw(hourColor,HOUR*lastHour,g);
  396.         lastMinute=cur_time.getMinutes();
  397.         minuteHand.draw(minuteColor,MINSEC*lastMinute,g);
  398.         g.setColor(minuteColor);
  399.         g.fillOval(x1-size(4),y1-size(4),size(8),size(8));
  400.         g.setColor(sweepColor);
  401.         g.fillOval(x1-size(3),y1-size(3),size(6),size(6));
  402.         lastSecond=cur_time.getSeconds();
  403.         angle=MINSEC*lastSecond;
  404.         sweep.draw(sweepColor, angle,g);
  405.         g.setColor(trimColor);
  406.         g.fillOval(x1-size(1),y1-size(1),size(2),size(2));
  407.     }
  408.     private Color parseColorString(String colorString)
  409.     {
  410.         if(colorString.length()==6)
  411.         {
  412.             int R = Integer.valueOf(colorString.substring(0,2),16).intValue();
  413.             int G = Integer.valueOf(colorString.substring(2,4),16).intValue();
  414.             int B = Integer.valueOf(colorString.substring(4,6),16).intValue();
  415.             return new Color(R,G,B);
  416.         }
  417.         else return Color.lightGray;
  418.     }
  419.     public void run()
  420.     {
  421.         //Let's not hog the system, now...
  422.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  423.         repaint();
  424.         while(null != clockThread)
  425.         {
  426.             cur_time= (localOnly)? new hms() :new hms(tzDifference);
  427.             repaint();
  428.             try
  429.             {
  430.                 Thread.sleep(1000);
  431.             }
  432.             catch (InterruptedException e)
  433.             {
  434.             }
  435.         }
  436.         System.exit(0);
  437.     }
  438.     public void paint(Graphics g)
  439.     {
  440.         int i,x0,y0,x2,y2;
  441.         if(images[BACKGROUND] == null)
  442.         {
  443.             offScrGC.setColor(bgColor);
  444.             offScrGC.fillRect(0,0,width,height);
  445.         }
  446.         else
  447.         offScrGC.drawImage(images[BACKGROUND], 0, 0, this);
  448.         offScrGC.setColor(caseColor);
  449.         //Shrink one pixel so we don't clip anything off...
  450.         offScrGC.fillOval( originX+1,
  451.         originY+1,
  452.         minDimension-2,
  453.         minDimension-2);
  454.         offScrGC.setColor(faceColor);
  455.         offScrGC.fillOval( originX + size(5),
  456.         originY + size(5),
  457.         minDimension - size(10),
  458.         minDimension - size(10));
  459.         offScrGC.setColor(trimColor);
  460.         offScrGC.drawOval( originX+1,
  461.         originY+1,
  462.         minDimension-2,
  463.         minDimension-2);
  464.         offScrGC.drawOval( originX + size(5),
  465.         originY + size(5),
  466.         minDimension - size(10),
  467.         minDimension - size(10));
  468.         offScrGC.setColor(textColor);
  469.         //Draw graduations, a longer index every fifth mark...
  470.         for(i=0;i<60;i++)
  471.         {
  472.             if(i==0 || (i>=5 && i%5 == 0))
  473.             {
  474.                 x0=(int)(x1+size(40)*Math.sin(MINSEC*i));
  475.                 y0=(int)(y1+size(40)*Math.cos(MINSEC*i));
  476.             }
  477.             else
  478.             {
  479.                 x0=(int)(x1+size(42)*Math.sin(MINSEC*i));
  480.                 y0=(int)(y1+size(42)*Math.cos(MINSEC*i));
  481.             }
  482.             x2=(int)(x1+size(44)*Math.sin(MINSEC*i));
  483.             y2=(int)(y1+size(44)*Math.cos(MINSEC*i));
  484.             offScrGC.drawLine(x0,y0,x2,y2);
  485.         }
  486.         drawHands(offScrGC);
  487.         g.drawImage(offScrImage,0,0,this);
  488.         isPainted=true;
  489.     }
  490.     public synchronized void update(Graphics g)
  491.     {
  492.         if(!isPainted)
  493.         paint(g);
  494.         else
  495.         {
  496.             drawHands(offScrGC);
  497.             g.drawImage(offScrImage,0,0,this);
  498.         }
  499.     }
  500. }

运行: $ appletviewer index.html

原文链接:
阅读(2257) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~