Java JComponent - 从左下角开始绘画?

标签 java graphics paint coordinate-systems jcomponent

我正在重写 JComponent 中背景的 paintComponent 方法,一切进展顺利。

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

我需要改变一些东西吗?

最佳答案

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

Screenshot

代码:

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);
}

关于Java JComponent - 从左下角开始绘画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6828654/

相关文章:

java - 如何在我的 Java 小程序中使用 paint() 方法之外的图形?

c# - UserControl 的 BeginUpdate() EndUpdate

c++ - 高效渲染频谱图可视化

java - hibernate : Java Application must be restarted for data to be realoaded

java - 部署tomcat 7时出现PermGen Space错误?

java - 在spring boot的管理端口使用prometheus metrics servlet

opengl - 在 opengl 中,为什么我们必须在 gluLookAt 之前执行 gluPerspective?

Java 在同一个 JFrame 中绘制多个正方形

java - 闪烁太多

java - 为什么当 zookeeper 重新上线时 curator 没有恢复?