Reloading and redisplay images in JScrollPane at runtime
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ajay_Partoti
Posted On:   Monday, October 3, 2005 03:37 AM

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 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 an   More>>

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 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

   <<Less

short code to test my exact problem

Posted By:   Ajay_Partoti  
Posted On:   Monday, October 3, 2005 03:09 PM

/*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 );

}

}

);



}

};

Re: Reloading and redisplay images in JScrollPane at runtime

Posted By:   Almagest_FUTT  
Posted On:   Monday, October 3, 2005 11:51 AM

I am not sure why this doesn't work, the image icon is not
being set when i load a new query image

Me neither. He are a few remarks:

  • Does an Exception get thrown anywhere (FileNotFound et al.) ?

  • Are you sure:
    new ImageIcon(_Query_Image.getDir())
    works properly ?

  • Maybe you should try:

    pictureScrollPane.setViewportView( new JLabel(new ImageIcon( imagePath )) );

    (That means keeping a reference to the scrollpane).

About | Sitemap | Contact