开发者

Java JComponent - start painting from lower left corner?

I'm overriding the paintComponent method for a background in a JComponent and all is going well.

However, I want to start painting from the lower left cor开发者_开发技巧ner instead of the upper left.

Do I need to transform something, or what?


Yes, you can use an AffineTransform to draw from the lower left corner:

Java JComponent - start painting from lower left corner?

Code:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜