【问题标题】:How is it possible to paint more than one rectangle on a glass pane?如何在玻璃板上绘制多个矩形?
【发布时间】:2012-03-30 04:18:45
【问题描述】:

我正在尝试在玻璃窗格上绘制一系列矩形,如here 中所述。问题是我的列表中只有最后一个元素显示在窗格上。

有谁可以在同一个窗格上绘制多个矩形?

以下是正在使用的代码:

面板类中的paint方法,扩展JComponent

protected void paintComponent(Graphics g) {
        if (point != null) {

            int value = this.getGradient();


            Color myColour = new Color(255, value, 0, 175);
            g.setColor(myColour);
            g.fillRect(point.x - 13, point.y - 15, this.width, this.height);

        }
    }

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • Graphics g = container.getGraphics(); 这很值得怀疑,不过我会等待 SSCCE 再发表评论。
  • +1,对于这个问题,让我学到了一些新东西:-)

标签: java swing glasspane


【解决方案1】:

除了剪裁边界之外,在玻璃窗格上绘画没有内在限制。例如,在MyGlassPane 中尝试以下操作。

protected void paintComponent(Graphics g) {
    if (point != null) {
        g.setColor(Color.red);
        g.drawRect(point.x, point.y, 60, 20);
        g.setColor(Color.blue);
        g.drawRect(point.x, point.y, 20, 60);
    }
}

【讨论】:

  • 非常感谢您的帮助,我并没有用这种方法绘制所有的矩形,但我调用了好几次......非常感谢:)
  • 很高兴你把它整理好了。如果您直接调用paintComponent(),请考虑改为调用repaint()
  • 很好,因为直到现在我都确信 Glass/RootPane 是关于绘画(API 和其他垃圾..)而不是绘画组件 :-),对,非常好
  • @mKorbel:我也必须看看。 :-)
猜你喜欢
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
相关资源
最近更新 更多