Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Simplest way to draw images in Java

Name: Anonymous 2010-06-14 13:38

JFrame, ImageIO, Graphics2D, ImageBuffer... uhg
Lurking around I found there are a lot of complicated ways to do a simple thing.
So how does /prog/ throw up an image in java?
The fewest lines gets to name my first born.

Name: Anonymous 2010-06-14 14:45

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.lang.Exception;
import javax.swing.JPanel;

class ImagePanel extends JPanel {
   protected BuferedImage bi = null;
   public ImagePanel(x,y,w,h) {
      super();
      this.setLocation(x,y);
      this.setSize(w,h);
      this.setLayout(new BorderLayout());
   }

   public boolean loadImage(String s) {
      try {
         File f = new File(s);
         if(f.exists()) {
            bi = ImageIO.read(f);
            return true;
         }
      }
      catch(Exception e) { }
      return false;
   }

   @Override
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      if(this.bi != null) {
         g.drawImage(bi, this.getX(), this.getY(), this.getWidth(), this.getHeight(), null);
         this.repaint();
      }
   }
}

Wrote this some time ago but never did more than compile it, not even used it once; this should give you the basic idea, though.  Add it to a JFrame and play around.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List