Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.image.BufferedImage;
  3. import java.io.*;
  4. import javax.imageio.ImageIO;
  5. import javax.swing.*;
  6.  
  7. public class LoadAndShow extends JPanel {
  8.     BufferedImage image;
  9.     Dimension size = new Dimension();
  10.  
  11.     public LoadAndShow(BufferedImage image) {
  12.         this.image = image;
  13.         size.setSize(image.getWidth(), image.getHeight());
  14.     }
  15.  
  16.     /**
  17.      * Drawing an image can allow for more
  18.      * flexibility in processing/editing.
  19.      */
  20.     protected void paintComponent(Graphics g) {
  21.         // Center image in this component.
  22.         int x = (getWidth() - size.width)/2;
  23.         int y = (getHeight() - size.height)/2;
  24.         g.drawImage(image, x, y, this);
  25.     }
  26.  
  27.     public Dimension getPreferredSize() { return size; }
  28.  
  29.     public static void main(String[] args) throws IOException {
  30.         String path = "images/hawk.jpg";
  31.         BufferedImage image = ImageIO.read(new File(path));
  32.         LoadAndShow test = new LoadAndShow(image);
  33.         JFrame f = new JFrame();
  34.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         f.add(new JScrollPane(test));
  36.         f.setSize(400,400);
  37.         f.setLocation(200,200);
  38.         f.setVisible(true);
  39.         //showIcon(image);
  40.     }
  41.  
  42.     /**
  43.      * Easy way to show an image: load it into a JLabel
  44.      * and add the label to a container in your gui.
  45.      */
  46.     private static void showIcon(BufferedImage image) {
  47.         ImageIcon icon = new ImageIcon(image);
  48.         JLabel label = new JLabel(icon, JLabel.CENTER);
  49.         JOptionPane.showMessageDialog(null, label, "icon", -1);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement