Reputation: 655
I am new to Applets, and am trying to get comfortable with some basics - like how to display a jpg image in one. I've read what I think are the relevant parts of the Java Tutorials on Applets, and I don't understand why this doesn't work, all I get is a blank applet area. "/cards/as.jpg" - that file exists in subdirectory of the directory the applet is running from. Can anyone see what I'm doing wrong? I'm at a loss.
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestApplet extends JApplet {
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
}
catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
private void createGUI() {
JPanel newContentPane = new JPanel();
ImageComponent card = new ImageComponent();
card.setImage(getCodeBase() + "/cards/as.jpg");
newContentPane.add(card);
newContentPane.setOpaque(true);
setContentPane(newContentPane);
}
}
import java.awt.*;
import javax.swing.JComponent;
import javax.imageio.*;
import java.net.*;
import java.io.*;
/**
A component that draws an Image.
*/
public class ImageComponent extends JComponent {
private Image image;
private String url;
public ImageComponent() {
image = null;
url = null;
}
public void setImage(String urlCardName) {
url = urlCardName;
if (url != null) {
try {
image = ImageIO.read(new URL(url));
}
catch (IOException e)
{
e.printStackTrace();
}
}
else {
image = null;
}
this.repaint();
}
public void paintComponent(Graphics g) {
if (image == null) return;
// draw the images in the upper-left corner of the component
g.drawImage(image, 0, 0, null);
}
}
Upvotes: 1
Views: 997
Reputation: 324197
Check to see how large your ImageComponent is when rendered -- I'll bet you it's very small, 0 size even.
This is because you never set a preferred size for you component.
The real question is why are you drawing the image yourself. There is no need to create a custom component. Just use a JLabel with a Icon and this would not be a problem.
Upvotes: 2
Reputation: 285430
I think it's your layouts. Normally a contentPane uses BorderLayout by default, but when you use your own JPanel as contentPane, you're using the JPanel default layout which is FlowLayout, and components added to a container using FlowLayout won't expand to fill the container as they would with BorderLayout. Check to see how large your ImageComponent is when rendered -- I'll bet you it's very small, 0 size even.
A solution is to use the contentPane that's already present in the applet with its default BorderLayout, or else if you must use your own JPanel, give it a BorderLayout and add the new ImageComponent BorderLayout.CENTER. For example:
JPanel newContentPane = new JPanel();
newContentPane.setLayout(new BorderLayout()); // *** added this
ImageComponent card = new ImageComponent();
card.setImage(getCodeBase() + "/cards/as.jpg");
newContentPane.add(card, BorderLayout.CENTER); // *** changed
Upvotes: 2