【问题标题】:Java JComponent - start painting from lower left corner?Java JComponent - 从左下角开始绘画?
【发布时间】:2011-07-26 10:29:36
【问题描述】:

我正在为 JComponent 中的背景覆盖 paintComponent 方法,一切顺利。

但是,我想从左下角而不是左上角开始绘画。

我需要改变什么,还是什么?

【问题讨论】:

    标签: java graphics paint coordinate-systems jcomponent


    【解决方案1】:

    是的,您可以使用AffineTransform 从左下角绘制:

    代码:

    public static void main(String[] args) {
    
        JFrame frame = new JFrame("Test");
    
        frame.add(new JComponent() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                Graphics2D g2d = (Graphics2D) g;
    
                // save the "old" transform
                AffineTransform old = g2d.getTransform();
    
                // update graphics object with the inverted y-transform
                g2d.translate(0, getHeight() - 1);
                g2d.scale(1, -1);
    
                // draw what you want
                g2d.drawLine(0, 0, 300, 200);
    
                // restore the old transform
                g2d.setTransform(old);
            }
        });
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }
    

    【讨论】:

    • 谢谢 看起来它有效,除了我的图像是颠倒的 :/ 。仅供未来用户使用 AffineTransform 位于java.awt.geom.AffineTransform
    • 是的,转换转换对图形对象的任何调用,如果您想使用“默认”转换绘制任何东西,您需要在更改它之前执行它(在调用 @987654325 之后@)
    猜你喜欢
    • 2014-02-28
    • 2014-07-26
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多