Home arrow static arrow Java Programming [Archive] - problem with ImageIcon (repaint??)
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - problem with ImageIcon (repaint??)
This topic has 4 replies on 1 page.

Posts:6
Registered: 8/2/04
problem with ImageIcon (repaint??)  
Aug 2, 2004 4:35 PM



 
Hello All,

I am interested in having an application to display some gif files where the user can select from a list the image to display. For that I am basically using ImageIcon. The application basically works fine but When I overlap other window or I scroll down (or up) and then go back to the applet the image is not conserved. In other words, if I select an image different than the firstone and open another window and go back, the applet will display the fist image loaded. For a "working" minimal appler you can go to http://shadow.eas.gatech.edu/~choyos/Minimal/

I am also including the code.. Thanks a lot!!!

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;

public class Minimal extends javax.swing.JApplet implements ListSelectionListener {
private DefaultListModel listModel;
public String[] filenames;
public String filename;
public String datesname;
public String title;
public String path;
public String[] dates;
public Vector data2 = new Vector();
public ImageIcon icon;

public void init() {
initComponents();
loadData();
}

public void loadData(){
URL u=null;
DataInputStream inFile=null;
String record = null;
int recCount = 0;
int botpos = 0;

String datesname = getParameter("Dates");
String filename = getParameter("Files");

try{
u=new URL(getCodeBase(),datesname);
inFile=new DataInputStream(u.openStream());

while ( (record=inFile.readLine()) != null ) {
recCount++;

data2.addElement(record);

}
}catch (Exception ee){
System.out.println("Got an Exception error! " + ee.getMessage());
return;
}finally {

if (inFile != null) {
try {
inFile.close();
} catch (IOException ioe) {
}
}
}

dates = new String[data2.size()];
filenames = new String[data2.size()];

try{
u=new URL(getCodeBase(),filename);
inFile=new DataInputStream(u.openStream());
for (int i=0;i<data2.size();i++){
record=inFile.readLine();
filenames=record;

}

}catch (Exception ee){
System.out.println("Got an Exception error! " + ee.getMessage());
return;
}finally {

if (inFile != null) {
try {
inFile.close();
} catch (IOException ioe) {
}
}
}

jList1.setListData(data2);
jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.getViewport().setView(jList1);

data2.toString();
data2.toArray();
dates = (String[])data2.toArray(dates);

jList1.addListSelectionListener(this);
jScrollPane1.getViewport().setView(jList1);
jList1.setSelectedIndex(0);
}

private void initComponents() {
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jList1 = new javax.swing.JList();
jScrollPane1 = new javax.swing.JScrollPane();

jPanel2.setLayout(new java.awt.BorderLayout());

jPanel3.setLayout(new java.awt.BorderLayout());

jPanel4.setLayout(new java.awt.BorderLayout());

jLabel2.setText("Dates:");
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

jPanel3.add(jPanel4, java.awt.BorderLayout.SOUTH);

jLabel1.setBackground(new java.awt.Color(255, 255, 255));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jPanel3.add(jLabel1, java.awt.BorderLayout.NORTH);

jPanel2.add(jPanel3, java.awt.BorderLayout.CENTER);

getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

jPanel1.setLayout(new java.awt.BorderLayout());

jPanel1.add(jLabel2, java.awt.BorderLayout.NORTH);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

}

public void valueChanged(ListSelectionEvent e) {
jPanel5 = new javax.swing.JPanel();
jPanel5.setLayout(new java.awt.BorderLayout());
jPanel3.add(jPanel5, java.awt.BorderLayout.CENTER);
DisplayImage(jList1.getSelectedIndex());

}

private void DisplayImage(int index){
MediaTracker tracker = new MediaTracker(this);
Image image = null;

try
{
String path = getParameter("Path");
image = getImage(new URL(getCodeBase(),path+filenames[index]));

}
catch(MalformedURLException e)
{
System.out.println("Failed to create URL:\n" + e);
}

tracker.addImage(image,0);
try
{
tracker.waitForAll();
if(tracker.isErrorAny())
return;

ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
jPanel5.add(label,java.awt.BorderLayout.CENTER);

String title = getParameter("Title");
jLabel1.setText(title+": "+dates[index]);

}
catch(InterruptedException e)
{
System.out.println(e);
}

}

private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JList jList1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JScrollPane jScrollPane1;

}

 

Posts:64
Registered: 10/2/03
Re: problem with ImageIcon (repaint??)  
Aug 2, 2004 6:28 PM (reply 1 of 4)



 
what i would suggest it to overload the paint method of your JApplet. Since this should be automatically called after the browser has been minimized, lost focus, etc.

public void paint(Graphics g) {     int i = jList1.getSelectedIndex();    // make sure a selection is made (-1 if no selection)     if (i < 0)          i = 0;     // update the map     DisplayImage(0);}


Hope this helps you out. Also for future reference please use the '' tags in here, it will make your code easier to read. ;-)
 

Posts:6
Registered: 8/2/04
Re: problem with ImageIcon (repaint??)  
Aug 2, 2004 7:12 PM (reply 2 of 4)



 
Sorry for the code.... Thanks for your reply. It still does not work. When I add public void paint it just displays a blank "screen".
 

Posts:64
Registered: 10/2/03
Re: problem with ImageIcon (repaint??)  
Aug 2, 2004 7:36 PM (reply 3 of 4)



 
Oh i'm sorry, i made a typo even though i'm sure u picked it up

instead of this....
DisplayImage(0);


it should have been.......
DisplayImage(i);


dur.......sorry
 

Posts:6
Registered: 8/2/04
Re: problem with ImageIcon (repaint??)  
Aug 2, 2004 9:30 PM (reply 4 of 4)



 
Yes, I noticed the typo but it did not work.

Thanks again. I don't really know what should I do. I tried something similar but with ImagePanel instead of ImageIcon but I have the same problem.
 
This topic has 4 replies on 1 page.