Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. package test;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.image.BufferedImage;
  7. import java.io.IOException;
  8. import java.net.HttpURLConnection;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import java.net.URLConnection;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.imageio.ImageIO;
  15. import javax.swing.*;
  16.  
  17. class ImageTest {
  18. public static void main(String[] args) throws IOException {
  19. getPicture("https://static1.e621.net/data/43/1d/431de47433a927af2b84462044535ed1.png");
  20. }
  21.  
  22. public static void getPicture(String path) throws MalformedURLException, IOException {
  23. URL url = new URL(path);
  24. HttpURLConnection connection = (HttpURLConnection) url
  25. .openConnection();
  26. connection.setRequestProperty(
  27. "User-Agent",
  28. "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");
  29.  
  30. BufferedImage image = ImageIO.read(connection.getInputStream());
  31. JLabel label = new JLabel(new ImageIcon(image));
  32. JFrame f = new JFrame();
  33. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. f.getContentPane().add(label);
  35. f.pack();
  36. f.setLocation(200,200);
  37. f.setVisible(true);
  38.  
  39. ActionListener actionListener = new ActionListener() {
  40. public void actionPerformed(ActionEvent actionevent) {
  41. try {
  42. test();
  43. } catch (Exception ex) {
  44. Logger.getLogger(ImageTest.class.getName()).log(Level.SEVERE, null, ex);
  45. }
  46. System.out.println("I was selected");
  47. }
  48. };
  49. JButton startButton = new JButton("Start");//The JButton name.
  50. f.add(startButton, BorderLayout.SOUTH);//Add the button to the JFrame.
  51. startButton.addActionListener(actionListener);//Reads the action.
  52.  
  53.  
  54. }
  55. public static void test() throws Exception {
  56. getPicture("https://static1.e621.net/data/0f/46/0f46c48f0cd11e22caff46a307d91034.jpg");
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement