Dissapearing Image

I am having trouble with the following code, which reads in a set of .png images that are named 1 -26 respectively, and outputs 25 of them in a grid. I draw each image to an offscreen image, back, which I then draw to the JFrame.

When I run the program, I see the image for a second (actually much less time) and then it dissapears.

import javax.swing.*;

import java.awt.*;

import java.awt.image.*;

publicclass wordtestextends JFrame{

ImageIcon lett;

ImageIcon [] letters;

ImageIcon correct;

ImageIcon incorrect;

ImageIcon outOfPlace;

public wordtest(){

letters =new ImageIcon [26];

BufferedImage back =new BufferedImage(230,230,BufferedImage.TYPE_INT_ARGB_PRE);// image to which i draw the 25 sepatate images

outOfPlace =new ImageIcon("outofplace.png");

try{// loading the 26 images

for(int i = 0; i < 26; i++){

System.out.println("Image pre-Loaded");

lett =new ImageIcon((i +1) +".png");

letters[i] = lett;

}

}

catch(Exception e){

System.out.println("File loading Exception");

System.out.println(e);

}

setSize(500,500);//set size of JFrame

//validate();

Graphics2D g = back.createGraphics();//get the graphics context for off screen image

int curr = 0;//counter for drawing each image from the array

try{

for(int j = 0; j < 5; j++){

for (int k = 0; k < 5; k++){

System.out.println("Image pre drawn");

g.drawImage(letters[curr].getImage(), j*46,k*46,null);

System.out.println("Image drawn");

curr++;

}

}

}

catch(Exception e ){

System.out.println("printing exception");

System.out.println(e);

}

g.dispose();

setVisible(true);

Graphics f = getGraphics();

f.drawImage(back,10,10,null);//draw offscreen image

f.dispose();

//validate();

}

publicstaticvoid main(String[] args){

wordtest larry =new wordtest();

}

}

[3966 byte] By [dbonhoppera] at [2007-11-26 18:50:46]
# 1

import java.awt.*;

import java.awt.font.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class WT extends JFrame{

ImageIcon[] letters;

ImageIcon outOfPlace;

ImageComponent imageComponent;

public WT() {

generateImagesToLoad();

ImageIcon[] icons = loadImages();

BufferedImage image = makeImage(icons);

imageComponent = new ImageComponent(image);

getContentPane().add(imageComponent);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(500,500);//set size of JFrame

setVisible(true);

}

private ImageIcon[] loadImages() {

ImageIcon[] letters = new ImageIcon [26];

outOfPlace = new ImageIcon("outofplace.png");

for(int j = 0; j < 26; j++){

letters[j] = new ImageIcon("wtImages/" + (j+1) + ".png");

int loadStatus = letters[j].getImageLoadStatus();

// ImageIcon doesn't throw exceptions so you have to

// check on the loading status/success on your own.

if(loadStatus != MediaTracker.COMPLETE) {

String errorStr = "letters["+j+"] loadStatus = ";

switch(loadStatus) {

case MediaTracker.ABORTED:

errorStr += "ABORTED";

break;

case MediaTracker.ERRORED:

errorStr += "ERRORED";

}

System.out.println(errorStr);

}

}

return letters;

}

private BufferedImage makeImage(ImageIcon[] letters) {

// image to which i draw the 25 sepatate images

BufferedImage back = new BufferedImage(230,230,BufferedImage.TYPE_INT_ARGB_PRE);

// get the graphics context for off screen image

Graphics2D g = back.createGraphics();

int curr = 0;//counter for drawing each image from the array

try{

for(int j = 0; j < 5; j++){

for (int k = 0; k < 5; k++){

g.drawImage(letters[curr].getImage(), j*46,k*46,null);

curr++;

}

}

} catch(Exception e ){

System.out.println("printing exception");

System.out.println(e);

}

g.dispose();

return back;

}

private void generateImagesToLoad() {

// Make up some images to save for loading above.

int w = 46;

int h = 46;

BufferedImage[] toSave = new BufferedImage[26];

for(int j = 0; j < toSave.length; j++) {

int type = BufferedImage.TYPE_INT_ARGB_PRE;

toSave[j] = new BufferedImage(w, h, type);

Graphics2D g2 = toSave[j].createGraphics();

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setPaint(Color.blue);

g2.drawRect(0,0,w-1,h-1);

g2.setPaint(Color.red);

g2.setFont(g2.getFont().deriveFont(16f));

FontRenderContext frc = g2.getFontRenderContext();

String s = String.valueOf(j+1);

float sw = (float)g2.getFont().getStringBounds(s, frc).getWidth();

float sh = g2.getFont().getLineMetrics(s, frc).getAscent();

float x = (w - sw)/2;

float y = (h + sh)/2;

g2.drawString(s, x, y);

}

// Save the images to file.

File folder = new File("wtImages");

if(!folder.exists()) {

folder.mkdir();

for(int j = 0; j < toSave.length; j++) {

File file = new File(folder, (j+1)+".png");

try {

ImageIO.write(toSave[j], "png", file);

} catch(IOException e) {

System.out.println("IO Error for " + file.getPath());

}

}

}

}

public static void main(String[] args){

WT larry = new WT();

}

}

class ImageComponent extends JPanel {

BufferedImage image;

public ImageComponent(BufferedImage image) {

this.image = image;

setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int x = (getWidth() - image.getWidth())/2;

int y = (getHeight() - image.getHeight())/2;

g.drawImage(image, x, y, this);

}

}

crwooda at 2007-7-9 6:24:49 > top of Java-index,Security,Cryptography...
# 2
[url http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html]Performing Custom Painting[/url][url http://java.sun.com/products/jfc/tsc/articles/painting/index.html]Painting in AWT and Swing[/url]
Rodney_McKaya at 2007-7-9 6:24:49 > top of Java-index,Security,Cryptography...