Hello, having problems as noted above. Trying to make a game that randomly generates a dungeon. The commented part of the code below works. I am trying to change the code to
draw a starting chamber automatically instead of relying on a mouse click to start the process. But when I alter the code to what is shown below, the window appears, but the
background color does not change and the chamber does not appear. The DungeonFeature and Exit arraylists hold objects of classes that extend DungeonPanel.
In that code you create an empty feature list and an empty exit list in the constructor of the JPanel. And when you paint the panel, you draw something for every element in those lists. But they are empty. So nothing happens.
just a quick question: I do not see you setting any size for the JPanel. The panel also does not contain any JComponents like JButtons or JLabels. Chances are that the calculated size of the panel will be 0, and therefore you see nothing. This would also be the case if you add a MouseListener.
Can you show the code snippets that add the panel to the frame and that make the frame visible? You can also add this method to the JPanel:
I recommend you give testStart() private access; you probably don't want that method called twice. Only call a method from the constructor if it is marked final or private. It isn't necessary to use both modifiers.
posted 2 weeks ago
Thank you for the responses! I am sorry, I was concerned the way I posted these with the commented code would be confusing.
When I run the code with the commented part uncommented, the mouseListener works fine. The program draws the graphic appropriately.
But when I run the program WITHOUT the mouseListener I cannot get the program to draw the StartChamber directly. The window appears,
but the background color is not set appropriately, and the starting chamber is not drawn. Not sure if I explained it better?
Thanks again for the help!
This is the JFrame class that adds the panel
Here is the StartChamber class that constructs the first room
You don't want to attempt to repaint the JPanel before it's even been constructed, after all.
I think you may need to call pack as well, though not so sure about that part as you only have the one panel.
And, to be honest, I wouldn't make the frame visible before you've actually added anything to its content.
I would have the order as:
posted 2 weeks ago
thanks for the additional code. It is clear to me now what happens, apart from your story about the MouseListener.
My first suspicion was as I wrote: the panel has size 0, since you do not explicitely set any size. However, you add the panel to the contentpane of the frame. That contentpane has a BorderLayout, and that scales its components. That makes that your panel gets the size of that contentpane, that has the size of the frame. If you had thisline of code before you add the panel, then you would see what I mean:
Since you didn't show some classes, I stripped down your code to the bare minimum, and ran it. I see no difference in behaviour whatsoever, whether I add the panel after the frame was made visible, or whether I add that MouseListener to the panel or not, or that you do a repaint() in the constructor. So, I can't repeat the effect that you described.
To make things a little more clear, I gave the panel a preferred size, I gave the contentpane a FlowLayout, so that you can experiment freely with the code (change sizes, add or remove the mouselistener, experiment with different Layoutmanagers for the contentpane, et cetera.
Here is the much stripped doen code that I used.
One last remark. You have your class 'GraphicsExperiment' extend JFrame. If you give it a second look, do you think that this extending is necessary?
I hate signatures!
posted 2 weeks ago
Thank you all!! Problem Solved, and each of you had me think of a different aspect. So thanks to all of you!
The solution was to place the call to draw the first chamber in the JFrame class. And as mentioned I did not need to extend JFrame.
Also, I revised the method for placing the StartChamber in the Arraylist. I needed to cast it to DungeonFeature as the arraylist was expecting an object of that class.
Revised code as below:
You can't expect to wield supreme executive power just because