2016-12-04 114 views
0

我目前正在進行一個以地球小時爲主題的項目,我們只允許使用矩形,圓形和三角形。下面是我在特林創建圖像(不完全是,我的會更簡單!):如何使用簡單的方法在Java中旋轉矩形或三角形?

https://www.google.com/search?q=earth+hour&biw=1366&bih=586&source=lnms&tbm=isch&sa=X&ved=0ahUKEwj__5H0vtvQAhXLrlQKHTi8BagQ_AUIBygC#imgrc=fQkBxn0a8LnwbM%3A (不知道你是否能看到該鏈接)

但是,當我編碼它,我遇到了麻煩,將這些矩形旋轉到圓的切線上。我是一名剛學到java的基礎知識的學生,比如循環和數組。所以我的問題是,如果有一些可以理解的方式,不涉及一些可以旋轉矩形的複雜和奇特的方法?我知道它可能會涉及一些超出我的知識的複雜解決方案。但任何幫助非常感謝。

這是我垂直構建建築物站立到圓(大地)的代碼的一部分:

// create mid buildings 
Color blc = new Color(0, 0, 0); 
Rectangle midBld = new Rectangle(240, 220, 20, 40, blc); 
midBld.draw(g); 
Rectangle midBld1 = new Rectangle(242, 190, 16, 30, blc); 
midBld1.draw(g); 
Triangle midBld2 = new Triangle(250, 160, 8, 30, blc); 
midBld2.draw(g); 
Triangle midBld3 = new Triangle(250, 160, -8, 30, blc); 
midBld3.draw(g); 
+0

你可以把你的代碼,讓任何人都可以幫助你嗎? – esprittn

+0

是的。我已經把代碼的一部分,我用於垂直 –

回答

0

Rectangle不能轉動,其邊緣總是在平行於軸線。但是你可以旋轉和平移繪製形狀的座標系。來自Graphics2D API文檔。

傳遞給Graphics2D對象的所有座標都在獨立於設備的稱爲用戶空間的座標系中指定,該座標系由應用程序使用。 Graphics2D對象包含一個AffineTransform對象作爲其渲染狀態的一部分,該對象定義瞭如何將座標從用戶空間轉換爲設備空間中與設備相關的座標。

Graphics2D還提供了兩個方法,在這個任務中是有用的:translate即移動座標和rotate,好吧,旋轉系統的原點。

package graphics; 

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

/** 
* Earth Hour 
*/ 
public class RotateRect extends JFrame { 

    private static final int WIDTH = 400; 
    private static final int HEIGHT = 400; 

    public RotateRect() { 
     this.setSize(WIDTH, HEIGHT); 
     this.setTitle("Rotate Rectangles"); 
     this.setContentPane(new JPanel() { 
      @Override 
      public void paint(Graphics g) { 
       Graphics2D g2 = (Graphics2D) g; 

       // Background: White 
       g2.setColor(Color.WHITE); 
       g2.fillRect(0, 0, this.getWidth(), this.getHeight()); 

       // Draw "Earth": Center(200, 400), Radius=200 
       g2.setColor(Color.BLACK); 
       g2.fillOval(0, 200, 400, 400); 

       // Move origin to center of the canvas (surface of earth) 
       g2.translate(200, 200); 
       // Rotate the coordinate system, relative to the center of earth. 
       // note x, y are in the translated system 
       // Transforms are accumulative 
       g2.rotate(-Math.PI/6, 0, 200); 
       // Fill a rectangle with top-left corner at (-20, 80) in the rotated system 
       // It's important to make the rectangle symmetrical to the y-axis, otherwise the building looks 
       // funny. 
       // Also, make the building "sunk" a little, so that it's fully on the ground. 
       g2.fillRect(-20, -80, 40, 100); 

       g2.rotate(Math.PI/3, 0, 200); 
       g2.fillRect(-20, -80, 40, 100); 

       g2.rotate(-Math.PI/6, 0, 200); 
       g2.fill(new Rectangle(-20, -80, 40, 100)); 
      } 
     }); 
    } 

    public static void main(String [] args) { 
     RotateRect rr = new RotateRect(); 
     rr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     EventQueue.invokeLater(()->rr.setVisible(true)); 
    } 
} 
+0

Thansks男子,這絕對幫助我理解,我會去弄明白這個了!順便說一下,我打算在「地球」上建立雙方建築物,有沒有辦法有效地建造它們,比如使用陣列或其他東西? –

+0

「矩形不能旋轉」:哦,是啊??檢查你的寶貝來源;這是做什麼? affineTransform.createTransformedShape(Shape pSrc);再次延續另一個廢話 – gpasch

+0

技術上你得到的不再是一個「矩形」實例。 –