简体   繁体   English

另一个类在框架中的JComponent repaint()

[英]JComponent repaint() in frame from another class

I am working for fun developing and old 80's draw 80 poker game. 我正在为开发有趣的游戏和80年代的平局80扑克游戏而工作。

class DDHGamePanel extends JPanel{
...etc...   
DDHAdvertising adv = new DDHAdvertising();
...etc...

 public void paintComponent(Graphics g) {
adv.isOptimizedDrawingEnabled();
adv.setEnabled(true);
adv.repaint();
 }
}

This is the main panel of my game. 这是我游戏的主面板。 I took out a lot of code to make this fit better. 我花了很多代码来使它更合适。 I have a class DDHAdvertising that has a paint Component. 我有一个DDHAdvertising类,具有绘画Component。

public class DDHAdvertising extends JComponent {

 public void paintComponent(Graphics g) {
           super.paintComponent(g);  
    drawAdvertisingBanner(g,getBanner1(),30,30);
    g.drawString ("Test",40,360);
  }
}

I want to be able to at the end of the paintComponent in my JPanel be able to repaint any class that extends JComponent with the repaint() method. 我希望能够在我的JPanel中的paintComponent的结尾处重新绘制使用repaint()方法扩展JComponent任何类。 I am certain that this can be done but I am not sure how to do it. 我敢肯定这是可以做到的,但是我不确定该怎么做。

I want all of my graphics class that have some component that reference the came, example would be say all of the cards that are draw to the screen. 我希望我所有的图形类都具有一些引用来的组件,例如说所有绘制到屏幕上的卡。 I want a separate class for each component on the screen and then to call its repaint() method which should invoke the paintComponent() method of that particular class. 我想要屏幕上每个组件的单独类,然后调用其repaint()方法,该方法应调用该特定类的paintComponent()方法。 I have read a lot on the subject but I have not seen this particular example in code. 我已经阅读了很多有关该主题的文章,但是我没有在代码中看到这个特定的示例。

What you want is super.paintComponent(g); 您想要的是super.paintComponent(g); in the paintComponent of the JPanel. 在JPanel的paintComponent中。 It causes all of its children to also be repainted. 它会导致所有子项也重新粉刷。 You should never remove this when you override a component (including JPanel). 当您覆盖组件(包括JPanel)时,永远不要删除它。

 public void paintComponent(Graphics g) {
     super.paintComponent(g);
     adv.isOptimizedDrawingEnabled();
     adv.setEnabled(true);
     adv.repaint();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM