Image Not Draw in GUI wehen Starting

This first step of GUI i design for a Java Application.

But my loading image ( hard code to the url) will not display in when GUI starts.

After display the blank frame I have to manually bit resize(using mouse) the frame to see the image.

_

import java.awt.BorderLayout;

import java.net.URL;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Example extends javax.swing.JPanel {

private JFrame frame ;

java.net.URL url;

ImageIcon icon;

JPanel buttonPanel;

JButton but1;

String curDir;

public Example(){

super();

frame = new JFrame();

frame.setSize(500,500);

frame.setVisible(true);

buttonPanel = new javax.swing.JPanel();

frame.getContentPane().add(this,BorderLayout.CENTER);

url = getClass().getResource("/feature.jpg");

buttonPanel.add(but1);

frame.addWindowListener(new java.awt.event.WindowAdapter() {

public void windowClosing(java.awt.event.WindowEvent evt) {

exitForm(evt);

frame.repaint();

}

});

java.awt.Dimension dim = getToolkit().getScreenSize();

frame.setLocation((int)dim.getWidth()/2 - 100, (int)dim.getHeight()/2 - 100);

frame.getContentPane().add(buttonPanel,BorderLayout.SOUTH);

frame.setSize(500,500);

frame.repaint();

}

public void paint(java.awt.Graphics g) {

if(url!=null)

{

icon = new javax.swing.ImageIcon(url);

java.awt.Image image = icon.getImage();

g.drawImage(image, 2, 20, image.getWidth(this), image.getHeight(this), this);

frame.repaint();

}

}

private void exitForm(java.awt.event.WindowEvent evt) {

System.exit(0);

}

public static void main(String d[]){

new Example();

}

}

[1949 byte] By [007Lakmala] at [2007-11-26 23:46:46]
# 1

what you are seeing is what is 'displayable' when you call

frame.setVisible(true);

if you add components after this call, they will not be seen until a repaint()

e.g. resizing the frame

move the above line to the end of the constructor

(and use code tags when posting code)

Michael_Dunna at 2007-7-11 15:20:27 > top of Java-index,Desktop,Core GUI APIs...
# 2

if you add components after this call, they will not be seen until a repaint() e.g. resizing the frame

He seems to have implemented a continuous paint loop, though, so I don't think we're short of repaints :o)

Resizing the frame also results in a call to validate(), which you should call after adding components and before calling repaint(). Without this, the changes in teh component hierarchy will not be displayed.

Your paint() implementation is pretty duff - firstly you should override paintComponent(), secondly it doesn't call super.paint() and thirdly it results in that perpetual paint cycle which will hog the processor for no reason.

itchyscratchya at 2007-7-11 15:20:27 > top of Java-index,Desktop,Core GUI APIs...