I was wondering if you could help me out with the structure of my program. I'm making a game with Java and I have 2 classes. 1 class is a graphics class that extends JFrame with a paint method:

Graphics Class paint() and calls various objects that use JFrame as a parameter. Inside the class is a subclass that implements MouseListener and Mouse Motion Listener which interacts with paint method.

Canvas Class I was planning to embed the first class into a larger canvas class that would have buttons, panels, and so forth so essentially use the class as a smaller (sub-program) inside a larger program. Is this possible/ am I doing this the right way. Whenever I try to call the graphic class inside the canvas class, the graphic doesn't turn up.

    public Canvas(){
    graphic = new Graphic(name);

}
/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Canvas canvas = new Canvas();
            canvas.setDefaultCloseOperation(EXIT_ON_CLOSE);
            canvas.setSize(1500, 1500);
    canvas.setVisible(true);
}

} Any help/guidance would be appreciated. I just want to mainly to know if I'm thinking about graphic programming correctly.

有帮助吗?

解决方案

Canvas is not "Window". In order to display anything on the screen, you components must be added to a window delegate.

I would, personally, avoid overriding the paint methods of top level containers. The main reason for this is top level containers are not double buffered and it makes your application less portable. It's much easier to add a component to other components, a frame is pretty final.

I would also use JPanel over Canvas, simply because JPanel is double buffered.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top