2012-03-14 25 views
1

我已經開始了它,但我不知道如何把一些數字放在時鐘上。估計線是如此痛苦。 = /任何想法如何做到這一點更容易和更快? x-x這裏是我的代碼。如何使用鞦韆製作更好更快的掛鐘?

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

public class DrawingProject extends JFrame 
{ 
    public static void main(String[] args) 
{ 
     DrawingProject frame = new DrawingProject(); 
     frame.setSize(750, 750); 
     frame.setVisible(true); 
} 

public void paint(Graphics g) 
{ 
    g.drawOval(170, 170, 300, 300); 
    g.drawLine(235, 235, 330, 330); 
    g.drawLine(235, 235, 237, 257); 
    g.drawLine(235, 235, 257, 237); 
    g.drawLine(330, 255, 330, 330); 
    g.drawLine(330, 255, 345, 270); 
    g.drawLine(330, 255, 313, 270); 


} 
} 
+6

你是什麼意思的快速時鐘?如果它是一個時鐘,它的意思是與其他時鐘一起運行,但是如果它運行得很快,它會成爲任何使用的時鐘嗎? :-)更好地把你的擺動時鐘放在太空中,這將使其運行速度比其他地面上快:-) – 2012-03-14 14:15:03

+2

更快的時鐘?就像一個可以在短短900毫秒內完成第二個任務的人? ;) – 2012-03-14 14:15:53

+0

*「更好更快的掛鐘......」*如果'定位'更好,'編碼'更快,也許解決的辦法就是把它變成數字時鐘。 ;) – 2012-03-14 14:19:20

回答

2

使用polar coordinates來實現數字的定位。一個完整的圓有360度。有12個小時,這意味着每30度從角度0開始必須放置一個數字。

Here's a full example

+0

掛鐘不一定要移動。只是一個設計。 ._。有點混淆所有這些守則和意見。 X-X – 2012-03-14 14:53:29

3

考慮使用polar coordinates。有了它,你可以將每個點都作爲距離中心(總是相同)和旋轉角度(每增加一個數字)的距離。

2

首先,它的錯誤做法是使用hard coded座標繪製您的線條。特別是如果這些座標可以使用一些簡單的幾何來計算。點擊提供給您的第一個鏈接,我們看到這些公式:

// polar to Cartesian 
double x = Math.cos(angleInRadians) * radius; 
double y = Math.sin(angleInRadians) * radius; 

// Cartesian to polar. 
double radius = Math.sqrt(x * x + y * y); 
double angleInRadians = Math.acos(x/radius); 

來源:mindprod.com

前兩行做的正是你想要的!他們以一定的角度計算軸上行進的距離。我們可以使用這些公式來計算線條的目標點。對於起點,我們將使用框架的中心。

現在讓我們把這些公式爲一些方法:

private int calculateHorizontalOffset(int angle, int radius) { 
    return (int) Math.round(Math.cos(Math.toRadians(angle)) * radius); 
} 

private int calculateVerticalOffset(int angle, int radius) { 
    return (int) Math.round(Math.sin(Math.toRadians(angle)) * radius); 
} 

現在它很容易計算出你的線條的座標,這裏的最終結果:

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

public class DrawingProject extends JFrame { 

    /** 
    * This is the size of our clock 
    */ 
    int radius = 300; 

    public static void main(String[] args) { 
     DrawingProject frame = new DrawingProject(); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setSize(750, 750); 
     frame.setVisible(true); 
    } 

    private int calculateHorizontalOffset(int angle, int radius) { 
     return (int) Math.round(Math.cos(Math.toRadians(angle)) * radius); 
    } 

    private int calculateVerticalOffset(int angle, int radius) { 
     return (int) Math.round(Math.sin(Math.toRadians(angle)) * radius); 
    } 

    public void paint(Graphics g) { 

     int offsetX, offsetY; 
     //here we calculate the center of our drawing pane 
     int centerX = getWidth()/2; 
     int centerY = getHeight()/2; 

     //then we draw a circle using the data we have so far 
     g.drawOval(centerX - radius/2, centerY - radius/2, radius, radius); 

     //then, with our formulas, we create a line to the left (angle = 0) 
     offsetX = calculateHorizontalOffset(0, radius/2 - 50); 
     offsetY = calculateVerticalOffset(0, radius/2 - 50); 
     g.drawLine(centerX, centerY, centerX + offsetX, centerY + offsetY); 

     //and a second line to the top (angle = 270) 
     offsetX = calculateHorizontalOffset(270, radius/2 - 10); 
     offsetY = calculateVerticalOffset(270, radius/2 - 10); 
     g.drawLine(centerX, centerY, centerX + offsetX, centerY + offsetY); 
    } 
} 

另一個技巧,默認關閉操作的JFrame是HIDE_ON_CLOSE。只是隱藏框架通常是不夠的。我們希望在窗口關閉後立即關閉應用程序。我爲此使用了EXIT_ON_CLOSE。