2016-07-04 49 views
2

下面的代碼,我使用旋轉兩個矩形的代碼如下旋轉兩條平行線,以創建一個X

Graphics2D g2d = (Graphics2D) g; 
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
g2d.setColor(Color.WHITE); 

//r1 
Rectangle2D r1 = new Rectangle2D.Double(0, 0, 50, 4); 
g2d.rotate(Math.toRadians(45)); 
g2d.fill(r1); 

//r3 
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4); 
g2d.rotate(Math.toRadians(-90)); 
g2d.fill(r3); 

這創造的東西,看起來像這樣

enter image description here

而我正試圖創造一些看起來像這樣的東西

enter image description here

發生這種情況是因爲矩形旋轉時,它們都圍繞點0,0旋轉。爲了解決這個問題,我嘗試使用rotate(double theta, double x, double y)。但是,我無法正確使用它。例如,當我試圖

Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4); 
g2d.rotate(Math.toRadians(-90), 25, 25); 

Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4); 
g2d.rotate(Math.toRadians(-90), 0, 25); 

我得到類似意外的結果當兩個矩形正在圍繞點0,0旋轉。如果能解決我的問題,我將不勝感激。

如果你想知道爲什麼我已經做了這個樣子,這是因爲我希望能做出類似的,當你點擊由我完成編碼圖形

回答

0

所以事實證明,這可以用一些相對簡單的數學來完成。由於我試圖製作的形狀是完美的X.

要計算矩形的位置,我們可以使用Pythagorean theorem

enter image description here

上面的圖象顯示了兩個步驟。

Translation [2√2, 0] from the point [0, 0] 
Rotate 45deg from the point [2√2, 0] 

接下來我們需要計算出這個矩形的最小點。我們再次可以使用Pythagorean theorem

enter image description here

這告訴我們在第二個矩形的頂點將

Difference in height: 4 - 2√2 
Bottom of line when straight: [0, 27√2 + (4 - 2√2)] = [0, 4 + 25√2] 
Top of line when straight: [0, 25√2] 

最後,我們可以把過去的矩形開始[0, 0]

Translation [0, 25√2] from the point [0, 0] 
Rotate -45deg from the point [0, 25√2] 

現在,理論是不可行的,這在代碼中看起來像什麼?它看起來類似於下面的代碼

//Values 
final static double[] r1Points = {2.828427125, 0}; //Equivilant 2√2 
final static double[] r3Points = {0, 35.35533906}; //Equivilant 25√2 
final static int[] widthNHeight = {50, 4}; //Width then height 
final static double angle = 45.0; //Angle to rotate lines 

//Declaring the rectangles 
Rectangle2D r1 = new Rectangle2D.Double(r1Points[0], r1Points[1], widthNHeight[0], widthNHeight[1]); 
Rectangle2D r3 = new Rectangle2D.Double(r3Points[0], r3Points[1], widthNHeight[0], widthNHeight[1]); 

//r1 
g2d.rotate(Math.toRadians(angle), r1Points[0], r1Points[1]); //Rotates graphic for first rectangle 
g2d.fill(r1); 

//r3 
g2d.rotate(Math.toRadians(-angle), r1Points[0], r1Points[1]); //Rotates the graphic back to straight 
g2d.rotate(Math.toRadians(-angle), r3Points[0], r3Points[1]); //Rotates graphic for second rectangle 
g2d.fill(r3); 
2
package test; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.AffineTransform; 
import java.awt.geom.Rectangle2D; 

import javax.swing.*; 

public class Cross extends JPanel { 

    private Rectangle2D rectangle; 

    Cross() { 
     rectangle = new Rectangle2D.Double(0, 0, 50, 4); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setColor(Color.red); 
     g2.fillRect(0, 0, getWidth(), getHeight()); 

     g2.setColor(Color.white); 
     AffineTransform at = g2.getTransform(); 
     g2.translate(5, 5); 
     g2.rotate(Math.toRadians(45)); 
     g2.fill(rectangle); 

     g2.setTransform(at); 
     g2.translate(5, 5 + Math.sqrt(2) * 25); 
     g2.rotate(Math.toRadians(-45)); 
     g2.fill(rectangle); 

     g2.setTransform(at); 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("Cross"); 
     frame.add(new Cross()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(128, 128); 
     frame.setVisible(true); 
    } 



} 
時看到 here 3條平行線的效果

雖然我覺得我可能在數學的某個地方犯了錯誤(這看起來有點奇怪),但這應該會給你一個想法。