Help with passing from an arraylist to an arraylist
*******PLEASE NOTE EXTENDED CODE********
I Need help with passing a String from the images Array List to the imageList array list which should then use the string to create the image and display it with the media tracker.
When I run the code nothing happens(in terms of the images being displayed). I tried checking if the objects were actually being passed into the array. The actual references to the images are being got from a text file and put into the "images" array list( there are 8) but when I try to put these into the "imageList" array list before they are processed by the media tracker it says that only 6 have been added but still displays nothing.
// File: ImageChanger.java
// Version 2
// Based on ImageViewer Version 3
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.Timer;
import java.io.File;
import java.io.*;
public class ImageChanger
{
public static void main( String[] args )
{
new ImageChanger();
}
private ArrayList images;
private ImagePane pane;
private List imageList;
private Toolkit toolkit;
private JFrame f;
private JFrame f1;
private JSplitPane splitPane;
private Timer timer;
private JFileChooser chooser;
private JMenuItem newfile;
private JMenuItem open;
private JMenuItem save;
private JMenuItem close;
private JTextArea j;
private JPanel jp1;
private final int IMAGE_COUNT = 6;
private int currentImage;
public ImageChanger()
{
f = new JFrame();
images = new ArrayList();
chooser = new JFileChooser();
j = new JTextArea();
open = new JMenuItem("Open");
newfile = new JMenuItem("New");
close = new JMenuItem("Close");
save = new JMenuItem("Save");
JMenuBar jmb = new JMenuBar();
JMenu file = new JMenu ("File");
file.add(newfile);
file.add(open);
open.addActionListener (new ActionListener()
{ public void actionPerformed( ActionEvent e )
{
if( e.getSource() == open )
{
doOpen();
}
}
});
file.addSeparator();
file.add (save);
file.addSeparator();
file.add (close);
jmb.add (file);
f.setJMenuBar (jmb);
f.setVisible(true);
//Create a split pane with the two scroll panes in it.
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
//Provide minimum sizes for the two components in the split pane
Dimension minimumSize = new Dimension(100, 50);
launchGUI();
doOpen();
toolkit = Toolkit.getDefaultToolkit();
imageList = new ArrayList();
for( int i = 0; i< IMAGE_COUNT; i++ )
{
imageList.add( toolkit.createImage( (String)images.get(i) ) );
MediaTracker mt = new MediaTracker( f );
mt.addImage( (Image)imageList.get( i ), 0 );
try
{
mt.waitForID(0);
}
catch (InterruptedException ie)
{
System.err.println(ie);
System.exit(0);
}
}
showPics();
currentImage = 0;
//launchGUI();
}
public void showPics()
{
String temp;
for (int i=0;i<=imageList.size()-1; i++)
{
temp = (String) imageList.get(i);
System.out.println(temp.toString());
}
}
private void launchGUI()
{
f.getContentPane().setLayout( new BorderLayout() ); // should be default
pane = new ImagePane();
f.getContentPane().add( pane, BorderLayout.CENTER );
JButton b0 = new JButton( "Play" );
JButton b1 = new JButton( "Pause" );
JPanel jp1 = new JPanel();
//b1.setEnabled( false );
f.getContentPane().add( jp1, BorderLayout.NORTH);
jp1.add( b1, BorderLayout.EAST );
jp1.add( b0, BorderLayout.WEST);
j.setLineWrap(true);
f.getContentPane().add(j, BorderLayout.SOUTH);
b0.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
startShow();
}
});
b1.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
if( e.getActionCommand().equals( "Pause" ))
{
timer.stop();
((JButton)(e.getSource())).setText("Restart");
}
else
{
timer.restart();
((JButton)(e.getSource())).setText("Pause");
}
}
});
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize( 800, 600 );
f.setVisible( true );
}
public void changeImage()
{
pane.setImage( (Image)imageList.get( currentImage ) );
currentImage++;
if( currentImage == IMAGE_COUNT )
{
currentImage = 0;
}
pane.changeImage();
}
public void startShow()
{
timer = new Timer( 10000, new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
changeImage();
}
});
timer.setInitialDelay(0);
timer.start();
}
public void actionPerformed( ActionEvent e )
{
if( e.getSource() == open )
{
doOpen();
}
}
private void doOpen() {
// Carry out the Open command by letting the user specify
// the file to be opened and reading up to 100 lines from
// that file. The text from the file replaces the text
// in the JTextArea.
File file; // The file that the user wants to open.
JFileChooser chooser; // File dialog that lets the user specify a file.
chooser = new JFileChooser(new File("myslideshow.txt"));
chooser.setDialogTitle("Open File...");
int action = chooser.showOpenDialog(f);
if (action != JFileChooser.APPROVE_OPTION) {
// User canceled the dialog, or an error occurred.
return;
}
file = chooser.getSelectedFile();
try {
// Read lines from the file until end-of-file is detected,
// or until 100 lines have been read. The lines are added
// to the JTextArea, with a line feed after each line.
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
j.setText("");
// int lineCt = 0;
line = in.readLine();
while ( line != null )
{
j.append(line + "\n");
System.out.println( line );
images.add(line);
line = in.readLine();
}
in.close();
System.out.println(images.size());
}
catch (Exception e) {
// Some error has occurred while trying to read the file.
// Show an error message.
JOptionPane.showMessageDialog(f,
"Sorry, some error occurred:\n" + e.getMessage());
}
}
}

