JAVA can't paint onto JFrame from within another class

前端 未结 1 427
南旧
南旧 2021-01-16 23:00

I am aware this is my error. My question is why isn\'t this working what am i missing i can call this is i put it a method instead of a class so i am assuming theirs somethi

相关标签:
1条回答
  • 2021-01-16 23:55

    Your problem is that you're misusing inheritance. Your JVMDiagram is extending JVMComponent and it shouldn't. Yes, you gain JVMComponent's getWidth() and getHeight() method, but they don't mean anything since JVMDiagram isn't being added to the GUI as a component, shouldn't be added as a component, and it has 0 height and width (print them out).

    Rethink your design, don't use inheritance for this. Instead pass in values from one object to the other if needed. For instance, create a JVMDiagram field within JVMComponent and initialize it. Pass in the width and height with the Graphics2D in the JVMDiagram draw method in JVMComponent's paintComponent method.

    Side issue: never call repaint() from within a painting method or from code that is called from within a painting method.

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