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]
# 1
You haven't created an instance of testING, nor have you added it to your JFrame. regards,Owen
omcgoverna at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 2
I'm sorry I am still quite new how do add to the Frame
AhBoY84a at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 3
f.setContentPane( new testING() );
da.futta at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 4

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);

}

}

AhBoY84a at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 5
- call super.paint before you paint the image- call f.repaint() after f.setVisible(true)
da.futta at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 6

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();

}

}

AhBoY84a at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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.

da.futta at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 8
If i have created a frame in the constructor how should i go about doing it?
AhBoY84a at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 9
If think you really should work your way through [url http://java.sun.com/docs/books/tutorial/reallybigindex.html#uiswing]this tutorial[/url] first.
da.futta at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 10

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);

}

});

}

}

DrLaszloJamfa at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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?

AhBoY84a at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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).

da.futta at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 13
so no matter what there is no way to add all in the constructor is it?
AhBoY84a at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...
# 14
> 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
freesoft_2000_2a at 2007-7-15 23:27:57 > top of Java-index,Java Essentials,Java Programming...