【问题标题】:How to getSubimage of JPanel?如何获取 JPanel 的子图像?
【发布时间】:2012-07-15 05:07:04
【问题描述】:

BufferedImage 有一个返回 Image 的 getSubimage(x, y, width, height) 方法。我在 JPanel 上绘制图形,如何使用 JPanel 执行类似于 BufferedImage 方法的操作?

例如(如果它是正确的):

BufferedImage bi = jPanel.getSubimage(x, y, width, height);

谢谢

【问题讨论】:

    标签: java graphics jpanel bufferedimage


    【解决方案1】:

    您可以将任何摆动组件渲染到缓冲图像。

    由于您只需要一个子图像,我想这将是一个 2 步过程,1)将您的 JPanel 渲染为 BufferedImage,2)获取子图像。

    BufferedImage bi = new BufferedImage(jPanel.getWidth(), jPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D)bi.getGraphics();
    panel.printAll(g); //I assume you wanted child elements too... otherwise just use paint(g)
    BufferedImage result= bi.getSubimage(...);
    

    注意:这只有在所有组件都被初始化、定位等并显示后才有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多