help- loading and displaying 2D images

I have been Having difficulty getting an image file to load and be displayed properly. The code below gives me no errors when I try to run it but all I get is the word opaque printed on a blue background. I am trying to use an example in a Java game programing book but it is outdated. The book is written for java 1.4 if that helps anybody. Keep in mind I defined the class SimpleScreenManger, should have been SimpleScreenManager, and have used it for other code in the same project so its not the problem. Any tips or advise are welcome.

import java.awt.*;
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(1440,900,16, 
						DisplayMode.REFRESH_RATE_UNKNOWN);
		}
		ImageTest test = new ImageTest() ;
		test.run(displayMode);
		
	}
	
	private static final int FONT_SIZE = 24;
	private static final long DEMO_TIME = 5000;
	
	private SimpleScreenManger screen;
	private Image bgImage;
	private Image opaqueImage;
	private boolean imagesLoaded;
	
	public void run(DisplayMode displayMode) {
		setBackground(Color.blue);
		setForeground(Color.white);
		setFont (new Font("Dialog", Font.PLAIN, FONT_SIZE)) ;
		imagesLoaded = false;
		
		screen = new SimpleScreenManger();
		try {
			screen.setFullScreen(displayMode, this);
			loadImages();
			try {
				Thread.sleep(DEMO_TIME);
			}
			catch (InterruptedException ex) {}
		}
		finally {
			screen.restoreScreen();}
				
	}
	public void loadImages () {
		bgImage = loadImage("Background.jpg");
		opaqueImage = loadImage("opaquecircle.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 );
		}
		
		if (imagesLoaded = true) {
			g.drawImage(bgImage,0,0,null);
			drawImage (g,opaqueImage,320,0, "opaque");
			
		}
		else {
			g.drawString("Loading Images...", 5, FONT_SIZE);	
		}
	}

		public void drawImage (Graphics g ,Image image , int x, int y, String caption) {
			g.drawImage(image,x,y,null);
			g.drawString(caption , x +5, y + FONT_SIZE+ image.getHeight(null));
		
	}

}

Where are the images located?

Anyways, you can try using this code in the meantime:

	private Image loadImage(String name) {
		try {
			return new ImageIcon(ImageIO.read(Assets.class.getResource(name))).getImage();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

The files are located on my hard drive, I wasn’t sure if I needed the file location when I called them I’m the code or just the file name. Fyi I’m on a mac if that changes how I should refer to them

Well, if the images folder are in the build path, you can just refer to them like they are in the root folder
otherwise you need to write the entire image path

:o remove that whole “new ImageIcon(” and the “.getImage()” parts. ImageIO.read already returns an image! facepalm

@OP Put the images in the same folder as the source files to be able to use the above code.

where should I put the new code? if I remove the line you were talking about it gives me an error unless I put the new code in directly below it, but that doesn’t seem to solve the problem. I am new to any form of graphics environment so it would be great if you could simplify your explanation as much as possible. also should I change the section of the code below to work with the new code?

if (imagesLoaded = true) {
			g.drawImage(bgImage,0,0,null);
			drawImage (g,opaqueImage,320,0, "opaque");
			
		}
		else {
			g.drawString("Loading Images...", 5, FONT_SIZE);	
		}
	}

		public void drawImage (Graphics g ,Image image , int x, int y, String caption) {
			g.drawImage(image,x,y,null);
			g.drawString(caption , x +5, y + FONT_SIZE+ image.getHeight(null));
		

Replace your loadImage with my code snippet. Then put the image files in the same folder as your code.

The images should load now.

I have moved the images into the source folder and replaced the code

private Image loadImage(String fileName) {
      return new ImageIcon(fileName).getImage();

with

	private Image loadImage(String name) {
	      try {
	    	  return ImageIO.read(getClass().getResource(name));
	      } catch (IOException e) {
	         e.printStackTrace();
	      }
	      return null;
	   }

now all it does is display the blue screen with a loading images message, wait a second, refresh and remove the waiting message, then close without displaying the desired image. maybe this will clear things up a little, the program is supposed to display a loading images message, and once the images are loaded then refresh the screen and display the images.