Click here to Skip to main content
Click here to Skip to main content

How to Make Frame Full Screen in Java

By , 20 Jun 2012
Rate this:
Please Sign up or sign in to vote.

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;
	
	//Initialize the vc with the Screen Device
	public FullScreen(){
		GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
		vc=ge.getDefaultScreenDevice();
	}	
	//Set the Frame to Full Screen
	public void setFullScreen(DisplayMode dm, JFrame win){
	
	//Remove the Title Bar, Maximization , Minimization Button...
	win.setUndecorated(true); 
	
	// Can not be resized
	win.setResizable(false);
	
	//Make the win(JFrame) Full Screen
	vc.setFullScreenWindow(win);		
	
	 //check low-level display changes are supported for this graphics device.
	if(dm!=null && vc.isDisplayChangeSupported()){
			try{
					vc.setDisplayMode(dm);
			}
			catch(Exception ex){
				JOptionPane.showMessageDialog(null,ex.getMessage());
			}
	}
	}	
	//To Exit From Full Screen
	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){

          //set the background color of the frame to pink
		setBackground(Color.PINK);

          // set the Foreground color (for text) to green      
		setForeground(Color.GREEN);

          // set the font of the Text
		setFont(new Font("Arial", Font.PLAIN,27));

          //Create an Object of FullScreen class
		FullScreen fs=new FullScreen();
		try{

            //Call method setFullScreen to make the frame Full Screen
			fs.setFullScreen(dm, this);
			try{

             // Stay Full Screen for 5 sec.
				Thread.sleep(5000);
			}catch(InterruptedException ex){}
		}finally{

            // after 5 sec. get out of the Full Screen
			fs.restore();
		}		
	}
          //To draw a String on the Frame
	public void paint(Graphics g){
		if(g instanceof Graphics2D){
			Graphics2D g2=(Graphics2D)g;

            //Make the Text Smooth
			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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Hassanoor
Software Developer
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
Questionreagarding Multiple JFRAMES PinmemberMember 1030796330-Sep-13 19:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 21 Jun 2012
Article Copyright 2012 by Hassanoor
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid