Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

short description on Reloading and redisplaying images at runtime

Thread view: 
ajay - 03 Oct 2005 07:36 GMT
Short discription of the problem:

Reloading and redisplaying images:
I am using->

Windows:windows 98se   Java version: Jdk 1.4.1

I am a beginner programmer.But i had worked with various languages
previously. I
got stunned with such a problem in Java is not fixed.I had already
given my source code and long problem discription in Reloading and
redisplaying images.

I am designing one program in which i need to display images when i get
it from
socket. I get the first image on JScrollPane properly. But when i get
second updated image from the socket i am unable to display it on
JScrollPane.

Simillar short code:

Here is my code (I have left some less important bits out but can post
it all if you want).

creating the label and image icon

String image_dir = new String();

   if (type.equals("QUERY") && _Query_Image != null)
   {
     image_dir = _Query_Image.getDir();
     ImageIcon icon = new ImageIcon("C:\\Documents and
Settings\\Administrator\\My Documents\\My Pictures\\01.jpg");
     _Image_Label_1 = new JLabel(icon);
     JScrollPane pictureScrollPane = new JScrollPane(_Image_Label_1);
     pictureScrollPane.setPreferredSize(new Dimension(500,500));

pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
     return pictureScrollPane;
   }

in my refresh method

if (_Query_Image != null)
   {
     _Query_Image_Dir.setText(_Query_Image.getName());

   //Change the image icon at runtime it doesn't work.....

    _Image_Label_1.setIcon(new ImageIcon(_Query_Image.getDir()));
    _Image_Label_1.repaint();
   }

I am not sure why this doesn't work, the image icon is not being set
when i load a new query image at runtime.I had already used
image.flush() and Runtime code. But i didn't get the desired output. If
anyone know more about this tophic please help
me!

thanks
ajay - 03 Oct 2005 19:38 GMT
Smaller code to test my problem:
/*Author: Ajay Partoti
This is the smallest code to illustrate my problem
Before running this program
Please make sure u had screen.jpg and actionsdemo.jpg in your
Test program directory*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame
{
    //Declare variables....
    private JPanel picpnl;
    private JButton refresh;
   private Image image;

   public Test()
    {
        //Set container to BorderLayout....
        Container c = getContentPane();
        c.setLayout( new BorderLayout() );

                              //Add button to container
        refresh=new JButton("refresh");
    image = Toolkit.getDefaultToolkit(  ).getImage("screen.jpg");
                      //Add actionListener to button refresh...
               refresh.addActionListener(
         new ActionListener(){
         public void actionPerformed(ActionEvent e)
             {
         if(e.getActionCommand()=="refresh")
                 {
 /*THIS IS WHERE THE PROBLEM IS...
 AT RUNTIME I DIDN'T GET THE SECOND IMAGE TO PAINT ON JPANEL*/

 image = Toolkit.getDefaultToolkit(  ).getImage("actionsdemo.jpg");
picpnl.add(new JScrollPane(new
ImageComponent(image)),BorderLayout.CENTER);
                     picpnl.setVisible(true);
                     JOptionPane.showMessageDialog(Test.this,"I didn't get the new
image on JPanel...");
                 }
             }
         }
         );

        //Declare picpnl as JPanel

        picpnl = new JPanel();
        picpnl.setLayout(new BorderLayout());

//Add scrollPane component by using   Imagecomponent class
        picpnl.add(new JScrollPane(new
ImageComponent(image)),BorderLayout.CENTER);
       picpnl.setVisible(true);

        //add button and picpnl and set visible
        c.add(refresh,BorderLayout.NORTH);
        c.add(picpnl,BorderLayout.CENTER);

        picpnl.setVisible(true);
        setSize(300,300);
        show();
    }
 class ImageComponent extends JComponent {
    Image image;
    Dimension size;

    public ImageComponent(Image image) {
      this.image = image;
      //Set MediaTracker to observer images...
      MediaTracker mt = new MediaTracker(this);
      mt.addImage(image, 0);
      try {
       mt.waitForAll(  );
       }
   catch (InterruptedException e) {
     // error ...
   };
         //get and set Dimensions
   size = new Dimension (image.getWidth(null),
                         image.getHeight(null));
   setSize(size);}

    public void paint(Graphics g) {
          //repaint images on the screen....
    g.drawImage(image, 0, 0, this);

 }

 public Dimension getPreferredSize(  ) {
   //Get the size...
    return size;
 }

}
 public static void main(String[] args)
    {
      Test app = new Test();
      app.addWindowListener(
          new WindowAdapter(){
                public void windowClosing( WindowEvent e )
               {
                    System.exit( 0 );
                }
          }
        );

    }
};
Roedy Green - 03 Oct 2005 20:44 GMT
>Smaller code to test my problem:

Just noticing the rather jagged indentation. That might be tabs or it
might be that you don't have an IDE to maintain that for you.  You are
in for quite  treat if you have never used an IDE.

See http://mindprod.com/jgloss/ide.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

ajay - 03 Oct 2005 20:51 GMT
Thanks sir,
     I got very happy by seeing you on this forum.
I like your site mindprod very much because it contain all the glossary
keywords of java.Please help me to solve my problem.

Thank You.
ajay - 03 Oct 2005 21:38 GMT
I used your imageviewer program with my code...
But it didn't solve my problem..
But the original problem is:
if AFTER RUNNING THE PROGRAM I DELETE AND REPLACE A FILE WITH THE SAME
NAME
AND TRY TO DISPLAY THAT FILE AFTER PRESSING REFRESH IT IS NOT
WORKING...

Change in the program...

if(e.getActionCommand()=="refresh")
   {
  /*BUT IF I CHANGE THE SECOND IMAGE TO THE SAME IMAGE NAME

 image = Toolkit.getDefaultToolkit().getImage("screen.jpg");

 picpnl.removeAll();
 picpnl.add(new JScrollPane(new
ImageComponent(image)),BorderLayout.CENTER);
picpnl.setVisible(true);
 validate();
 }

I appreciate your help but the above is a original problem...
Roedy Green - 03 Oct 2005 20:49 GMT
> class ImageComponent extends JComponent

I wrote a similar class called ImageViewer.

You could plug my code in instead.  If it works you know the problem
is in ImageComponent. You can compare your code and mine to find the
problem.  If it does not work, then the problem is elsewhere.

Source is onted at:
http://mindprod.com/jgloss/image.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Vova Reznik - 03 Oct 2005 21:52 GMT
> I wrote a similar class called ImageViewer.

It is awt, not swing.
Roedy Green - 04 Oct 2005 09:39 GMT
>> I wrote a similar class called ImageViewer.
>
>It is awt, not swing.

You could create a Swing version by changing Canvas to JPanel and
paint to paintComponent.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

ajay - 04 Oct 2005 10:15 GMT
Problem is solved by Andrew Thanks...
Roedy Green - 04 Oct 2005 10:34 GMT
>Problem is solved by Andrew Thanks...

What was the problem?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.