Created
October 20, 2015 01:34
-
-
Save AnEmortalKid/8382c3d952c59d0c7d9d to your computer and use it in GitHub Desktop.
displays a scrubbed image on a panel
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 com.anemortalid.essex.whatson.scrape; | |
import java.awt.Graphics; | |
import java.awt.image.BufferedImage; | |
import java.io.InputStream; | |
import java.net.URL; | |
import javax.imageio.ImageIO; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
/** | |
* Lets you pull down that image and see it | |
* | |
*/ | |
public class ImageDisplayer extends JFrame { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
public ImageDisplayer(String urlStr) { | |
System.out.println("Constructing panel"); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
try { | |
URL url = new URL(urlStr); | |
System.out.println("Opening connection"); | |
InputStream inputStream = url.openConnection().getInputStream(); | |
System.out.println("Stream Open"); | |
BufferedImage bi = ImageIO.read(inputStream); | |
System.out.println("Image read"); | |
this.add(new ImagePanel(bi)); | |
int width = bi.getWidth(); | |
int height = bi.getHeight(); | |
setSize(width, height); | |
} catch (Exception exception) { | |
exception.printStackTrace(); // yolo namean | |
} | |
} | |
private class ImagePanel extends JPanel { | |
private BufferedImage image; | |
private ImagePanel(BufferedImage bi) { | |
image = bi; | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); | |
} | |
} | |
public static void main(String[] args) { | |
String url = "http://www.essexstudent.com/asset/Event/6006/NEW-Blades-Logo.png"; | |
new ImageDisplayer(url).setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment