• Post Reply Bookmark Topic Watch Topic
  • New Topic

repaint() problem

 
Fasih Awan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi so I've been doing a school assignment but I can't seem to figure out why my repaint command isn't calling the paintComponent.

I declare an object for a class with a value of 52, which then uses an arraylist and calls another class, and in that class the data fields are assigned a value, and then repaint is called, but it won't call the paint component.

Here is bits of the code

 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first place, calling repaint() in the constructor is meaningless as the component isn't realized until added to a realized hierarchy (or when the hierarchy to which ist is added is realized).

In addition, calling repaint() on a component that has a size of [0, 0] doesn't do anything as the Swing subsystem is smart enough to not attempt to paint a component that doesn't occupy any screen real estate.

Finally, don't load images or perform any other disk I/O inside a painting method. Preload any images that you require.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unrelated to Swing, your series of if-else blocks can be compactly written asUsing a zero-based approach would get rid of the -1 / +1 manipulations.

Still better would be to use enums for rank and suit, but you may not have covered those yet.
 
Fasih Awan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well when I tried this before loading images worked fine in the paintComponent. Also, where does it call on the paintComponent with [0,0]? What do you suggest I do?
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First tell me: what do you expect to achieve by a repaint() call in the constructor?

When the component is first shown, it will be painted.

edit
Well when I tried this before loading images worked fine in the paintComponent.

Do you realize that you don't have any control over when and how often a painting method is called? Do you really want to tie up your disk and slow down the painting process by repeatedly loading the same resource?
 
Fasih Awan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I call the class with the number 52, I want it to repaint 52 cards. A deck.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how does that prevent you from loading the image in the constructor?
 
Fasih Awan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not. What is another option? Have a method show that repaints?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!