【问题标题】:swing object in front of other objects在其他物体前面摆动物体
【发布时间】:2015-02-01 05:29:15
【问题描述】:

我听说swing默认是双缓冲的。我不想让摆动双缓冲。我正在使用双缓冲,我想添加一些摆动对象(现在只是一个添加到 JFrame 中的 JButton)。

问题是在我渲染其他内容后调用框架的绘制、重绘或paintComponents 方法会擦除其他内容的视觉效果。在渲染其他东西之前调用这些方法会导致其他东西出现在摆动对象的前面(现在只是一个 JButton)。直接对JPanel做同样的事情似乎没有效果。

我相信我需要一种方法来绘制没有默认背景(灰色)的 Jframe 或添加到其中的 JPanel,这会导致窗口在设置为 Color(0,0,0,0) 时变为空白。

public void paint() {
    // uses double buffering system.
    do {
        do {
            Graphics2D g2d = (Graphics2D) bufferStrategy.getDrawGraphics();
            g2d.fillRect(0, 0, frame.getWidth(), frame.getHeight());
            try {// frame.paintComponents(g2d); // calling it here draws buttons to behind of the object
                rendering(g2d); // I draw other objects in this method
                // frame.paintComponents(g2d);// calling it here makes other objects disappear

        } catch (NullPointerException e) {
                e.printStackTrace();
        }
            g2d.dispose();
            } while (bufferStrategy.contentsRestored());
        bufferStrategy.show();
     } while (bufferStrategy.contentsLost());
}// method end 

这就是我设置按钮的方式:

private void setUpGUI() {
    panel = new JPanel();

    LayoutManager layout = new FlowLayout();
    panel.setLayout(layout);
    panel.setOpaque(false);//this does not seems to have any effect
    panel.setBackground(Color.yellow);//this does not seems to have any effect. this is just for testing

    JButton but1 = new JButton("but1"); 

    panel.add("but1", but1);
    frame.add(panel);

}

编辑: 这是解决方法/修复:

新的绘制方法:

public void paint() {
    // uses double buffering system.
    do {
        do {
            Graphics2D g2d = (Graphics2D) bufferStrategy.getDrawGraphics();
            g2d.fillRect(0, 0, frame.getWidth(), frame.getHeight());
            try {
                frame.paint(g2d); 

        } catch (NullPointerException e) {
                e.printStackTrace();
        }
            g2d.dispose();
            } while (bufferStrategy.contentsRestored());
        bufferStrategy.show();
     } while (bufferStrategy.contentsLost());
}// method end 

我已经覆盖了面板的绘制方法:

    @Override
    public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            Main.main().rendering(g2d);

            super.paint(g);
            g.dispose();
            g2d.dispose();
        }
    } 

【问题讨论】:

  • 那么你应该覆盖update(Graphics)并防止swing清除你的组件
  • @msrd0 我试过了。现在显示面板的黄色背景。我已经设置了 panel.setOpaque(false)。之后只有我的按钮显示,其他对象在灰色背景下丢失

标签: java swing jframe jbutton paintcomponent


【解决方案1】:

无需创建自己的BufferStategy!!!

如果您想进行自定义绘制,请覆盖 JPanel 的 paintComponent() 方法,然后将面板添加到框架中。阅读 Custom Painting 上的 Swing 教程部分,了解更多信息和示例。

您仍然可以像添加任何其他面板一样向该面板添加组件。面板的布局管理器将定位组件。

panel.setOpaque(false);
panel.setBackground(Color.yellow);

setBackground(...) 什么都不做,因为你说面板不是不透明的,这意味着父组件的背景将被绘制。

 panel.add("but1", but1);

这不是您将组件添加到面板的方式。正确的使用方法是:

panel.add(but1);

由于您的面板使用的是 FlowLayout,因此无需指定约束。

【讨论】:

  • 感谢您的回复。为什么 BufferStrategy 不是必需的?我在除 jpanel 之外的程序中有动态图片。根据您给出的链接:paint() invokes paintComponent(). If ui is non-null, paintComponent() invokes ui.update(). If the component's opaque is true, ui.update() fills the component's background and invokes ui.paint(). ui.paint() renders the content of the component. 我做了panel.setOpaque(false) 我已经覆盖了框架和面板的更新方法(什么都不做),但是一些灰色(面板和框架的背景颜色都不是)背景清除了其他对象
  • Why is BufferStrategy is not necessary? - Swing 默认是双缓冲的。 I've overridden update methods - 你不应该重写 update() 方法,这是旧的 AWT 代码,没有在 Swing 中使用。如果您需要更多帮助,请阅读教程并发布正确的SSCCE 来演示问题。您对问题的口头描述不足以给出具体的答案,这就是为什么我只给您一个笼统的答案。
  • 我已经修复了它(请参阅问题中的编辑代码),但我很困惑。我已经覆盖了面板的绘画并从那里调用了渲染,它可以工作,但绘画很慢。我正在画一些瓷砖,当瓷砖移动时,它们会错位,直到它们停止移动。以前没有这样的问题,所以我使用了缓冲策略。我的问题是:如果不需要缓冲策略,为什么不使用它会减慢绘画速度并导致视觉错误? frame.createBufferStrategy(2) 减少了视觉伪影,但并未完全修复。它被新的paint()方法postimg.org/image/r7w8qb217它修复了
  • I've overridden panel's paint - 你读过教程还是我的答案?两者都说你应该重写paintComponent()。此外,您不应该处置 Graphics 对象。该对象被框架的所有组件使用。根据几行代码我不知道你为什么会出现问题。
  • 我已阅读本教程,但无法使其与覆盖的油漆组件一起使用。无论如何,感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
相关资源
最近更新 更多