Introduction
This tip guides you to make a Frame full screen.
Background
To make a frame full screen, first we need to have access to the local screen. The GraphcisEnvironment
class contains a method which helps to access the default screen device. Once we get access to the screen, we need a method that helps to set the frame to full screen. For this, we have GraphicsDevice
class that contains a method setFullScreen
(Window frame). Another class DisplayMode
is needed to set the screen size and refresh rate.
Using the Code
First, create a class with name FullScreen.java. Now create GraphicsDevice
reference say vc
, in the constructor initialize the vc
. Create two methods, one setFullScreen
with two parameters DisplayMode
and Frame
(which you want to be full screen). The other one is CloseFullScreen
to get out of the full screen.
import java.awt.*;
import javax.swing.*;
public class FullScreen(){
GraphicsDevice vc;
public FullScreen(){
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
vc=ge.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame win){
win.setUndecorated(true);
win.setResizable(false);
vc.setFullScreenWindow(win);
if(dm!=null && vc.isDisplayChangeSupported()){
try{
vc.setDisplayMode(dm);
}
catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
}
public void CloseFullScreen(){
Window w=vc.getFullScreenWindow();
if(w!=null){
w.dispolse();
}
vc.setFullScreenWindow(null);
}
}
Compile the FullScreen.java.
c:\>javac FullScreen.java
Now create a class EntryPoint.java, which contains main()
, within main()
create an object of DisplayMode
. See DisplayMode
's constructor has four parameters, the first parameter is for width of the screen, the second is height, the third is color depth, and the last one is for refresh rate. Since I don't know the refresh rate, that's why I set it to REFRESH_RATE_UNKNOWN
. Go through the comment and you will understand.
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
public class EntryPoint extends JFrame{
public static void main(String[] args) {
DisplayMode dm=new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
EntryPoint b=new EntryPoint();
b.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.GREEN);
setFont(new Font("Arial", Font.PLAIN,27));
FullScreen fs=new FullScreen();
try{
fs.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch(InterruptedException ex){}
}finally{
fs.restore();
}
}
public void paint(Graphics g){
if(g instanceof Graphics2D){
Graphics2D g2=(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
g.drawString("You Have Done", 200, 200);
}
}
Compile the EntryPoint.java.
c:\>javac EntryPoint.java
and run.
c:\>java EntryPoint
Download the attached file, unzip, compile and run.
History
- 20th June, 2012: Initial post