Java Game Design HELP

+1 Sam Donahue · January 13, 2016
I am following along with the Java game design tutorial, I just finished the creating a full screen. Here is my Code:

Sam:

import java.awt.*;
import javax.swing.*;

public class Sam extends JFrame{

public static void main(String[] args) {

DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
Sam s = new Sam();
s.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));

Screen s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch(Exception ex){}
}finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("THIS IS GUNNA BE AWESOME", 200, 200);
}

}


Screen:

import java.awt.*;
import javax.swing.*;
public class Screen {

private GraphicsDevice vc;

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);
}
}

Post a Reply

Replies

Oldest  Newest  Rating
+2 Nicholas Eason · January 13, 2016
So on the basis that you've given us no errors (just your code), I'm going to assume it happens in one of the following places:
try{
vc.setDisplayMode(dm);
}catch(Exception ex){}



try{
Thread.sleep(5000);
}catch(Exception ex){}


Try changing all of your catch clauses to this:

catch(Exception ex){e.printStackTrace();}

Doing the above will output the error to the console in the event you ever reach an error. Once you have that, assuming that it gave you a stack trace, then if you can't figure it out feel free to post it here so I can help you further :)
+1 Nicholas Eason · January 14, 2016
That IS a Stack Trace error. The error overall is called a Stack Trace. So as you can see, you've passed it a Display Mode that isn't valid. I'm pretty sure that this is hardware specific, so I can't help you, but your error is occurring when you try to set the display mode here:

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

However, all is not lost. You can get a list of display modes that ARE compatible with your computer using:

DisplayMode[] dms = vc.getDisplayModes();
//Figure out which one you want. How?
for(DisplayMode dis: dm){
System.out.println(dis.getBitDepth() + "       " + dis.getRefreshRate() + "          " + dis.getWidth() + "  " + dis.getHeight());
}

This will print out relevant information for each display mode, and you can create an appropriate DisplayMode based on the information received. For example, one of my DisplayModes that are compatible with my Graphics Card is:
//Bit Depth, Refresh Rate, Width, Height
32       60          1600  900

Like I said, I'm pretty sure this is a hardware specific issue, sorry I couldn't help you out more than just telling you "find out what works and use it", but that's sadly all I can do :(
0 Todd Coker · January 21, 2016
I ran into this problem yesterday.  For me it ran at my current graphic settings regardless because my device could not accept 16 bit as an argument.  When I changed it to 32 it worked just fine and ran at 800 x 600 resolution. and boy did that bring back memories, although I remember when it was 640.

As Nicholas said I think you need to change your code to an appropriate graphic setting for your computer.  I'm a one day Java beginner but I changed that part to the below to ensure it was running on the current settings but that won't test if your program can actually change the display settings.

 //Get the current video settings
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int dheight = gd.getDisplayMode().getHeight();
        int dwidth = gd.getDisplayMode().getWidth();
        int ddepth = gd.getDisplayMode().getBitDepth();
        int drefresh = gd.getDisplayMode().getRefreshRate();
       
        //Set the Display mode - use current settings
        DisplayMode dm = new DisplayMode (dwidth, dheight, ddepth, drefresh);
0 Sam Donahue · January 22, 2016
I fixed the code with help on Stack Overflow: http://stackoverflow.com/questions/34913066/java-setbackground-not-working
here is my new code for reference:

import java.awt.*;
import javax.swing.*;

public class Sam extends JFrame{

public static void main(String[] args) {

DisplayMode dm = new DisplayMode(1920, 1080, 32, 60);
Sam s = new Sam();
s.run(dm);
}
public void run(DisplayMode dm){
paint p = new paint();
add(p);

Screen s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch(Exception ex){ex.printStackTrace();}
}finally{
s.restoreScreen();
}
}
//public void paint(Graphics g){
//g.drawString("THIS IS GUNNA BE AWESOME", 200, 200);
//}
public class paint extends JPanel{

public paint(){
super();
}

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
setBackground(Color.PINK);
getContentPane().setForeground(Color.BLACK);
setFont(new Font("Arial", Font.PLAIN, 48));
g.drawString("THIS IS GUNNA BE AWESOME", 200, 200);
}
}

}


and my screen class:

import java.awt.*;
import javax.swing.*;
public class Screen {

private GraphicsDevice vc;

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){ex.printStackTrace();}
}
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}


instead of making a method paint, I made a new class that extends JPanel that overrides the paintComponent method. I then created a paint Object in my Sam Class and added it to the frame, I put the setBackground(), setForground, and set font in the paintComponet() method to
0 Sam Donahue · January 14, 2016
I did get an error, but I dont think this is a stack trace error:
java.lang.IllegalArgumentException: Invalid display mode
at sun.awt.Win32GraphicsDevice.setDisplayMode(Unknown Source)
at Screen.setFullScreen(Screen.java:18)
at Sam.run(Sam.java:19)
at Sam.main(Sam.java:10)
0 David Frenzel · January 14, 2016
Just an idea - this could also be a problem with your emulator. So maybe Genymotion or Bluestacks can solve this. Maybe not, I'm not an expert!
0 Sam Donahue · January 15, 2016
Its not an emulator.

But I changed some things and I think the problem is that it is not setting the screen to full screen, its not changing the background color, and it is not changing the foreground color.
0 Sam Donahue · January 15, 2016
Currently it takes what ever screen I am using whether it be my desktop on eclipse, uses those for the background and the dimensions are what is set in the statement: 
DisplayMode dm = new DisplayMode(800, 600, 32, 60);
  • 1

Java / Android Development

107,224 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator