Ben*_*Ben 2 java graphics paint jcomponent coordinate-systems
我正在覆盖paintComponentJComponent中背景的方法,一切顺利.
但是,我想从左下角而不是左上角开始绘画.
我需要改造什么,或者什么?
是的,您可以使用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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1523 次 |
| 最近记录: |