Skip to content

Instantly share code, notes, and snippets.

@mnd999
Last active August 29, 2015 14:08
Show Gist options
  • Save mnd999/3380b41f21ab981ffb66 to your computer and use it in GitHub Desktop.
Save mnd999/3380b41f21ab981ffb66 to your computer and use it in GitHub Desktop.
package csssprite;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class CssBox {
public static void main(String[] args) throws Exception {
final JFrame f = new JFrame();
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
String url = "http://www.bbc.co.uk";
String url2 = "http://icanhas.cheezburger.com/";
String url3 = "http://slashdot.org/";
getImages(url3).stream().forEach(i -> {
f.getContentPane().getGraphics().drawImage(i, 0, 0, null);
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
System.exit(0);
}
private static List<BufferedImage> getImages(String urlStr) throws Exception {
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
StringBuilder b = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream()))) {
String l = br.readLine();
while (l != null) {
l = br.readLine();
b.append(l);
}
}
System.out.println(b);
Pattern p = Pattern.compile("<img.*?src=\"([^\"]*)\".*?>",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(b.toString());
List<BufferedImage> imgs = new ArrayList<BufferedImage>();
while (m.find()) {
String imgurl = m.group(1);
System.out.println(imgurl);
if (imgurl.startsWith("//")) imgurl = "http:" + imgurl;
if (imgurl.startsWith("http")) {
URLConnection c = new URL(imgurl).openConnection();
imgs.add(ImageIO.read(c.getInputStream()));
}
}
return imgs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment