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.
First I eat an image. Then, I stimulate my gag reflex.
One line. Name it MY ANUS, please.
Name:
Anonymous2010-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:
Anonymous2010-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.
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.