• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making different graphical objects  RSS feed

 
Jeremy Graham
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to make different graphical objects, but I've hit a snag. The master plan is to write my own cribbage program, so I want to have a green background (the convention for card games by Microsoft), but I can't seem to put anything on that background. Here's the code for the green background:



And here's the main:


And here's what I have for a blank card (in red because in my messing around trying to find a solution I ended up with a white background...):


So, where should I go from here?

Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeremy,

The problem is that you're adding both your Cardtable and your Blankcard to the JFrame. You should probably add your card to the table instead.

However, it's probably a better approach to draw your cards directly, instead of making them panels.

Just make Blankcard a simple class (don't let it inherit JPanel) and draw your cards in the paintComponent() method of Cardtable, after you've filled the background.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to create a CardTable class (at least not to set the background color). All you need to do is:



Now the background will be green no matter how big the frame is. You would then add your cards to this panel. Also you should never hard code a value like 1280 x 768. Not everybody uses the same resolution.

I do like your approach of using a component to represent each card. It makes it easier to handle mouse events.

You might find the Overlap Layout helpful to layout your cards.

 
Jeremy Graham
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not exactly sure how to draw the Blankcard on to the Cardtable... That's what got me stumped.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure but don't you need to override the paint method instead of the paintComponent method?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't you need to override the paint method instead of the paintComponent method?


Custom painting is done by overriding the paintComponent() method.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote: . . . Custom painting is done by overriding the paintComponent() method.
. . . and there is lots about custom painting in the Java™ Tutorials.

I think this thread would sit better on our GUIs forum. Moving.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!