How to activate fullscreen in a different class?
I am trying to get a simple fullscreen window for a game I am making. I want to have the code that makes the window go fullscreen in a different class to my main class (called FullScreenWindow), but have the main class activate the fullscreen mode. The code that makes the window fullscreen in in the 'Screen' class.
Here is the code for the Screen class:
Code :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class Screen extends JFrame{
public Screen( String title )
{
super(title);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FullScreenWindow frame = new FullScreenWindow();
frame.setVisible(true);
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
setUndecorated(true);
if (gd.isFullScreenSupported()) {
setUndecorated(true);
gd.setFullScreenWindow(this);
} else {
System.err.println("Full screen not supported");
setSize(100, 100); // just something to let you see the window
setVisible(true);
}
getContentPane().add(new JLabel("Test Fullscreen"), BorderLayout.NORTH);
setBackground(Color.BLUE);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
drawString("This is cool", 200, 200);
if(isVisible() == true){
try{
try {
Thread.sleep(5000);
} catch (Exception ex) {
ex.printStackTrace();
}
}finally{
Screen.this.setVisible(false);
System.exit(0);
}
}
}
private void drawString(String string, int i, int j) {
// TODO Auto-generated method stub
}
}
Here is the code in my FullScreenWindow class (my main class)
Code :
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class FullScreenWindow extends JFrame {
public static void main(String[] args){
}
}
I hope you can help.
Re: How to activate fullscreen in a different class?
If you need clarification, just ask :)
Re: How to activate fullscreen in a different class?
Why are either of those classes extending JFrame? They don't override any of the default behavior. Favor composition over inheritance, and I think it will make more sense what you have to do.
Re: How to activate fullscreen in a different class?
lol...
try this man:
Code :
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
once you have the size of the screen simply set the height and width of the game frame to width and height that dim is equal to.
Re: How to activate fullscreen in a different class?
Quote:
Originally Posted by
macko
lol...
try this man:
Code :
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
once you have the size of the screen simply set the height and width of the game frame to width and height that dim is equal to.
Not only is that not the correct way to use a fullscreen JFrame, that doesn't answer the OP's actual question. Please read this before posting again: http://www.javaprogrammingforums.com...n-feeding.html
Re: How to activate fullscreen in a different class?
Just a note, all of my CODE that makes it fullscreen works, just, it won't ACTIVATE. If this is something wrong with my code, I feel like an idiot.
I just need to know how I would activate fullscreen from my main class, on startup of the program.
I was wondering if a IF statement around the code in Screen class would work.
Re: How to activate fullscreen in a different class?
I had done all of this code in the FullScreenWindow class, but I didn't want the code in my main class, so I moved it over to the Screen class.
Re: How to activate fullscreen in a different class?
Again, I'm not sure why you're extending JFrame at all. That's making things more confusing than they have to be.
Because all you need to do is create an instance of your Screen class (which contains, not extends, a JFrame). Then from the FullScreenWindow class, you can either get that instance's JFrame and make it fullscreen, or you can call a method that does it for you in the Screen class.