Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to update image in Frame

 
sumit karan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a program which takes a image from the current directory and displays it over a frame..But i am calling this frame class from an another class every 5 sec. and in the meantime the image in the current directory changes...But the PROBLEM is that the new image doesn't show up in the new frame which turns up in a new window. Everytime we see the very first image which was there in the first frame.

The code for Frame class is:

import java.awt.*;
import java.awt.event.*;

public class ImageFrame extends Frame {
private static Image _image = null;
MediaTracker mt = null;

public ImageFrame()
{
super("");
System.out.print("Iframe Constructor....");
showImage();
setLayout(new FlowLayout());
setSize(500, 500);
addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Bye.");
System.exit(0);
}
}
);
}

public void showImage()
{
try {
mt = new MediaTracker (this);
_image = null;
_image = Toolkit.getDefaultToolkit().getImage("Desk.png");
System.out.print("Iframe showImage....\n");
mt.addImage(_image, 0);
mt.waitForID(0);
}
catch (Exception e) { e.printStackTrace(); }
}


public void update( Graphics g) {
paint(g);
}

public void paint(Graphics g) {
System.out.println("In Paint....\n");
if(_image != null)
{
int x = 0, y = 0;
g.drawImage(_image, x, y, this);
}
else {
g.clearRect(0, 0, getSize().width, getSize().height);
}

}
}


Every 5 sec. i am making a new object of this frame n call its constructor as:

ImageFrame iframe=null;
iframe = new ImageFrame();
iframe.setVisible(true);

I can see the image "Desk.png" changing in the current directory.

the reply for this problem will be appriciated
or you can mail me your solution on karan.sumit@gmail.com/sumit_karan@da-iict.org
do try this one!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic