• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Problem with Drawing on a JPanel

 
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric, welcome to the Ranch!

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.
 
Bartender
Posts: 3766
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Eric,

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:
 
Paul Clapham
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:But they are empty.



No. Sorry about that, I missed the crucial line of code which makes them not empty. So what Piet said is more likely to be your answer.
 
Marshal
Posts: 68066
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

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.
 
Eric Madore
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 4492
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call repaint in the JPanel constructor.

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:
 
Piet Souris
Bartender
Posts: 3766
154
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Eric,

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?
 
Eric Madore
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!