★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/Java_by_Example/section7_5.htmlNouvelle recherche | Portail wanayoo
Computer Digital Expo

The Enterprise IM Strategies & Solutions Event


Find a Web Host With:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
Internet News
Internet Investing
IT
Windows Technology
Linux/Open Source
Developer
Interactive Marketing
xSP Resources
Small Business
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers
Be a Commerce Partner
Business Search
Compare Prices & Shop
Software Store
Promote Your Website
Find a Web HostFind a Web Host
Web Hosting
Shop For Computers
Register Domains
Internet Jobs
Hacker's Secrets
FlashKit
Gif.com
HierMenusCentral
JavaBoutique
JavaScript.com
JavaScriptSource
WebDev Jobs
JustSMIL
ScriptSearch
StreamingMediaWorld
Web Hosts
WebDevelopersJournal WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com
CGI
HTML
Java
JavaScript
Web Design and Promotion
Web Site Development
E-mail Address:


Can you receive
HTML e-mail?

Yes   No

Zip Code:



Tutorials : Java by Example : Section 7 :
Section Seven Contents
Fun With Letters and Words
Rotating Lines and Polygons
Sorting and Shuffling

Letters - Words - Lines - Shuffle

Rotating Lines and Polygons - Analog Clock

With the knowledge we have gained now, it is not very difficult to make a real analog clock with hands, like this one here. Note that Java doesn't allow to specify a pen with more than 1 pixel width, so you would have to draw several parallel lines in order to increase the width of the clock hands.

//Sourcecode

import java.awt.*;
import java.applet.*;
import java.util.*;

public class Project41 extends Applet implements Runnable
{
    Thread runner;
    Image Buffer;
    Graphics gBuffer;
    int angle;

    public void init()
    {
        Buffer=createImage(size().width,size().height);
        gBuffer=Buffer.getGraphics();
    }

    public void start()
    {
        if (runner == null)
        {
            runner = new Thread (this);
            runner.start();
        }
    }

    public void stop()
    {
        if (runner != null)
        {
            runner.stop();
            runner = null;
        }
    }

    public void run()
    {
        while(true)
        {
            try {runner.sleep(100);}
            catch (Exception e) { }

            repaint();
        }
    }

    void drawStuff()
    {
        //paint background black
        gBuffer.setColor(Color.black);
        gBuffer.fillRect(0,0,size().width,size().height);

        //get the center of the applet
        Point p=new Point(size().width/2, size().height/2);

        //define the hand lengths
        int S_LENGTH=85, M_LENGTH=80, H_LENGTH=60;

        Date d=new Date();
        int sec=d.getSeconds();
        int min=d.getMinutes();
        int hour=d.getHours();

        //calculate the vectors, using the constant Pi
        double vs = sec * Math.PI*2 / 60.0;
        double vm = min * Math.PI*2 / 60.0;
        double vh = hour * Math.PI*2 / 12.0 + vm / 12.0;

        int sx=(int)(p.x+S_LENGTH*Math.sin(vs));
        int sy=(int)(p.y-S_LENGTH*Math.cos(vs));

        int mx=(int)(p.x+M_LENGTH*Math.sin(vm));
        int my=(int)(p.y-M_LENGTH*Math.cos(vm));

        int hx=(int)(p.x+H_LENGTH*Math.sin(vh));
        int hy=(int)(p.y-H_LENGTH*Math.cos(vh));

        //the second hand
        gBuffer.setColor(Color.yellow);
        gBuffer.drawLine(p.x, p.y, sx, sy);

        //the minute hand
        gBuffer.setColor(Color.red);
        gBuffer.drawLine(p.x, p.y, mx, my);

        //the hour hand
        gBuffer.setColor(Color.green);
        gBuffer.drawLine(p.x, p.y, hx, hy);

        //the face and axis
        gBuffer.setColor(Color.blue);
        gBuffer.fillOval(p.x-6,p.y-6,12,12);
        gBuffer.drawOval(p.x-90,p.y-90,180,180);
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        drawStuff();
        g.drawImage (Buffer,0,0, this);
    }
}

internet.com logo














WebDev News

JBoss Group: Defections Aren't a Big Deal

IE Object Tag Buffer Overflow Patched

SCO Shuts Down German Site





JWizardPane
Build modern wizard style interface quickly. API easy to use. Bulti-in show dialog and frame support.
Download of the Week
BSCOutline v5.10
BSCOutline Java Outline Applet is a customizable tree view control for use in Web pages, similar to that used by Windows File Manager or Explorer. BSCOutline has been written to be the simple to use and small to store.

JB User Poll
As a JavaBoutique Reader what kind of articles do you prefer?
The JavaBoutique Top 15:
1. ChompText
2. PingPong
3. PulseText
4. DJPopMenu
5. RushHour
6. NavBar
7. 3DMaze
8. AScroll2
9. Encyclo
10. Nibbly
11. AnimLetters_anim
12. Distorter
13. BMMenu
14. SiteBrowser
15. Viewer
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
AOL 9.0 Represents Streaming Sea Change
After years of investment, AOL's own multimedia player technology represents an emerging agnostic standard among competing media player formats.

Lessons From the Lemonade Stand
10 ways for your small business to succeed at e-commerce..

Global Biz Still Unprepared
Record-breaking digital attacks and threats of more serious disasters have not inspired organizations to adopt complete business continuity plans for dealing with catastrophes.


The Event For Leading Edge Flash Development

Copyright 2003 Jupitermedia Corporation All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/