Tuesday, 15 January 2013

java - Swing - Repainting of a Photo on JPanel -


Hi guys, please ask me to help you.

Well, I need to refresh the jpnil with a different picture than the file, with the photo on one frame, jpnil The first time during adding - the photo is shown correctly! Everything is fine

But when I try to change the current photo dynamically from each other and refresh zipalal - I can only see one photo I see. And it does not matter where the following refreshing part of the code is used:

PicturePanel.print ();

Photo panele.valid ();

You can find the code below:

  // Create your own JPEG public class ImagePanel JPanel {Personal image image; Public image getImage () {returns image; } Public Zero Set Image (Image Image) {this.image = image; } @ Override Public Empty Paint Comonant (Graphics G) {SuperPaintConant (G); If (image! = Null) {g.drawImage (image, 0, 0, getWidth (), getHeight (), null); } Else System.out.println ("The picture is missing!"); }}  

Get the photo from the file and add it to your zenal ( ImagePanel )

  Public JPEG getTestPicture (string by file ) {ImagePanel PP = New ImagePanel (); Pp.setLayout (new border layout ()); Try {pp.setImage (imageIO.read (new file (from file)); } Hold (IOException e) {e.printStackTrace (); } Return PP; }  

and JPanel's main call properly:

  picturePanel = getTestPicture ("picture.jpg"); Frame.add (picturePanel); // looks correct - the photo is showing.  

....

If we are trying to redesign the genetic once again during the program, then stay on the old photo panel. The new picture has not been painted.

  Imagepanel = Friendly Picture ("Picture JPG"); Frame.add (picturePanel); //picture.jpg - This is shown correctly! PicturePanel = getTestPicture ("pic2.jpg"); PicturePanel.repaint (); PicturePanel.validate (); // does not work! Picture.jpg is still on the Zenith!  

Please help people with me! I need to understand what's wrong with my code ! Please do not propose to use JLabel or something similar.

Thank you in advance !!!!!

Do not add one to the new ImagePanel frame, make an existing one. ..

  public class SomeOtherComponent JPanel {Private ImagePanel imagePanel; // ... public SomeOtherComponent () {// ... imagePane = getTestPicture ("picture.jpg"); (ImagePane); // ...}  

When you need to change the image, just use something

  imagePane.setImage (ImageIO.read ( ...)); ImagePane.repaint ();  

No comments:

Post a Comment