2013-05-14 602 views
-1

我想设置一个从(-1,-1)到(+1,+1)的数学坐标(其中y不增长),并使其适合窗口不管窗口大小如何。如何在Graphics2D中设置所需的坐标

我正在使用的Java SE 7的匿名JComponent的子类和铸造在paintComponent传入GraphicsGraphics2D,然后在Graphics2D

Graphics2D绘图设置为计算机坐标与大小的变化空间窗户。如何根据窗口大小重新调整它,并让Y向上?以下程序应在右上象限中显示一个黑色方块。

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

import javax.swing.JComponent; 
import javax.swing.JFrame; 

public class G { 
    public static void main (String [] args) { 
    JFrame frame = new JFrame(G.class.getCanonicalName()); 
    frame.setUndecorated(true); 
    JComponent component = new JComponent() { 
     private static final long serialVersionUID = 1L; 
     @Override 
     protected void paintComponent (Graphics g) { 
     super.paintComponent(g); 
     paint2D((Graphics2D)g); 
     } 
     protected void paint2D (Graphics2D g2) { 
     g2.draw(new Rectangle2D.Double(0.1, 0.1, 0.9, 0.9)); 
     } 
    }; 
    frame.add(component); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 400); 
    frame.setVisible(true); 
    } 
} 

回答

4

设置的坐标系统,您想怎么使用transform()translate()。所以:

  1. 你想要原点在(0,高度);左下方。
  2. 然后你想翻转Y轴。

示例代码:

AffineTransform tform = AffineTransform.getTranslateInstance(0, height); 
tform.scale(1, -1); 
g2.setTransform(tform); 

[我的编辑版本]:

public static void main (String [] args) { 
    JFrame frame = new JFrame(G2dTransform_Question.class.getCanonicalName()); 
    JComponent component = new JComponent() { 
     private static final long serialVersionUID = 1L; 
     @Override 
     protected void paintComponent (Graphics g) { 
      super.paintComponent(g); 
      paint2D((Graphics2D)g); 
     } 
     protected void paint2D (Graphics2D g2) { 
      AffineTransform tform = AffineTransform.getTranslateInstance(0, getHeight()); 
      tform.scale(getWidth(), -getHeight()); // NOTE -- to make 1.0 'full width'. 
      g2.setTransform(tform); 

      g2.setColor(Color.BLUE); // NOTE -- so we can *see* something. 
      g2.fill(new Rectangle2D.Double(0.1, 0.1, 0.8, 0.8)); // NOTE -- 'fill' works better than 'draw'. 
     } 
    }; 

    frame.setLayout(new BorderLayout()); // NOTE -- make the component size to frame. 
    frame.add(component, BorderLayout.CENTER); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 400); 
    frame.setVisible(true); 
} 

[气垫船的版本]:谢谢哈弗!

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

import javax.swing.JComponent; 
import javax.swing.JFrame; 

public class G { 
    public static final int PREF_W = 400; 
    public static final int PREF_H = PREF_W; 

public static void main (String [] args) { 
    JFrame frame = new JFrame(G.class.getCanonicalName()); 
    frame.setUndecorated(true); 
    JComponent component = new JComponent() { 
     private static final long serialVersionUID = 1L; 
     @Override 
     protected void paintComponent (Graphics g) { 
     super.paintComponent(g); 
     AffineTransform tform = AffineTransform.getTranslateInstance(0, getHeight()); 
     tform.scale(1, -1); 
     Graphics2D g2 = (Graphics2D) g.create(); 
     g2.setTransform(tform); 
     paint2D(g2); 
     g2.dispose(); 
     } 
     protected void paint2D (Graphics2D g2) { 
     g2.draw(new Rectangle2D.Double(10, 10, 20, 30)); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
     } 
    }; 
    frame.add(component); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setVisible(true); 
    } 
} 
+0

谢谢,但我不知道如何做到这一点。我应该使用getWidth()和getHeight()使[[-1,+ 1]'区间适合每个维度吗?我如何翻转?我不熟悉矩阵。有没有创建翻转转换的方法?或者我旋转两次?它本质上是具体的方法和供应的参数,我期待。 – necromancer 2013-05-14 02:22:14

+0

是的,我编辑了答案来提供一个例子。 – 2013-05-14 02:23:12

+0

谢谢,但它没有奏效。早些时候,我曾经像预期的那样在左上角出现一个小点。现在没有什么...... upvoted,但没有解决在右上象限中显示矩形的问题。 – necromancer 2013-05-14 02:32:28

相关问题