texturegroup.java
来自「Java mulitplayer strategy game. Adaptati」· Java 代码 · 共 140 行
JAVA
140 行
/*
* Created on 2005-10-04.
* $Id: TextureGroup.java,v 1.7 2005/10/31 00:15:45 overmindx Exp $
*/
package net.sf.jawp.j3d.texture;
import java.awt.Image;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.media.j3d.Texture;
import net.sf.jawp.util.Log;
import org.jdom.Element;
import com.sun.j3d.utils.image.TextureLoader;
/**
* TODO maciekm (2005-10-04) comment this type!
*
* <p>
* <b>Latest changed:</b><br/> $Date: 2005/10/31 00:15:45 $ $Author: overmindx $
* </p>
*
* @author Maciej Malecki
* @version $Revision: 1.7 $
*/
public final class TextureGroup
{
private static final Log LOG = Log.getLog(TextureGroup.class);
private final String name;
private final Map<String, Texture> textures = new HashMap<String, Texture>();
private final Random rnd = new Random(System.currentTimeMillis());
private final Map<String, Image> images = new HashMap<String, Image>();
TextureGroup(final Element configNode)
{
this.name = configNode.getAttributeValue("id");
final List textureNodes = configNode.getChildren("texture");
for (Object obj : textureNodes)
{
final Element textureNode = (Element) obj;
final String id = textureNode.getAttributeValue("id");
final String fileName = textureNode.getAttributeValue("file");
try
{
loadTexture(id, fileName);
}
catch ( final IOException ioe)
{
LOG.error( ioe, ioe);
//texture is not loaded ... is it error?
}
}
}
/**
* Returns number of textures defined within this group.
* @return number of textures defined within group.
*/
public int getCount()
{
return textures.size();
}
public Texture getTexture(final String id)
{
final Texture tex = textures.get(id);
if (tex == null)
{
throw new NoSuchElementException("Texture " + id + " not found in group " + name);
}
return tex;
}
public Image getImage(final String id)
{
return this.images.get(id);
}
public Texture getRandomTexture()
{
final int max = textures.size();
final int randomPick = rnd.nextInt(max);
final Iterator<Texture> iter = textures.values().iterator();
for (int i = 0; i < randomPick; ++i)
{
iter.next();
}
return iter.next();
}
public String getName()
{
return name;
}
void loadTexture(final String id, final String resourceName)
throws IOException
{
final InputStream is = getClass().getResourceAsStream(resourceName );
if ( is != null)
{
try
{
final Image img = ImageIO.read( is);
final Texture tex = new TextureLoader(img, null).getTexture();
textures.put(id, tex);
images.put( id, img);
}
finally
{
is.close();
}
}
else
{
throw new FileNotFoundException ( resourceName);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?