i fixed my problem but..... how would i put something up to tell the user that the images are loading. it takes about 5 seconds to load. i drew a string that said "loading" but it took the string off after half the time. the way the loading works is this...
at the end of the init it sets a loaded boolean to false. then in the paint it says
if (!loaded)
{
g.setColor(Color.red);
g.drawString("loading", width/2, height/2, this);
g.drawImage("map2.png", 0, -6008, this);
g.drawImage("f-16.png", 0, -6008, this);
etc.....
loaded = true;
}
thx in advance for suggestions
Message was edited by:
stephensk8s
Load it in a separate thread and wait on that thread. This class is a new work in progress (modifying my old one) so take it with a grain of salt (like the missing messages class that can be easily yanked out). When you're ready to use it
Imageloader images = new ImageLoader();
images.add("/images/test.jpg"); //add all your images
images.run(); //run the thread
try
{
test1.join(); //wait for this thread to die
}
catch (InterruptedException interruptedException)
{
interruptedException.printStackTrace();
}
/*-
* Javoids -- Javoids is an asteroids based game (that look nothing like the original).
*
* Copyright (C) 1999-2007 Patrick Mallette
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* I can be reached at parickmallette@rogers.com
*/
package javoids2;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.Serializable;
import java.net.URL;
import java.util.HashMap;
import java.util.IllegalFormatException;
import javax.swing.JOptionPane;
import javoids2.Debug.Flag;
/* ImageLoader- */
/**
* A class to load images easily at once in a separate Thread.
* @author Patrick Mallette
*/
public class ImageLoader extends Thread implements Serializable
{
/** This is the version used for serializing/deserializing (storing/retrieving) this object */
private static final long serialVersionUID = 1;
/** a mapping of image names to the loaded images */
private HashMap<String,Image> images = new HashMap<String,Image>();
/**
* Load the images in a separate thread.
*/
/*
* (non-Javadoc)
* @see java.lang.Thread#run()
*/
@Override
public void run()
{
String errorName = "No Image";
System.out.printf(Messages.getString("ImageLoader.GetImages")); //$NON-NLS-1$
try
{
for (String name : this.images.keySet())
{
errorName = name;
URL url = this.getClass().getResource(name); //$NON-NLS-1$
if (Flag.debugImage.value)
System.out.printf(Messages.getString("ImageLoader.LoadingImage"),url.toString()); //$NON-NLS-1$
if (url != null)
this.images.put(name,Toolkit.getDefaultToolkit().getImage(url));
else
System.out.println(String.format("File \"%1$s\" not found.",name));
}
}
catch (NullPointerException nullPointerException)
{
JOptionPane.showMessageDialog(null,Messages.getString("ImageLoader.ErrorMissingFile") + errorName + Messages.getString("ImageLoader.ErrorProgramWillNotLoad") + nullPointerException.getMessage(),Messages.getString("ImageLoader.ErrorImageFileMissing"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
catch (IllegalFormatException illegalFormatException)
{
JOptionPane.showMessageDialog(null,Messages.getString("ImageLoader.ErrorMissingFile") + errorName + Messages.getString("ImageLoader.ErrorProgramWillNotLoad") + illegalFormatException.getMessage(),Messages.getString("ImageLoader.ErrorFileMissing"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
System.out.printf(Messages.getString("ImageLoader.GotAllImages")); //$NON-NLS-1$
}
/**
* @return the number of images
*/
public int size()
{
return this.images.size();
}
/**
* @param name the name (enum) of the item
* @return the image for the provided name
*/
public Image getImage(String name)
{
return this.images.get(name);
}
/**
* Scale an image from the list
* @param name the name (enum) of the item
* @param width the desired width of the images
* @param height the desired height of the image
* @param hints image rendering hints (see sun constants)
* @return the scaled image
*/
public Image getScaledImage(String name,int width,int height,int hints)
{
return this.images.get(name).getScaledInstance(width,height,hints);
}
/**
* @param name the name (enum) of the item
* @param image the image to associate with the provided name
*/
public void putImage(String name,Image image)
{
this.images.put(name,image);
}
/**
* Provide a String representation of this object.
* @return String A representation of the object for debugging.
*/
@Override
public String toString()
{
return String.format(Messages.getString("ImageLoader.ToString"),this.images); //$NON-NLS-1$
}
/**
* @param arguments the commang line arguments
*/
public static void main(String[] arguments)
{
ImageLoader test1 = new ImageLoader();
test1.putImage("/images/Ship1.gif",null);
test1.putImage("/images/Bullet1.gif",null);
System.out.println("Test 1.0 = " + test1.toString());
test1.run();
try
{
test1.join();
}
catch (InterruptedException interruptedException)
{
interruptedException.printStackTrace();
}
System.out.println("Test 2.0 = " + test1.toString());
System.out.println("Test 3.0 = " + test1.size());
test1.putImage("missing",null);
test1.run();
try
{
test1.join();
}
catch (InterruptedException interruptedException)
{
interruptedException.printStackTrace();
}
}
}
/* ImageLoader- */
Message was edited by:
patrickmallette
> >Load it in a separate thread and wait on that
> thread.
>
> I dont understand how to do multiple threads at once.
> all i really understand about threads is that they
> can be run at the same time. can someone explain the
> concept to me? i tried researching it, but to no avail
Did you see this?
http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
That seems like a good link. I wish I had found that while figuring this stuff out. I use code like this to load my text files, images, and sounds for my game. Before I did this I had issues with resources not being ready or sounds playing all at once as the system cached them as many java web sites used to suggest.
Basically a thread is a lightweight program that shares resources with your application.
In the example below even though thread1,2,3 are in order they might not finish loading the images in that order because each is a different bit of code that can be run out of order by the virtual machine and CPU.
If you can't understand that then read the tutorial at the links above it should really help you understand what is going on and how to do it.
Take care.
Imageloader images1 = new ImageLoader();
Imageloader images2 = new ImageLoader();
Imageloader images3 = new ImageLoader();
//try loading many large images for each thead
//in the run method print the file name being loaded when it starts and finishes
//this may give you a better idea of what is going on
images1.add("/images/test1.jpg");
images2.add("/images/test2.jpg");
images3.add("/images/test3.jpg");
//start the three threads loading the image(s) but don't wait for them to finish
images1.run();
images2.run();
images3.run();
System.out.println("Going to wait now.");
try
{
//this bit of code waits for all three threads to finish before running the printline
//this insures your resources will be loaded before you try using them
test1.join();
test2.join();
test3.join();
}
catch (InterruptedException interruptedException)
{
interruptedException.printStackTrace();
}
System.out.println("Finally done all three threads.");
> //start the three threads loading the image(s) but don't wait for them to finish
> images1.run();
> images2.run();
> images3.run();
To make it run on a different thread you need to activate the thread start method not the run. The start method runs a new thread and that run will run the 'run' method.
This code right now loads the images in sequence on the same thread so it does not help you.
sushik is correct those should be calls to start() nor run(). When I was writing some new tests I grabbed run from the auto-complete instead of start. Thanks for pointing that out. I have corrected my test.
To the OP, since the image loader extends the Thread class, when the start method is called, it will create a new thread to load the images in it's list.