2016-07-04 48 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条平行线的效果

虽然我觉得我可能在数学的某个地方犯了错误(这看起来有点奇怪),但这应该会给你一个想法。