【问题标题】:JPanel does not paint when scaling with Affine Trasform使用仿射变换缩放时 JPanel 不绘制
【发布时间】:2012-04-11 13:44:33
【问题描述】:

我的应用程序布局类似于: 自定义 JFrame(仅处理 gui 的创建),其中包含一个标准 JPanel,其中包含一个自定义 JPanel

在自定义的 JPanel 内部,称为 MinimapPanel,我更改了绘制方法:

    //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 都会保持空白。

关于我可能做错的任何想法?

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 感谢您的提醒!

标签: java swing scaling paint affinetransform


【解决方案1】:

不要使用清晰的变换。以前的组件添加自己的转换,例如translate() 到子位置等。而是像这样调用代码

AffineTransform old=g2d.getTransform();
//do your changes here
g2d.scale(scaleAmount, scaleAmount);
//paint
g2d.setTransform(old);

【讨论】:

  • 感谢您的提醒,我对 Swing 还是很陌生。实际解决问题的是使用“g2d.scale(amount, amount)”而不是“g2d.setTransform(trasform)”知道为什么吗?
  • 正如我所说,JComponents 在绘制子项之前使用 translate() 将转换位置更改为 0(对于每个子项的 0)。
猜你喜欢
  • 2014-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 2010-12-17
相关资源
最近更新 更多