Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- class ImageTest {
- public static void main(String[] args) throws IOException {
- getPicture("https://static1.e621.net/data/43/1d/431de47433a927af2b84462044535ed1.png");
- }
- public static void getPicture(String path) throws MalformedURLException, IOException {
- URL url = new URL(path);
- HttpURLConnection connection = (HttpURLConnection) url
- .openConnection();
- connection.setRequestProperty(
- "User-Agent",
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
- BufferedImage image = ImageIO.read(connection.getInputStream());
- JLabel label = new JLabel(new ImageIcon(image));
- JFrame f = new JFrame();
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.getContentPane().add(label);
- f.pack();
- f.setLocation(200,200);
- f.setVisible(true);
- ActionListener actionListener = new ActionListener() {
- public void actionPerformed(ActionEvent actionevent) {
- try {
- test();
- } catch (Exception ex) {
- Logger.getLogger(ImageTest.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println("I was selected");
- }
- };
- JButton startButton = new JButton("Start");//The JButton name.
- f.add(startButton, BorderLayout.SOUTH);//Add the button to the JFrame.
- startButton.addActionListener(actionListener);//Reads the action.
- }
- public static void test() throws Exception {
- getPicture("https://static1.e621.net/data/0f/46/0f46c48f0cd11e22caff46a307d91034.jpg");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement