Created
July 23, 2018 06:40
-
-
Save TW2/f30032a66ad2f171c89d13aab8ee1746 to your computer and use it in GitHub Desktop.
A GIF on a JPanel that works with small images. (We would prefer using JLabel for GIF!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gifinpaneldemo; | |
import java.awt.image.BufferedImage; | |
/** | |
* | |
* @author util2 | |
*/ | |
public class Gif { | |
private BufferedImage image = null; | |
private float delay = 1000; | |
public Gif() { | |
} | |
public Gif(BufferedImage image, int delay) { | |
this.image = image; | |
this.delay = delay; | |
} | |
public void setImage(BufferedImage image) { | |
this.image = image; | |
} | |
public BufferedImage getImage() { | |
return image; | |
} | |
public void setDelay(float delay) { | |
this.delay = delay; | |
} | |
public float getDelay() { | |
return delay; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gifinpaneldemo; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.imageio.ImageIO; | |
import javax.imageio.ImageReader; | |
import javax.imageio.metadata.IIOMetadata; | |
import javax.imageio.metadata.IIOMetadataNode; | |
import javax.imageio.stream.ImageInputStream; | |
import javax.swing.ImageIcon; | |
import javax.swing.JPanel; | |
/** | |
* | |
* @author util2 | |
*/ | |
public class ImagePanel extends JPanel implements Runnable { | |
private String imagePath = null; | |
private Image img = null; | |
private Repeat repeat = Repeat.NoRepeat; | |
private int width = 0, height = 0; | |
private Thread animationTH = null; | |
public ImagePanel(String imgFullPath, Repeat repeat) { | |
this.imagePath = imgFullPath; | |
this.repeat = repeat; | |
init(); | |
} | |
private void init(){ | |
setOpaque(true); | |
setVisible(true); | |
ImageIcon ii = new ImageIcon(imagePath); | |
img = ii.getImage(); | |
width = ii.getIconWidth(); | |
height = ii.getIconHeight(); | |
if(imagePath.endsWith(".gif") == true){ | |
animationTH = new Thread(this); | |
animationTH.start(); | |
} | |
} | |
@Override | |
public void paint(Graphics g) { | |
if(img != null){ | |
int x, y; | |
switch(repeat){ | |
case NoRepeat: | |
x = 0; | |
y = 0; | |
g.drawImage(img, x, y, null); | |
break; | |
case RepeatX: | |
x = 0; | |
y = 0; | |
while(x < getWidth()){ | |
g.drawImage(img, x, y, null); | |
x += width; | |
} | |
break; | |
case RepeatY: | |
x = 0; | |
y = 0; | |
while(y < getHeight()){ | |
g.drawImage(img, x, y, null); | |
y += height; | |
} | |
break; | |
case RepeatXY: | |
x = 0; | |
y = 0; | |
while(y < getHeight()){ | |
while(x < getWidth()){ | |
g.drawImage(img, x, y, null); | |
x += width; | |
} | |
x = 0; | |
y += height; | |
} | |
break; | |
case RepeatCountX: | |
//x = 0; | |
y = 0; | |
for(int i=0; i < repeat.getCountX(); i++){ | |
x = i * width; | |
g.drawImage(img, x, y, null); | |
} | |
break; | |
case RepeatCountY: | |
x = 0; | |
//y = 0; | |
for(int i=0; i < repeat.getCountY(); i++){ | |
y = i * height; | |
g.drawImage(img, x, y, null); | |
} | |
break; | |
case RepeatCountXY: | |
//x = 0; | |
//y = 0; | |
for(int i=0; i < repeat.getCountX(); i++){ | |
for(int j=0; j < repeat.getCountY(); j++){ | |
x = i * width; | |
y = j * height; | |
g.drawImage(img, x, y, null); | |
} | |
} | |
break; | |
} | |
} | |
} | |
@Override | |
public void run() { | |
if(imagePath.endsWith(".gif")){ | |
try{ | |
List<Gif> gifs = gifSetup(imagePath); | |
while(true){ | |
for(Gif gif : gifs){ | |
img = gif.getImage(); | |
repaint(); | |
java.util.concurrent.TimeUnit.MILLISECONDS.sleep(((long)gif.getDelay())*10L); | |
} | |
} | |
} catch (IOException | InterruptedException e){ | |
Logger.getLogger(Image.class.getName()).log(Level.SEVERE, null, e); | |
} | |
} | |
} | |
private List<Gif> gifSetup(String path) throws IOException{ | |
List<Gif> gifs = new ArrayList<>(); | |
ImageReader reader = ImageIO.getImageReadersBySuffix("GIF").next(); | |
try (ImageInputStream in = ImageIO.createImageInputStream(new File(path))) { | |
reader.setInput(in); | |
IIOMetadataNode graphicsControlExtensionNode = null; | |
for (int i = 0, count = reader.getNumImages(true); i < count; i++){ | |
BufferedImage image = reader.read(i); | |
IIOMetadata metadata = reader.getImageMetadata(i); | |
String metaFormatName = metadata.getNativeMetadataFormatName(); | |
IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(metaFormatName); | |
if(graphicsControlExtensionNode == null){ | |
for (int j = 0; j < root.getLength(); j++){ | |
if (root.item(j).getNodeName().equalsIgnoreCase("GraphicControlExtension")){ | |
graphicsControlExtensionNode = (IIOMetadataNode) root.item(j); | |
break; | |
} | |
} | |
} | |
if(graphicsControlExtensionNode == null){ | |
return null; | |
} | |
int delay = Integer.parseInt(graphicsControlExtensionNode.getAttribute("delayTime")); | |
gifs.add(new Gif(image, delay)); | |
} | |
} | |
reader.dispose(); | |
return gifs; | |
} | |
public enum Repeat{ | |
NoRepeat("NoRepeat"), | |
RepeatX("RepeatX"), | |
RepeatY("RepeatY"), | |
RepeatXY("RepeatXY"), | |
RepeatCountX("RepeatCountX"), | |
RepeatCountY("RepeatCountY"), | |
RepeatCountXY("RepeatCountXY"); | |
int countX = 1; | |
int countY = 1; | |
String name; | |
Repeat(String name){ | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setCountX(int countX) { | |
this.countX = countX; | |
} | |
public int getCountX() { | |
return countX; | |
} | |
public void setCountY(int countY) { | |
this.countY = countY; | |
} | |
public int getCountY() { | |
return countY; | |
} | |
public static Repeat getRepeat(String name){ | |
Repeat rp = NoRepeat; | |
for(Repeat search : values()){ | |
if(search.getName().equalsIgnoreCase(name)){ | |
rp = search; | |
break; | |
} | |
} | |
return rp; | |
} | |
@Override | |
public String toString() { | |
return name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment