开发者

Java2D negative position can not be displayed, move origin to bottom left

I am trying to draw lines on coordinates system in Graphics2D. However, I find out that the part on line in n开发者_开发技巧egative area can not be shown. Is there anyway I can make the lines in negative area be seen?

Also, is there anyway I can convert direct of y-axis from downward to upward?

Graphics2D g2 = (Graphics2D) g;
g2.scale(1, -1);
g2.translate(0, -HEIGHT);

Can't work. Object disappears.

Thanks!


Ah, you are using the HEIGHT attribute. You should be using getHeight().


The code below produces this screenshot (g2.drawLine(0, 0, 100, 100)):

Java2D negative position can not be displayed, move origin to bottom left

Code:

public static void main(String[] args) throws Exception {

    JFrame frame = new JFrame("Test");

    frame.add(new JComponent() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g.create();
            {
                g2.translate(0, getHeight() - 1);
                g2.scale(1, -1);

                g2.drawLine(0, 0, 100, 100);
            }
            g2.dispose();
        }
    });

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


As far as I understand Java2D you can't use negative coordinates. You always operate in the so-called "User Space" in Java2D. The translates coordinates of your position in "Device Space" might be negative, but this is invisible to you in Java. See also Java2D Tutorial - Coordinates and Graphics2D API.

You might be able to achieve what you want by subclassing Graphics2D and doing the those translation yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜