Java - Calling paint method from different class?

后端 未结 1 1983
挽巷
挽巷 2020-12-20 05:18

I have a class which extends JFrame and creates a window and it needs to call the paint() method which is in a different class. I understand that if they were in the same c

相关标签:
1条回答
  • 2020-12-20 06:13

    If I understand your question you could pass the Die instance into the Game constructor with something like

    public class Game extends Frame {
      private Die die;
      public Game(Die die) {
        this.die = die;
      }
      public void window() {    
        setTitle("Roll");   //  Title of the window
        setLocation(100, 100);          //  Location of the window
        setSize(900, 600);              //  Size of the window
        setBackground(Color.lightGray); //  Color of the window
        setVisible(true);               //  Make it appear and call paint
        die.setVisible(true);           //  The same
      }
    }
    

    Then, wherever you call new Game() you add the Die instance argument. This is a fairly common way to implement a callback in Java (and other OOP languages).

    0 讨论(0)
提交回复
热议问题