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

Pages: 1-

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 13:51

Read SICP

Name: Anonymous 2010-06-14 13:56

First I eat an image. Then, I stimulate my gag reflex.
One line. Name it MY ANUS, please.

Name: Anonymous 2010-06-14 14:36

Most ways are equivalently simple.  If I'm doing a throw away program with java that uses graphics, I make an applet to avoid the boilerplate of making a window.

import java.awt.*;
import java.applet.*;
public class a extends Applet{
    Image img;
    public void init(){
      img=getImage(getCodeBase(),"10x.JPG");
    }
    public void paint(Graphics g){       
        g.drawImage(img,0,0,null);       
    }
}

Note that because getImage is non-blocking, the image may not be loaded in time for the initial painting, use a blocking version like ImageIO.read() if this is an issue.

Don't mind the trolls, those that know how to program use languages that need 3rd party libraries to display an image.  Savages.

Name: Anonymous 2010-06-14 14:44

>>4
That's quite the simple solution, isn't it? I seem to remember having 5-6 lines of code used to load an image, but I can't recall why.

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.

Name: Anonymous 2010-06-14 14:50

>>6
Haha, me.
protected BufferedImage bi = null;
...
public ImagePanel(int x,int y, int w, int h) {

Name: Anonymous 2010-06-14 15:42

>>4
What are your run configurations for this?

Name: Anonymous 2010-06-14 15:43

>>8
Read SICP

Name: Anonymous 2010-06-14 16:43

>>8
just the default applet-run config with eclipse.

From the CL you use the appletviewer tool.

Name: Anonymous 2010-06-14 16:59

>>10
eclipse
IHBT

Name: Anonymous 2010-06-14 17:09

>>11
this is a thread about java, what do you expect

Name: Anonymous 2010-06-14 17:30

>>12
This is a thread about how /prog/ ``throws up an image in Java'', so by rights it should have one reply, as follows:

We don't.

Name: Anonymous 2011-02-04 11:49

Name: Anonymous 2013-01-19 14:21

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

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