DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Code Snippet: Problem in working with images in swing (http://www.daniweb.com/forums/thread233351.html)

tsfaridi Oct 26th, 2009 1:29 am
Problem in working with images in swing
 
I am using this code for displaying images on JPanel, in which i succeeded, but the images are volatile. When i maximize or minimize the frame, it vanishes.

  1. public class ImagePanel extends JPanel{
  2.  
  3. private BufferedImage image;
  4.  
  5. public ImagePanel() {
  6. try {
  7. image = ImageIO.read(new File("image name and path"));
  8. } catch (IOException ex) {
  9. // handle exception...
  10. }
  11. }
  12.  
  13. @Override
  14. public void paintComponent(Graphics g) {
  15. g.drawImage(image, 0, 0, this);
  16.  
  17. }
  18.  
  19. }
BestJewSinceJC Oct 26th, 2009 2:13 am
This belongs in a normal thread, not in a code snippet. And I've experienced the same problem in the past, but I did a lot more debugging than you did. For example, when the window is resized, is your paintComponent method called?

tsfaridi Oct 26th, 2009 4:11 am
Thanks! you have shown me a path, i will work on it. Here the issue arises is, i am working on Checkers game, calling the paint method again will not resolve my issue. As the position of pieces will not be the same as initial.
@BestJewSinceJC: Kindly share with me your experience, how did you resolved this problem ??


All times are GMT -4. The time now is 11:56 pm.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC