Not using repaint() correctly?

Hello - I have a class which I have created which makes a window and then attempts to load a greyscale image into it. The image loads correctly if it is painted before the pane is displayed, but not after. The final application is going to have a button to press to display the image, so I need to load the image after the rest of the frame is displayed (and overwrite what image is already on the screen)

Because it is displaying correctly at first, I think I don't understand how repaint() works... any advice is helpful, code follows:

import javax.swing.*;

import java.awt.event.*;

import java.awt.image.*;

import java.awt.*;

import java.io.*;

publicclass Main

{

privatestatic ImageIcon icon;

privatestatic BufferedImage buffImage;

privatestatic Graphics g;

privatestatic JLabel image;

privatestatic JFrame frame;

publicstaticvoid main(String [] args)

{

frame =new JFrame();

frame.setSize(640, 480);

frame.setLocation(300, 300);

icon =new ImageIcon("blank.gif");

Image img = icon.getImage();

buffImage =new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

g = buffImage.getGraphics();

g.drawImage(img, 0, 0,null);

image =new JLabel(icon);

//setImage();//if called here - it loads the image

frame.getContentPane().add(image,"Center");

//setImage();//if called here - it loads the image

frame.setVisible(true);

setImage();//if called here - it is blank.gif

}

publicstaticvoid setImage()

{

finalint WIDTH = 162;

finalint HEIGHT = 128;

int x = 0;

int y = 0;

byte[] data =newbyte[WIDTH*HEIGHT*2];

try

{

System.out.println("Start");

FileInputStream stream =new FileInputStream("Patient000_2007-01-03_14-11-30_IM_1.raw");

stream.read(data);

for(int i=0; i<data.length/2; i++)

{

if(x>=WIDTH*2)

{

x=0;

y=y+2;

}

int value = 255-((byte)data[(i*2)] & 0xff);

g.setColor(new Color(value, value, value));

g.fillRect(x, y, 2, 2);

x=x+2;

}

//this is where I think I am messing up.

image =new JLabel(new ImageIcon(buffImage));

image.revalidate();

image.repaint();

frame.repaint();

frame.getContentPane().remove(image);

frame.getContentPane().add(image,"Center");

System.out.println("Done");

}catch(Exception e)

{

e.printStackTrace();

}

}

}

[4930 byte] By [CNelsona] at [2007-11-26 14:09:41]
# 1
See my example of using an Image and repaint here http://forum.java.sun.com/thread.jspa?forumID=20&threadID=746743
tjacobs01a at 2007-7-8 1:56:49 > top of Java-index,Security,Cryptography...
# 2

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class DynamicImage {

BufferedImage image;

JLabel label;

public DynamicImage() {

// You can load the image at construction.

image = loadImage();

}

private BufferedImage loadImage() {

BufferedImage bi = null;

try {

bi = ImageIO.read(new File("images/cougar.jpg"));

} catch(IOException e) {

System.out.println("read error: " + e.getMessage());

}

return bi;

}

private JScrollPane getContent() {

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(label = new JLabel(new ImageIcon(new byte[0])));

panel.add(new JLabel(new ImageIcon(image)));

return new JScrollPane(panel);

}

private JPanel getUIPanel() {

final JButton setImage = new JButton("set image");

final JButton removeImage = new JButton("remove image");

ActionListener l = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton button = (JButton)e.getSource();

ImageIcon icon = null;

if(button == setImage)

icon = new ImageIcon(image);

else if(button == removeImage)

icon = null;

label.setIcon(icon);

}

};

setImage.addActionListener(l);

removeImage.addActionListener(l);

JPanel panel = new JPanel();

panel.add(setImage);

panel.add(removeImage);

return panel;

}

public static void main(String[] args) {

DynamicImage test = new DynamicImage();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent());

f.getContentPane().add(test.getUIPanel(), "Last");

f.setSize(500,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-8 1:56:49 > top of Java-index,Security,Cryptography...