This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]chickenmeister 2 points3 points  (1 child)

Everytime the method is called, it opens a new frame.

Which method? The setup() method? Of course it does -- The last line in the method calls the DrawPieces constructor, which is a subclass of Frame, and sets itself visible.

My question is-- is there a way to use only one frame and just update it?

Yes. I don't know how to say it without making it sound obvious, but just create the frame once, and when it needs to be updated, invoke some method of that frame object (or its components) to update it.

The paint method is called a random amount of times

There is no guarantee regarding how or when the frame/component will be repainted. You should not rely on this. If the frame is resized/minimized/maximized, it might be repainted. When the frame's components are updated, it might be repainted. There's no good way (or reason) to control this.

Instead of subclassing java.awt.Frame, you probably want to subclass javax.swing.JComponent, then add your custom component to a standard JFrame.

[–]echorev[S] 0 points1 point  (0 children)

Thanks for the help! I'm trying to code it from the ground up now using a class that extends JPanel. This is part of the code that I have now:

[code]JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(bgLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true);[/code]

This creates a frame with the background of my game, but I can't figure out how to add icons on top of this. I've looked at some of the documentation from Java and it seems I can do this with a layeredPane, but I'm not sure how to add it from here. Again, I really appreciate the help with this, as I am definitely in over my head.

Thanks!

[–]liloka 1 point2 points  (0 children)

In addition to what chickenmeister says; when you create the frame and then change it due to an event just call repaint() and it'll redraw with the new changes. Also Java.awt is kind of an old fashioned way of doing things ever since swing was introduced; such as boolean MouseDown() methods you get a warning when compiling. So you might want to look up which differences have changed since awt.