Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  help- loading and displaying 2D images  (Read 820 times)
0 Members and 1 Guest are viewing this topic.
Offline darkjava55

Senior Newbie





« Posted 2012-05-12 20:24:29 »

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.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
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));
     
   }

}

Offline Rorkien
« Reply #1 - Posted 2012-05-12 21:21:37 »

Where are the images located?

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

1  
2  
3  
4  
5  
6  
7  
8  
   private Image loadImage(String name) {
      try {
         return new ImageIcon(ImageIO.read(Assets.class.getResource(name))).getImage();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return null;
   }
Offline darkjava55

Senior Newbie





« Reply #2 - Posted 2012-05-12 21:31:23 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rorkien
« Reply #3 - Posted 2012-05-12 21:43:29 »

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
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-05-12 21:49:58 »

Where are the images located?

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
   private Image loadImage(String name) {
      try {
-         return new ImageIcon(ImageIO.read(Assets.class.getResource(name))).getImage();
+         return ImageIO.read(getClass().getResource(name));
      } catch (IOException e) {
         e.printStackTrace();
      }
      return null;
   }

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

Offline darkjava55

Senior Newbie





« Reply #5 - Posted 2012-05-12 23:02:55 »

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?



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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));
     
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-05-12 23:33:47 »

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

The images should load now.

Offline darkjava55

Senior Newbie





« Reply #7 - Posted 2012-05-13 00:00:51 »

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

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


with
1  
2  
3  
4  
5  
6  
7  
8  
   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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!