1

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: enter image description here

Steven
  • 833
  • 3
  • 11
  • 18
  • Absolute path error? Are those valid paths to the images? –  Aug 04 '13 at 05:13
  • yes, thoes are the correct paths for images. – Steven Aug 04 '13 at 05:14
  • I tried doing what I usually do: `(ImageIcon ii = new ImageIcon(this.getClass().getResource(path)); Image i = ii.getImage(); g.drawImage(i, 0, 0, this);` it works perfectly fine – Steven Aug 04 '13 at 05:16
  • Try without the leading back slash. –  Aug 04 '13 at 05:16
  • 1
    @Steven : Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), hopefully that might be able to help :-) – nIcE cOw Aug 04 '13 at 05:36
  • This works! Thank you. This is still bothering me though, because I don't understand what is wrong with my code. nonetheless, I got it working. thank you :P – Steven Aug 04 '13 at 05:43
  • 1
    @Steven : I personally keep myself away from IDEs, since I am a learner, so can not tell you exactly which IDE you are using. But I can give you a small hint, in quick words :-) "When you write `getClass().getResource("/images/background.png")` now the first forward slash means that the images folder should reside alongside `main` folder (which is the package containing .class files or sub-packages), though if you will take the first forward slash out and write `getClass().getResource("images/background.png")` it means that images folder must be present next to the `.class` file which" – nIcE cOw Aug 04 '13 at 15:32
  • 1
    __Continued...__ : contains this code literal :-). I hope you understood :-) how this thingy is working now :-) For the rest You're MOST WELCOME and KEEP SMILING :-) and yeah, please do add `@` before posting a comment to someone, else the message won't reach that person's inbox ever :( Do watch the last link in those links, I had posted as an answer, that gives you the directory structure too, in the comments of the code provided, for you to understand that a bit more better :-) – nIcE cOw Aug 04 '13 at 15:33
  • 1
    @nIcE cOw: I understand what you are saying. I posted a stack question a while back because an image wasn't displaying and the slash was the issue. I tried it both with and without the slash. I get the same result, but thank you. What you said makes sense. I will definitely use it for future reference. – Steven Aug 04 '13 at 21:28

1 Answers1

3

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));
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
arynaq
  • 6,710
  • 9
  • 44
  • 74
  • I don't see what is wrong with my path :/ also I tried it with and without / before the path – Steven Aug 04 '13 at 05:22
  • when I try the system.out.print... I get this `null null sun.awt.image.ToolkitImage@2e378b91 sun.awt.image.ToolkitImage@2e378b91 sun.awt.image.ToolkitImage@2e378b91 ` – Steven Aug 04 '13 at 05:25
  • it appears as it is initially null, then get's the image later – Steven Aug 04 '13 at 05:25