Image problem
I am having a problem is that why the following image will not display?
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
publicclass testINGextends JPanel{
Image texture;
public testING(){
texture = Toolkit.getDefaultToolkit().getImage("texture.jpg");
}
publicvoid paint(Graphics g){
g.drawImage(texture, 10, 10,this);
super.paint(g);
}
publicstaticvoid main(String [] args){
JFrame f =new JFrame("Test");
f.pack();
f.setSize(800, 420);
f.setVisible(true);
}
}
[1361 byte] By [
AhBoY84a] at [2007-10-2 4:05:13]

You haven't created an instance of testING, nor have you added it to your JFrame. regards,Owen
I'm sorry I am still quite new how do add to the Frame
f.setContentPane( new testING() );
still the same no image
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class testING extends JPanel {
Image texture;
public testING() {
texture = Toolkit.getDefaultToolkit().getImage("982.JPEG");
}
public void paint(Graphics g) {
g.drawImage(texture, 10, 10, this);
super.paint(g);
}
public static void main(String [] args) {
JFrame f = new JFrame("Test");
f.setContentPane( new testING() );
f.setSize(800, 420);
f.setVisible(true);
}
}
- call super.paint before you paint the image- call f.repaint() after f.setVisible(true)
why it does not work when i shift all into the constructor?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class testING extends JPanel {
Image texture;
JFrame f;
public testING() {
texture = Toolkit.getDefaultToolkit().getImage("texture.jpg");
f= new JFrame("Test");
//f.setContentPane( new testING() );
f.setSize(800, 420);
f.setVisible(true);
f.repaint();
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(texture, 10, 10, this);
}
public static void main(String [] args) {
testING t = new testING();
}
}
> why it does not work when i shift all into the
> constructor?
Why should it ? Your frame is emtpy.
You want to display a background image, don't you ?
If you don't, you should rather put your image in a JLabel (using an ImageIcon). Simpler, cleaner, and the like.
If i have created a frame in the constructor how should i go about doing it?
If think you really should work your way through [url http://java.sun.com/docs/books/tutorial/reallybigindex.html#uiswing]this tutorial[/url] first.
If all you want to do is simply display an image, the JLabel + ImageIcon combination is a simple way to do it:
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ImageTest {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.starstore.com/acatalog/Seychelles-sunset-_L-poster.jpg");
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JLabel(new ImageIcon(url)));
f.pack();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
> If think you really should work your way through [url
> http://java.sun.com/docs/books/tutorial/reallybigindex
> .html#uiswing]this tutorial[/url] first.
Hi is it because that the paint() is being invoke ince the application starts. so the constructor is not beng call yet? Can i explain in this way?
> Hi is it because that the paint() is being invoke
> ince the application starts. so the constructor is
> not beng call yet? Can i explain in this way?
No. The constructor is always called, since you state new testING() in your main.
But you never add your custom component (testING) to the component hierarchy of the frame, which is the only thing shown.
As for further information, please refer to the tutorial(s).
so no matter what there is no way to add all in the constructor is it?
> so no matter what there is no way to add all in the> constructor is it?No. You really need to get your basics up first before proceeding further.Richard West