简体   繁体   中英

JComponent repaint() in frame from another class

I am working for fun developing and old 80's draw 80 poker game.

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.

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. 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. 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); in the paintComponent of the JPanel. It causes all of its children to also be repainted. You should never remove this when you override a component (including JPanel).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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