Author Topic: Java - Load Images on Screen Display  (Read 4496 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Java - Load Images on Screen Display
« 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.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Java - Load Images on Screen Display
« Reply #1 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 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.


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Java - Load Images on Screen Display
« Reply #2 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).

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Java - Load Images on Screen Display
« Reply #3 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
« Last Edit: June 19, 2011, 02:09:44 pm by cooliojazz »
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Java - Load Images on Screen Display
« Reply #4 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 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);


Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Java - Load Images on Screen Display
« Reply #5 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.