Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on June 19, 2011, 11:17:04 am

Title: Java - Load Images on Screen Display
Post by: Munchor on June 19, 2011, 11:17:04 am
I was trying to load 2 images in a display screen I made (inside a JFrame), but the screen appears empty and no images :/

Screen.java
Code: [Select]
import java.awt.*;
import javax.swing.JFrame;

public class Screen {

private GraphicsDevice vc; //Allows access to Graphics Card

public Screen() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}

public void setFullScreen(DisplayMode dm, JFrame window) {
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);

if (dm != null && vc.isDisplayChangeSupported()) {
try {
vc.setDisplayMode(dm);
} catch(Exception ex) {

}
}

}

public Window getFullScreenWindow() {
return vc.getFullScreenWindow();
}

public void restoreScreen() {
Window w = vc.getFullScreenWindow();
if (w != null) {
w.dispose();
}
vc.setFullScreenWindow(null);
}

}

Images.java
Code: [Select]
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class Images extends JFrame {
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
Images i = new Images();

i.run(dm);

}

private Screen s;
private Image bg;
private Image pic;
private boolean loaded;

//run method
public void run(DisplayMode dm) {
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));

loaded = false;

s = new Screen();

try {
s.setFullScreen(dm, this);
loadpics();
try {
Thread.sleep(5000);
} catch (Exception ex) {}
} finally {
s.restoreScreen();
}
}

public void loadpics() {
bg = new ImageIcon("background.jpeg").getImage();
pic = new ImageIcon("face.png").getImage();
loaded = true;
repaint();
}

public void paint(Graphics g) {
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D)g; //Typecasting g to Graphics2D
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}

if (loaded) {
g.drawImage(bg,0,0,null);
g.drawImage(pic,170,180,null);
}
}
}

I got this from one of thenewboston's tutorials and my code is supposedly equal to his, but it doesn't really work for me.

The images are saved in the same folder as the .class files, with the same names as in the code.

Thanks in advance.
Title: Re: Java - Load Images on Screen Display
Post by: nemo on June 19, 2011, 01:41:23 pm
could you show us which tutorial you're using? i see thenewboston has about 80 tutorials on java and i don't know which one your code references.



try this:
Code: [Select]
catch(Exception ex) {
System.out.println("An error occurred: " + ex);
}

instead of
Code: [Select]
catch(Exception ex) {

}

in the setFullScreen method of the Screen class.

also, put
Code: [Select]
System.out.println(vc.isDisplayChangeSupported());
before the line
Code: [Select]
if (dm != null && vc.isDisplayChangeSupported()) {

in the same method and class. if your program prints "false," your graphics card won't allow java to interact in a low-level way with it, and i don't know how to fix it. but here's a link (http://download.oracle.com/javase/6/docs/api/java/awt/GraphicsDevice.html#isDisplayChangeSupported()) describing what's wrong.

if you get "An error occurred: [something]," then copy/paste what the [something] says. although i haven't really dealt with the classes DisplayMode and GraphicsDevice, the rest of the code looks fine.
Title: Re: Java - Load Images on Screen Display
Post by: Munchor on June 19, 2011, 02:06:53 pm
Nemo, thanks a lot, I got this:

Code: [Select]
An error occurred in FullScreen: java.lang.IllegalArgumentException: Invalid display mode
And I got true in the other one, my graphics card is OK.

TheNewBoston Java Game Development Tutorials 3 to 7 (all of them).
Title: Re: Java - Load Images on Screen Display
Post by: cooliojazz on June 19, 2011, 02:08:03 pm
I don't remember if this is just so you can use it in jars, but you can try it to see if it helps. To load "included" files, instead of
Code: [Select]
Image bg;
image pic;
bg = new ImageIcon("background.jpeg").getImage();
pic = new ImageIcon("face.png").getImage();
I always use
Code: [Select]
import java.awt.image.*;
import javax.imageio.*;
BufferedImage bg;
BufferedImage pic;
bg = ImageIO.read(getClass().getResource("background.jpeg"))
pic = ImageIO.read(getClass().getResource("face.png"))
EDIT: forgot the includes :P
Title: Re: Java - Load Images on Screen Display
Post by: nemo on June 19, 2011, 07:10:46 pm
i always do the same thing cooliojazz does for images, although it won't affect the error you're getting scout.

i found a good explanation (http://download.oracle.com/javase/6/docs/api/java/awt/GraphicsDevice.html#setDisplayMode(java.awt.DisplayMode)) of how displaymodes work. what i would first do is test if isFullScreenSupported, and then do something similar to what's in the example code in the link. if it isn't, i don't have a solution. if it is, use setFullScreenWindow and see if your code works. if it still doesn't work, do the follow somewhere in the Screen class and copy/paste what's printed out:
Code: [Select]
for(DisplayMode dm : vc.getDisplayModes())
System.out.println(dm);

then it pick the DisplayMode printed out that's closest to the one in your code and use it:
Code: (your code) [Select]
DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
Title: Re: Java - Load Images on Screen Display
Post by: jnesselr on June 19, 2011, 10:08:37 pm
You really don't even have to mess with a displayMode.  After you have the graphicsDevice, get a Window (use a blank new frame to create the Window) and do vc.setFullScreenWindow(window) for whatever you named the java.awt.Window object.

You don't need to set it undecorated or any of that other nonsense.