我的应用程序布局是这样的: 自定义JFrame(它只处理gui的创建),其中包含一个包含自定义JPanel的标准JPanel
在自定义JPanel(名为MinimapPanel)中,我更改了paint方法:
//in a constructor:
scaledTransform = new AffineTransform(); = new AffineTransform();
scaledTransform = new AffineTransform();
scaledTransform.scale(scaleAmount, scaleAmount);
//...
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setTransform(scaledTransform);
mapDrawer.paintMinimap(g, seatMap, miniViewHandler);//it just calls a bunch of fillRect
if (viewRect != null) {
g.setColor(new Color(255, 0, 0));
int x = viewRect.x;
int y = viewRect.y;
g.drawRect(x, y, Math.abs(viewRect.width), Math.abs(viewRect.height));
}
g2d.setTransform(plainTransform);
}
如果我不应用trasform,或者缩放是1.0(无),一切正常,但如果我缩放,每次JFrame重新绘制时,MinimapPanel都会保持空白。
关于我可能做错的任何想法?
答案 0 :(得分:5)
不要使用明确的变换。以前的组件添加自己的转换,例如translate()
到子位置等。而是像这样调用代码
AffineTransform old=g2d.getTransform();
//do your changes here
g2d.scale(scaleAmount, scaleAmount);
//paint
g2d.setTransform(old);