Java Forum / GUI / October 2005
Reloading and display images at runtime
ajay - 01 Oct 2005 21:55 GMT I am trying to solve this problem since last week.I had made one JScrollPane and File transfer through sockets.I had sucessfully load and display my first screenshot image.But after my first success i am unable to display my second screenshot image which i download through sockets.My problem is how i can display my image on JScrollPane more than one time and when these images are coming through sockets.Here is my source code:- import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.text.*; import java.io.*; import java.net.*; import java.util.*;
public class remote extends JFrame { private JPanel btnpnl,picpnl; private JButton button[]; private UIManager.LookAndFeelInfo looks[]; private static int change = 1; private JLabel label1; private Icon icon[]; private String names[] = { "looks","connect","control", "refresh", "auto", "commands" }; private String iconnames[] = { "gifs/look.gif","gifs/connect.gif","gifs/control.gif","gifs/refresh.gif","gifs/auto.gif","gifs/command.gif" }; private String looknames[] = { "Motif", "Windows", "Metal" }; private int port=5000; private ScrollablePicture picture=null; private ServerSocket server; private Socket client; private ObjectInputStream ins = null; private ObjectOutputStream ous = null; private File file; private String fileName; private boolean autocontrol=true; private Image image=null; private Container c;
public remote() { super( "Remote Desktop" ); c = getContentPane(); c.setLayout( new BorderLayout() ); //String file = "screen.jpg"; looks = UIManager.getInstalledLookAndFeels();
btnpnl = new JPanel(); btnpnl.setLayout( new FlowLayout() );
button = new JButton[names.length]; icon = new ImageIcon[iconnames.length]; Buttonhandler handler = new Buttonhandler();
for( int i=0; i<names.length; ++i ) { icon[i] = new ImageIcon( iconnames[i] ); button[i] = new JButton( names[i], icon[i] ); button[i].addActionListener(handler);
btnpnl.add(button[i]); }
c.add( btnpnl, BorderLayout.NORTH );
//Adding Label1
label1 = new JLabel("LABEL1"); c.add( label1, BorderLayout.SOUTH );
picpnl = new JPanel(); picpnl.setLayout(new BorderLayout()); //image = Toolkit.getDefaultToolkit( ).getImage(file); // paintPicture(image); c.add( picpnl, BorderLayout.CENTER ); //setResizable(false); setSize( 808, 640 ); show();
}
public void paintPicture(Image image) {
picpnl.add( new JScrollPane(new ImageComponent(image)),BorderLayout.CENTER); picpnl.setVisible(true); }
public class Buttonhandler implements ActionListener { public void actionPerformed( ActionEvent e ) {
try{ if( e.getActionCommand() == "looks" ) { try { label1.setText( Integer.toString( change ) );
UIManager.setLookAndFeel( looks[change++].getClassName() ); SwingUtilities.updateComponentTreeUI(remote.this);
if(change==3) change=0;
} catch (Exception ex) { ex.printStackTrace(); } }
if( e.getActionCommand() == "connect" ) { label1.setText( "connect" ); runServer(); }
if( e.getActionCommand() == "refresh" ) { label1.setText( "refresh" ); ous.writeObject("refresh"); ous.flush(); recieveFile();
}
if( e.getActionCommand() == "auto" ) { label1.setText( "auto" ); ous.writeObject("auto"); button[4].setText("stop"); ous.flush(); setButton(false,false,false,false,true,false); auto(); }
if( e.getActionCommand() == "stop" ) { ous.writeObject("stop"); ous.flush(); button[4].setText("auto"); setButton(true,true,true,true,true,true); autocontrol=false; }
} catch(Exception ef) { ef.getMessage(); }
} }
public void auto() { while(autocontrol) { recieveFile(); } }
public void setButton( boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6) { button[0].setEnabled(b1); button[1].setEnabled(b2); button[2].setEnabled(b3); button[3].setEnabled(b4); button[4].setEnabled(b5); button[5].setEnabled(b6); }
public void recieveFile() { int flag = 0;
try{ while(flag<=2){ Object recieved = ins.readObject(); System.out.println("from client:" + recieved);
switch (flag) { case 0:
if (recieved.equals("sot")) { System.out.println("sot recieved"); flag++; } break;
case 1: fileName = (String) recieved; file = new File(fileName); System.out.println("fileName received");
flag++;
break;
case 2:
if (file.exists()) { file.delete(); } byte[] b = (byte[])recieved; //the following code is used to store and display the new file FileOutputStream ff = new FileOutputStream(fileName); ff.write(b); flag = 3; if(image!=null) image.flush(); image = null;
Runtime rt = Runtime.getRuntime(); rt.runFinalization(); rt.gc();
image = Toolkit.getDefaultToolkit().createImage( b ,0, b.length );
paintPicture(image);
break; }
}
} catch(Exception e) { e.printStackTrace(); }
}
class ImageComponent extends JComponent { Image image; Dimension size;
public ImageComponent(Image image) { this.image = image; MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); try { mt.waitForAll( ); } catch (InterruptedException e) { // error ... };
size = new Dimension (image.getWidth(null), image.getHeight(null)); setSize(size);} public void paint(Graphics g) {
g.drawImage(image, 0, 0, this); }
public Dimension getPreferredSize( ) { return size; }
}
public void runServer() { try{ server = new ServerSocket(port);
client = server.accept(); JOptionPane.showMessageDialog(null,"Connected"); System.out.println("connection received from" + client.getInetAddress().getHostName());
ous = new ObjectOutputStream( client.getOutputStream() ); ous.flush(); ins = new ObjectInputStream( client.getInputStream() ); System.out.println("get i/o streams"); } catch(Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) { remote app = new remote(); app.addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } );
}
} Please help me by seeing this code.when i am trying to display another image with the same name "screen.jpg" the jvm doesn't display it in JScrollpane.
Andrew Thompson - 01 Oct 2005 22:15 GMT > I am trying to solve this problem since last week.I had made one > JScrollPane and File transfer through sockets.I had sucessfully load > and display my first screenshot image.But after my first success i am > unable to display my second screenshot image which i download through > sockets. Are you able to display your second screenshot image when you load them from the local disk?
Notes on the example you posted. - It was too long (341 lines) yet did not compile because 'ScrollablePicture' was not included. - It uses deprecated (in 1.5) methods, most notably 'show()'. - You seem to *mostly* follow the standard Java nomenclature, except in your class name. 'remote' -> 'Remote'. - Please replace tab characters with a few spaces before posting code.
ajay - 02 Oct 2005 15:57 GMT I am using
Java platform:jdk 1.4.1 Windows platform: Windows 98se
Problem:I am making one application in which i need to display images when i get it from socket.I had used one JScrollPane over JPanel with scrollbars.But when i get the image i stored it into an Image object.
such as in my main program screen = Toolkit.getDefaultToolkit().getImage ( "screen.jpg" );
//Draw picture first time it works fine... paintPicture(screen);
paintPicture(Image image) {picpnl = new JPanel(); picpnl.setLayout(new BorderLayout()); image = Toolkit.getDefaultToolkit( ).getImage(image);
//Is picpnl is unable to display image second time
picpnl.add(new JScrollPane(new ImageComponent(image)),BorderLayout.CENTER); c.add( picpnl, BorderLayout.CENTER ); picpnl.setVisible(true);
--- --- from my refresh code: if(image!=null) image.flush(); Runtime rt = Runtime.getRuntime(); rt.runFinalization(); rt.gc(); image = null;
//is this 2nd time image not properly stored in variable image
image = Toolkit.getDefaultToolkit().createImage( b ,0, b.length ); paintPicture(image);
//go to paintPicture second time doesn't work
class ImageComponent extends JComponent { Image image; Dimension size;
public ImageComponent(Image image) { this.image = image; MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); try { mt.waitForAll( ); } catch (InterruptedException e) { // error ... };
size = new Dimension (image.getWidth(null), image.getHeight(null)); setSize(size);} public void paint(Graphics g) {
g.drawImage(image, 0, 0, this); }
public Dimension getPreferredSize( ) { return size; } }
The above code works fine for (the first time).But when i try to display a different socket image (the second time) it doesn't work.
I didn't know what the problem is... ---whether the previous image didn't get erased from RAM or ---the OS is not aware of the current changed image in variable (image) or the ---JScrollPane is unable to redraw it second time.
How can i display one changed image from the socket onto the JScrollPane? (the second time).
Also i want to know if we are using one file with the name "screen.jpg" and at runtime if i changed that file with another file with the same name. How can i display that changed file?Is it possible in Java or not.(Is it the limitation of Java).
I totally get frustated by reading all forums but i didn't get the answer.Please reply me soon with the complete code?
ajay - 02 Oct 2005 16:33 GMT The complete source code for the problem: (SERVER): import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.text.*; import java.io.*; import java.net.*; import java.util.*;
public class Remote extends JFrame { private JPanel btnpnl,picpnl; private JButton button[]; private UIManager.LookAndFeelInfo looks[]; private static int change = 1; private JLabel label1; private Icon icon[]; private String names[] = { "looks","connect","control", "refresh", "auto", "commands" }; private String iconnames[] = { "gifs/look.gif","gifs/connect.gif","gifs/control.gif","gifs/refresh.gif","gifs/auto.gif","gifs/command.gif" }; private String looknames[] = { "Motif", "Windows", "Metal" }; private int port=5000; private ScrollablePicture picture=null; private ServerSocket server; private Socket client; private ObjectInputStream ins = null; private ObjectOutputStream ous = null; private File file; private String fileName; private boolean autocontrol=true; private Image image=null; private Container c;
public remote() { super( "Remote Desktop" ); c = getContentPane(); c.setLayout( new BorderLayout() ); //String file = "screen.jpg"; looks = UIManager.getInstalledLookAndFeels();
btnpnl = new JPanel(); btnpnl.setLayout( new FlowLayout() );
button = new JButton[names.length]; icon = new ImageIcon[iconnames.length]; Buttonhandler handler = new Buttonhandler();
for( int i=0; i<names.length; ++i ) { icon[i] = new ImageIcon( iconnames[i] ); button[i] = new JButton( names[i], icon[i] ); button[i].addActionListener(handler);
btnpnl.add(button[i]); }
c.add( btnpnl, BorderLayout.NORTH );
//Adding Label1
label1 = new JLabel("LABEL1"); c.add( label1, BorderLayout.SOUTH );
picpnl = new JPanel(); picpnl.setLayout(new BorderLayout()); //image = Toolkit.getDefaultToolkit( ).getImage(file); // paintPicture(image); c.add( picpnl, BorderLayout.CENTER ); //setResizable(false); setSize( 808, 640 ); show();
}
public void paintPicture(Image image) { picpnl.add(new JScrollPane(new ImageComponent(image)),BorderLayout.CENTER); picpnl.setVisible(true); }
public class Buttonhandler implements ActionListener { public void actionPerformed( ActionEvent e ) {
try{ if( e.getActionCommand() == "looks" ) { try { label1.setText( Integer.toString( change ) );
UIManager.setLookAndFeel( looks[change++].getClassName() );
SwingUtilities.updateComponentTreeUI(remote.this);
if(change==3) change=0;
} catch (Exception ex) { ex.printStackTrace(); } }
if( e.getActionCommand() == "connect" ) { label1.setText( "connect" ); runServer(); }
if( e.getActionCommand() == "refresh" ) { label1.setText( "refresh" ); ous.writeObject("refresh"); ous.flush(); recieveFile();
}
if( e.getActionCommand() == "auto" ) { label1.setText( "auto" ); ous.writeObject("auto"); button[4].setText("stop"); ous.flush(); setButton(false,false,false,false,true,false); auto(); }
if( e.getActionCommand() == "stop" ) { ous.writeObject("stop"); ous.flush(); button[4].setText("auto"); setButton(true,true,true,true,true,true); autocontrol=false; }
} catch(Exception ef) { ef.getMessage(); }
} }
public void auto() { while(autocontrol) { recieveFile(); } }
public void setButton( boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6) { button[0].setEnabled(b1); button[1].setEnabled(b2); button[2].setEnabled(b3); button[3].setEnabled(b4); button[4].setEnabled(b5); button[5].setEnabled(b6); }
public void recieveFile() { int flag = 0;
try{ while(flag<=2){ Object recieved = ins.readObject(); System.out.println("from client:" + recieved);
switch (flag) { case 0:
if (recieved.equals("sot")) { System.out.println("sot recieved"); flag++; } break;
case 1: fileName = (String) recieved; file = new File(fileName); System.out.println("fileName received");
flag++;
break;
case 2:
if (file.exists()) { file.delete(); } byte[] b = (byte[])recieved;
FileOutputStream ff = new FileOutputStream(fileName);
ff.write(b); flag = 3; if(image!=null) image.flush(); Runtime rt = Runtime.getRuntime(); rt.runFinalization(); rt.gc();
image = null; Image = Toolkit.getDefaultToolkit().createImage( b ,0, b.length );
paintPicture(image);
break; }
}
} catch(Exception e) { e.printStackTrace(); }
}
class ImageComponent extends JComponent { Image image; Dimension size;
public ImageComponent(Image image) { this.image = image; MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); try { mt.waitForAll( ); } catch (InterruptedException e) { // error ... };
size = new Dimension (image.getWidth(null), image.getHeight(null)); setSize(size);} public void paint(Graphics g) {
g.drawImage(image, 0, 0, this); }
public Dimension getPreferredSize( ) { return size; }
}
public void runServer() { try{ server = new ServerSocket(port);
client = server.accept();
JOptionPane.showMessageDialog(null,"Connected"); System.out.println("connection received from" + client.getInetAddress().getHostName());
ous = new ObjectOutputStream( client.getOutputStream() ); ous.flush(); ins = new ObjectInputStream( client.getInputStream() ); System.out.println("get i/o streams"); } catch(Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) { remote app = new remote(); app.addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } );
}
} CLIENT code: import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.awt.image.*; import javax.imageio.*;
public class Client extends JFrame { private JLabel label1; private JButton button1; private Socket server; private ObjectInputStream ins=null; private ObjectOutputStream ous=null; private File file; private boolean autocontrol=true;
public Client() { super("Client");
Container c = getContentPane();
ButtonHandler handler = new ButtonHandler(); c.setLayout( new FlowLayout() );
label1 = new JLabel(""); button1 = new JButton("connect");
c.add(label1); c.add(button1); button1.addActionListener(handler); setSize(300,300);
show(); }
class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="connect") { runClient(); } } }
public void runClient() {
try{ server = new Socket(InetAddress.getByName("127.0.0.1"),5000); System.out.println("connected to" + server.getInetAddress().getHostName());
ous = new ObjectOutputStream(server.getOutputStream()); ous.flush(); ins = new ObjectInputStream(server.getInputStream());
System.out.println("get i/o streams");
file = new File("screen.jpg");
listencommands(); }catch(Exception e) { e.getMessage(); } } public void listencommands() { try {
while(true){ Object message = ins.readObject();
if(message.equals("refresh")) { screenshot(); sendFile(file); }
if(message.equals("auto")) { autocontrol=true; while(autocontrol) { screenshot(); sendFile(file); } }
if(message.equals("stop")) { autocontrol = false; }
} } catch(Exception e) { e.printStackTrace(); }
}
public void sendFile( File file ) throws IOException {
try{
System.out.println("Ready to send file"); ous.writeObject("sot"); ous.flush(); System.out.println("sot send");
ous.writeObject("screen.jpg");
ous.flush(); System.out.println("fileName send");
FileInputStream f = new FileInputStream (file); System.out.println("fileinputstream received"); int size = f.available(); byte array[] = new byte[size]; f.read(array,0,size); System.out.println("Declared array"); //ous.write(size); //ous.flush();
ous.writeObject(array); ous.flush();
System.out.println("image send");
} catch (Exception e) { System.out.println(e.getMessage()); }
} public void screenshot() { try { //Get the screen size Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height); Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(rectangle); File file;
//Save the screenshot as a png
file = new File("screen.jpg");
ImageIO.write(image, "jpg", file); } catch (Exception e) { System.out.println(e.getMessage()); } }
public static void main( String args[] ) { Client app = new Client();
app.addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } );
} }
Andrew Thompson - 02 Oct 2005 16:50 GMT > The complete .. It sure is not. ScrollablePicture is still missing, but do not dare post the code for SP.
>..source code for the problem: > (SERVER): [ snip 534 lines of code that is not short, not complete, does not compile, and therefore does not provide an example of the stated problem ]
Please read this document on preparing code for others to see, before posting any more example codes. <http://www.physci.org/codes/sscce.jsp>
A tip as well, make sure you can display a second picture when reading from the file system, as that is a necessary prerequisite to getting it right when dealing with a server/client combination.
That is, your should only submit an SSCCE involving a server/client if you have seen it work off the file-system. Read the SSCCE document carefully for more tips.
ajay - 02 Oct 2005 18:33 GMT Are you using the below version of jdk1.4.1 along with windows 98se Please make sure... Then compile the code.
I am using
Java platform:jdk 1.4.1 Windows platform: Windows 98se
Problem:I am making one application in which i need to display images when i get it from socket.I had used one JScrollPane over JPanel with scrollbars.But when i get the image i stored it into an Image object.
such as in my main program screen = Toolkit.getDefaultToolkit().getImage ( "screen.jpg" );
//Draw picture first time it works fine... paintPicture(screen);
paintPicture(Image image) {picpnl = new JPanel(); picpnl.setLayout(new BorderLayout()); image = Toolkit.getDefaultToolkit( ).getImage(image);
//Is picpnl is unable to display image second time
picpnl.add(new JScrollPane(new ImageComponent(image)),BorderLayout.CENTER); c.add( picpnl, BorderLayout.CENTER ); picpnl.setVisible(true);
--- --- from my refresh code: if(image!=null) image.flush(); Runtime rt = Runtime.getRuntime(); rt.runFinalization(); rt.gc(); image = null;
//is this 2nd time image not properly stored in variable image
image = Toolkit.getDefaultToolkit().createImage( b ,0, b.length ); paintPicture(image);
//go to paintPicture second time doesn't work The above code works fine for (the first time).But when i try to display a different socket image (the second time) it doesn't work.
I didn't know what the problem is... ---whether the previous image didn't get erased from RAM or ---the OS is not aware of the current changed image in variable (image) or the ---JScrollPane is unable to redraw it second time.
How can i display one changed image from the socket onto the JScrollPane? (the second time).
Also i want to know if we are using one file with the name "screen.jpg" and at runtime if i changed that file with another file with the same name. How can i display that changed file?Is it possible in Java or not.(Is it the limitation of Java).
I totally get frustated by reading all forums but i didn't get the answer.Please reply me soon with the complete code?
One more simillar example:- 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()); _Image_Label_1.setIcon(new ImageIcon(_Query_Image.getDir())); _Image_Label_1.repaint(); }
I not sure why this doesn't work, the image icon is not being set when i load a new query image at runtime.Please think and reply
ajay - 02 Oct 2005 18:38 GMT Please reply by seeing the next example
ajay - 02 Oct 2005 18:45 GMT Here is the Scrollable Picture source code: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*;
/* ScrollablePicture.java is used by ScrollDemo.java. */
public class ScrollablePicture extends JLabel implements Scrollable, MouseMotionListener {
private int maxUnitIncrement = 1; private boolean missingPicture = false;
public ScrollablePicture(ImageIcon i, int m) { super(i); if (i == null) { missingPicture = true; setText("No picture found."); setHorizontalAlignment(CENTER); setOpaque(true); setBackground(Color.white); } maxUnitIncrement = m;
//Let the user scroll by dragging to outside the window. setAutoscrolls(true); //enable synthetic drag events addMouseMotionListener(this); //handle mouse drags }
//Methods required by the MouseMotionListener interface: public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { //The user is dragging us, so scroll! Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1); scrollRectToVisible(r); }
public Dimension getPreferredSize() { if (missingPicture) { return new Dimension(320, 480); } else { return super.getPreferredSize(); } }
public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); }
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { //Get the current position. int currentPosition = 0; if (orientation == SwingConstants.HORIZONTAL) { currentPosition = visibleRect.x; } else { currentPosition = visibleRect.y; }
//Return the number of pixels between currentPosition //and the nearest tick mark in the indicated direction. if (direction < 0) { int newPosition = currentPosition - (currentPosition / maxUnitIncrement) * maxUnitIncrement; return (newPosition == 0) ? maxUnitIncrement : newPosition; } else { return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition; } }
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { if (orientation == SwingConstants.HORIZONTAL) { return visibleRect.width - maxUnitIncrement; } else { return visibleRect.height - maxUnitIncrement; } }
public boolean getScrollableTracksViewportWidth() { return false; }
public boolean getScrollableTracksViewportHeight() { return false; }
public void setMaxUnitIncrement(int pixels) { maxUnitIncrement = pixels; } }
ajay - 03 Oct 2005 19:33 GMT Please reply by seeing the small source code Donot bother by the above long code
/*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 ); } } );
} };
ajay - 04 Oct 2005 10:16 GMT Thank you all the problem is solved by Andrew... Bye
Andrew Thompson - 04 Oct 2005 11:20 GMT > Thank you all You're welcome.
>..the problem is solved by Andrew... Note that the other codes supplied would be well worth a look. AFAIR, each took a slightly different approach to loading the images, as well as the GUI components used.
More importantly, they probably put a lot of time and thought into the codes they posted, where I was just 'hacking about' till I saw the problem.
Glad it's working now. :-)
Roedy Green - 04 Oct 2005 10:36 GMT >I am trying to solve this problem since last week. You have three, maybe more, threads on the same problem. Please don't do that. It is hard enough to keep everyone's problems sorted out.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Andrew Thompson - 04 Oct 2005 11:29 GMT >>I am trying to solve this problem since last week. > > You have three, maybe more, threads on the same problem. Please don't > do that. It is hard enough to keep everyone's problems sorted out. Good advice Roedy, the thread(s) has been a little confusing. It must have been some work for ajay to keep them all relatively up to date as well!
Free MagazinesGet 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 ...
|
|
|