Results 1 to 5 of 5

Thread: code ทำนาฬิกา เอามาให้ศึกษากัน

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Posts
    1


    File :: Clock.java






    import java.awt.*;
    import javax.swing.*;
    import java.util.*;

    public class Clock extends JPanel {
    /**
    *
    */
    private static final long serialVersionUID = 1L;
    private int hour;
    private int minute;
    private int second;
    public Clock() {
    setCurrentTime();
    }

    public Clock(int hour, int minute, int second) {
    this.hour = hour;
    this.minute = minute;
    this.second = second;
    }
    public int getHour() {
    return hour;
    }
    public void setHour(int hour) {
    this.hour = hour;
    repaint();
    }
    public int getMinute() {
    return minute;
    }
    public void setMinute(int minute) {
    this.minute = minute;
    repaint();
    }
    public int getSecond() {
    return second;
    }
    public void setSecond(int second) {
    this.second = second;
    repaint();
    }
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    int clockRadius =
    (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
    int xCenter = getWidth() / 2;
    int yCenter = getHeight() / 2;
    g.setColor(Color.black);
    g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
    2 * clockRadius, 2 * clockRadius);
    g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);
    g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
    g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
    g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);
    int sLength = (int)(clockRadius * 0.8);
    int xSecond = (int)(xCenter + sLength *
    Math.sin(second * (2 * Math.PI / 60)));
    int ySecond = (int)(yCenter - sLength *
    Math.cos(second * (2 * Math.PI / 60)));
    g.setColor(Color.red);
    g.drawLine(xCenter, yCenter, xSecond, ySecond);
    int mLength = (int)(clockRadius * 0.65);
    int xMinute = (int)(xCenter + mLength *
    Math.sin(minute * (2 * Math.PI / 60)));
    int yMinute = (int)(yCenter - mLength *
    Math.cos(minute * (2 * Math.PI / 60)));
    g.setColor(Color.blue);
    g.drawLine(xCenter, yCenter, xMinute, yMinute);
    int hLength = (int)(clockRadius * 0.5);
    int xHour = (int)(xCenter + hLength *
    Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
    int yHour = (int)(yCenter - hLength *
    Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
    g.setColor(Color.green);
    g.drawLine(xCenter, yCenter, xHour, yHour);
    }
    public void setCurrentTime() {
    Calendar calendar = new GregorianCalendar();
    this.hour = calendar.get(Calendar.HOUR_OF_DAY);
    this.minute = calendar.get(Calendar.MINUTE);
    this.second = calendar.get(Calendar.SECOND);
    }
    public Dimension getPreferredSize() {
    return new Dimension(200, 200);
    }
    }






    File :: MyClock.java



    import java.awt.event.*;
    import javax.swing.*;
    public class MyClock extends Clock
    implements ActionListener {
    /**
    *
    */
    private static final long serialVersionUID = 1L;
    protected Timer timer = new Timer(1000,this);
    public MyClock(){
    timer.start();
    }
    public void actionPerformed(ActionEvent e){
    setCurrentTime();
    repaint();
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame("MyClock");
    MyClock clock = new MyClock();
    frame.getContentPane().add(clock);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
    }

    }







    ให้ นำ2 คลาสนี้ ไปรันดู (เลือก MainClass ด้วยนะ ถ้าจะทำเปนไฟล์ dot jar)

  2. #2
    Administrator asylu3's Avatar
    Join Date
    Jun 2000
    Location
    Thailand
    Posts
    3,557


    ใช้ Tag [ code ] [ / code ] ครอบด้วยนะครับพี่น้อง

  3. #3
    Junior Member
    Join Date
    Jul 2008
    Posts
    3


    คือว่าถ้าจะทำนาฬิกาแบบเข็มอ่ะครับ เอาตัวนี้ไปดัดแปลงได้มั้ย แนะนำด้วยครับ

  4. #4
    Junior Member
    Join Date
    Jul 2008
    Posts
    8


    ขอบคุณมากครับ กำลังทำความเข้าใจอยู่ งงมากมายในส่วนของกำหนดค่า สำหรับวาดรูป (พวกระยะรัศมี ระยะ วางตัวเลขเวลา รัยงี้) มันคำนวณได้น่าปวดหัวจิงๆ(เงื่อนไข) เหอๆ

    คือว่าถ้าจะทำนาฬิกาแบบเข็มอ่ะครับ เอาตัวนี้ไปดัดแปลงได้มั้ย แนะนำด้วยครับ[/b]
    ลองรันดูรึยังครับ ตัวนี้เป็นแบบเข็มอยู่แล้ว
    ถ้าเริ่มต้นไม่สวย ก็ต้องขยันระหว่างทาง...

  5. #5
    Junior Member
    Join Date
    Jul 2008
    Posts
    2


    ขอบคุนค่า
    จะลองนำไปศึกษาดูค่ะ

Similar Threads

  1. Inventory System Made In VB - Access With Code code
    By stepno01 in forum Visual Basic
    Replies: 0
    Last Post: 01-05-2009, 07:06 PM
  2. [E-BooK] Clean Code - มาทำ code ให้เรียบร้อยกันดีกว่า
    By singha.osot in forum E-Book, Video หรือบทความทั่วไปด้าน Computer
    Replies: 0
    Last Post: 07-12-2008, 07:35 PM
  3. Replies: 1
    Last Post: 09-02-2008, 12:26 AM
  4. Code หา image จาก code html มาฝาก
    By sut in forum PHP,ASP,Javascript, Html
    Replies: 1
    Last Post: 02-06-2007, 07:10 PM
  5. C++ code example
    By asylu3 in forum C/C++,C#,VC++,MFC,Win32
    Replies: 0
    Last Post: 28-02-2004, 12:15 AM

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •