Reputation: 833
package main;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class ImageTest extends JFrame {
public static void main(String[] args) {
DisplayMode displayMode;
if (args.length == 3) {
displayMode = new DisplayMode(Integer.parseInt(args[0]),
Integer.parseInt(args[1]), Integer.parseInt(args[2]),
DisplayMode.REFRESH_RATE_UNKNOWN);
} else {
displayMode = new DisplayMode(800, 600, 16,
DisplayMode.REFRESH_RATE_UNKNOWN);
}
ImageTest test = new ImageTest();
test.run(displayMode);
}
private SimpleScreenManager screen;
private boolean imagesLoaded;
private Image bgImage;
private Image opaqueImage;
private Image transparentImage;
private Image translucentImage;
private Image antiAliasedImage;
private void run(DisplayMode displayMode) {
setBackground(Color.blue);
setForeground(Color.white);
setFont(new Font("Dialog", Font.PLAIN, 24));
imagesLoaded = false;
screen = new SimpleScreenManager();
try {
screen.setFullScreen(displayMode, this);
loadImages();
try {
Thread.sleep(10000);
} catch (Exception e) {
}
} catch (Exception e) {
} finally {
screen.restoreScreen();
}
}
private void loadImages() {
bgImage = loadImage("/images/background.png");
opaqueImage = loadImage("/images/opaque.png");
transparentImage = loadImage("/images/transparent.png");
translucentImage = loadImage("/images/translucent.png");
antiAliasedImage = loadImage("/images/antialiased.png");
imagesLoaded = true;
repaint();
}
private Image loadImage(String fileName) {
return new ImageIcon(fileName).getImage();
}
public void paint(Graphics g) {
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
g.drawImage(opaqueImage, 0, 0, this);
if (imagesLoaded) {
g.drawImage(bgImage, 0, 0, null);
drawImage(g, opaqueImage, 0, 0, "Opaque");
drawImage(g, transparentImage, 320, 0, "Transparent");
drawImage(g, translucentImage, 0, 300, "Translucent");
drawImage(g, antiAliasedImage, 320, 300,
"Translucent (Anti-Aliased)");
} else {
g.drawString("Loading Images...", 5, 24);
}
}
public void drawImage(Graphics g, Image image, int x, int y, String caption) {
g.drawImage(image, x, y, this);
g.drawString(caption, x + 5, y + 24 + image.getHeight(null));
}
}
There are no errors!, the program runs, it displays the text, but not the images. Which means that loadImages() works, it must be a mistake in my paint method. What am I doing wrong!?!?
I don't see what is wrong with my path:
Upvotes: 1
Views: 290
Reputation: 6870
If you take a look at the ImageIcon
source you will notice that the ImageIcon(String)
constructor calls another constructor
ImageIcon(String filename, String description) {
image = Toolkit.getDefaultToolkit().getImage(filename);
if (image == null) {
return;
}
this.filename = filename;
this.description = description;
loadImage(image);
}
and .getImage()
public Image getImage() {
return image;
}
If it fails to load an image that image will simply be null without throwing any errors. Your code fails (silently) to load the image (check this with a System.out.println(image)
most likely because of an incorrect filepath.
Edit to your comments: I prefer ImageIO
to load my files feeding it an inputstream. It is more verbose and has the added benefit of letting me load files from within jars. Change
private Image loadImage(String fileName) {
return new ImageIcon(fileName).getImage();
}
to
private Image loadImage(String fileName) {
return ImageIO.read(getClass().getResourceAsStream(fileName));
}
Upvotes: 3