show white page when the image files in jar are missing

This commit is contained in:
huangyuhui committed 2017-02-25 19:14:08 +08:00
1 parent b4658463e1
commit 0dfd8af7d5
6 files changed
+113 -149

No files matched your search

@@ -3,21 +3,19 @@
*/
package org.jb2011.ninepatch4j;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.jb2011.ninepatch4j.GraphicsUtilities;
import org.jb2011.ninepatch4j.NinePatchChunk;
public class NinePatch {
public static final String EXTENSION_9PATCH = ".9.png";
private BufferedImage mImage;
private NinePatchChunk mChunk;
private final BufferedImage mImage;
private final NinePatchChunk mChunk;
public BufferedImage getImage() {
return this.mImage;
@@ -28,26 +26,20 @@ public class NinePatch {
}
public static NinePatch load(URL fileUrl, boolean convert) throws IOException {
BufferedImage image = null;
try {
image = GraphicsUtilities.loadCompatibleImage(fileUrl);
}
catch (MalformedURLException e) {
boolean is9Patch = fileUrl.getPath().toLowerCase().endsWith(".9.png");
return NinePatch.load(GraphicsUtilities.loadCompatibleImage(fileUrl), is9Patch, convert);
} catch (MalformedURLException e) {
return null;
}
boolean is9Patch = fileUrl.getPath().toLowerCase().endsWith(".9.png");
return NinePatch.load(image, is9Patch, convert);
}
public static NinePatch load(InputStream stream, boolean is9Patch, boolean convert) throws IOException {
BufferedImage image = null;
try {
image = GraphicsUtilities.loadCompatibleImage(stream);
}
catch (MalformedURLException e) {
return NinePatch.load(GraphicsUtilities.loadCompatibleImage(stream), is9Patch, convert);
} catch (MalformedURLException e) {
return null;
}
return NinePatch.load(image, is9Patch, convert);
}
/*
@@ -56,12 +48,12 @@ public class NinePatch {
*/
public static NinePatch load(BufferedImage image, boolean is9Patch, boolean convert) {
if (!is9Patch) {
if (!convert) return null;
if (!convert)
return null;
image = NinePatch.convertTo9Patch(image);
return new NinePatch(image);
} else {
} else
NinePatch.ensure9Patch(image);
}
return new NinePatch(image);
}
@@ -94,23 +86,19 @@ public class NinePatch {
int i = 0;
while (i < width) {
pixel = image.getRGB(i, 0);
if (pixel != 0 && pixel != -16777216) {
if (pixel != 0 && pixel != -16777216)
image.setRGB(i, 0, 0);
}
if ((pixel = image.getRGB(i, height - 1)) != 0 && pixel != -16777216) {
if ((pixel = image.getRGB(i, height - 1)) != 0 && pixel != -16777216)
image.setRGB(i, height - 1, 0);
}
++i;
}
i = 0;
while (i < height) {
pixel = image.getRGB(0, i);
if (pixel != 0 && pixel != -16777216) {
if (pixel != 0 && pixel != -16777216)
image.setRGB(0, i, 0);
}
if ((pixel = image.getRGB(width - 1, i)) != 0 && pixel != -16777216) {
if ((pixel = image.getRGB(width - 1, i)) != 0 && pixel != -16777216)
image.setRGB(width - 1, i, 0);
}
++i;
}
}
@@ -119,6 +107,11 @@ public class NinePatch {
BufferedImage buffer = GraphicsUtilities.createTranslucentCompatibleImage(image.getWidth() + 2, image.getHeight() + 2);
Graphics2D g2 = buffer.createGraphics();
g2.drawImage(image, 1, 1, null);
g2.setColor(Color.black);
g2.fillRect(0, 1, 1, 1);
g2.fillRect(1, 0, 1, 1);
g2.fillRect(1, image.getHeight() + 1, 1, 1);
g2.fillRect(image.getWidth() + 1, 1, 1, 1);
g2.dispose();
return buffer;
}
@@ -127,4 +120,3 @@ public class NinePatch {
return image.getSubimage(1, 1, image.getWidth() - 2, image.getHeight() - 2);
}
}