• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java graphics image not working  RSS feed

 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello i am making a simple java animation of a cloud moving from left to right , but problem is sometime the cloud is drawn on the screen and sometime a blank panel appears






what is going on . why this is happening . can i have some consistency ,
am i doing something wrong , i am new to java graphics .
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUIs forum.

Why are you using the paint() method. You should leave that alone and use paintComponent(). Override it with protected access and don't call it directly.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
siddarth, I believe that Campbell's advice will solve your problem. Swing double-buffers JPanels. I am guessing the problem is that you are painting your cloud after your call to super.paint(). If you do your drawing in paintComponent(), the JPanel's native paint() method will call it at the right time to be sure its effects reach the screen.

By the way, it is very common for new Swing coders to override JPanel's paint() method, and to then be told they should never override JPanel's paint() method. "Never" is a strong word. My own view is that you should hardly ever override it. From the javadoc:
Those Who Live On Mt. Olympus wrote:public void paint(Graphics g)
...
Subclasses can just override this method, as always. A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent.

So, they are giving you Campbell's advice: use paintComponent if you're just adding some specialization to your display (which is what you seem to be doing).

When, then, would you override paint()? Well, I do it in my code quite a bit, because I'm displaying live-action video that occupies the entire display rectangle, filling every pixel on every call to paint(). My JPanel has no components and no border it needs to draw. It's a minor enhancement, but it leaves me in total control of the entire display area. In pretty much any other scenario, I'd say don't override paint().
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:siddarth, I believe that Campbell's advice will solve your problem. Swing double-buffers JPanels. I am guessing the problem is that you are painting your cloud after your call to super.paint(). If you do your drawing in paintComponent(), the JPanel's native paint() method will call it at the right time to be sure its effects reach the screen.


hello , i have changed my paint method to



and in my main class



but again sometime my cloud appears and sometime not , am i doing something wrong ?
 
Piet Souris
Rancher
Posts: 1983
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes to overriding 'paint' or 'paintComponent', I think Stevens said it all.

Another aspect is that every component should be responsible for its own size.
That means: don't use 'jframe.setSize', but use 'jframe.pack()'. In order for that to
work, the drawingPanel must indicate its own size.

The sequence of commands in your setup is indeed important. Before issuing
'setVisible' and 'pack', make sure that all components are added.

I've slightly changed your code where necessary (I left the 'paint' intact, to show
that it works, in this case). I see the image moving left to right, continuously.
I've added, in 'main', a line of code that will make the rest of your
code to run on the EDT, and I used my own image, for testing purposes.

 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
siddarth, try taking that super.paint() call in your paintComponent() method out. The JPanel's paint() method is called as a result of you calling repaint(), so you never need to (nor should you) ever call it directly.
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:When it comes to overriding 'paint' or 'paintComponent', I think Stevens said it all.



hey thanks for the effort . i have copied your code line by line and changed the image location to my image (cloud.png) but now i get a 100 by 100 window and "Could not load image!" message in my console . i have placed my cloud image in my src folder
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must confess: I didn't see that one coming!

Well, there are two ways to fix it:

1) do as I did: use the full path of your cloud image. That is a quick solution, but it will only work on your machine
2) make use of packages, in stead of using the default package. Do you use an IDE, like NetBeans or Eclipse? If so, create a package, and if your cloud image is in the same package as your .java files, then this will load your image:

Let us know if this works.
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:I must confess: I didn't see that one coming!

Well, there are two ways to fix it:

1) do as I did: use the full path of your cloud image. That is a quick solution, but it will only work on your machine
2) make use of packages, in stead of using the default package. Do you use an IDE, like NetBeans or Eclipse? If so, create a package, and if your cloud image is in the same package as your .java files, then this will load your image:

Let us know if this works.


hey thanks . it is working fine now , the image loads every time , just one thing should i use paintComponent or paint method : )
 
Piet Souris
Rancher
Posts: 1983
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad we could help!

Normally: use "paintComponent".

There are situations in which you want to override deliberately 'paint'. You would do that to achieve some special effects, for instance:
(resize the frame!)

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!