Java Fullscreen Application

Posted on 2004-03-21
Last Modified: 2012-06-27
Ok, I'm building a Java App in fullscreen mode. I've got the fullscreen mode working on a JFrame object with a JPanel added to it for content. What I need to know is how to swap the JPanel for a different one without dropping out of fullscreen mode. I suspect this has something to do with the BufferStrategy class, but I can't get it to work.

Here's what I've got so far, with a MyPanel jsut being a simple JPanel with an image and a settable background color. It should show the red one, wait around for a sec, then show the blue one. Currently it jsut shows the red one then a blank screen.

public static void main(String args[]) {
      try {
                GraphicsDevice screen = screenSetup();
            DisplayMode dmode = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
            JFrame frame = makeFrame();

            MyPanel pan1 = testPanel(Color.RED);
            MyPanel pan2 = testPanel(Color.BLUE);

              Container conpane = frame.getContentPane();
            conpane.add(pan1, BorderLayout.CENTER);


            BufferStrategy bufstrat = frame.getBufferStrategy();
            Graphics g = bufstrat.getDrawGraphics();

            Rectangle bounds = frame.getBounds();
            javax.swing.SwingUtilities.paintComponent(g, pan2, conpane, bounds);

      } catch (Exception ex) {

Question by:Ulfsark
  • 3

Author Comment

ID: 10643346
Oh, sorry, here's the rest of the methods if they help.

      private static GraphicsDevice screenSetup() {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice[] gs = ge.getScreenDevices();
            GraphicsDevice gdev = gs[0];
            return gdev;


      public static JFrame makeFrame() {
            JFrame frame = new JFrame();
            return frame;


      public static MyPanel testPanel(Color c) {
            MyPanel panel = new MyPanel();
            JLabel lab = new JLabel(new ImageIcon("images/loading-screen.gif"));
            panel.add(lab, BorderLayout.CENTER);
            return panel;
LVL 30

Expert Comment

ID: 10643440
you can use something like this:
          conpane.add(pan2, BorderLayout.CENTER);
LVL 30

Accepted Solution

GrandSchtroumpf earned 125 total points
ID: 10643622
here is a class that demonstrates how to switch between 2 panels in full screen.
clicking the button switches the panels.

in the main method, you can use the showFrame() method instead of the fullScreen() to show your frame in normal mode (normal = not-full-screen).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferStrategy;

class PanelTest implements ActionListener {

  JFrame frame = new JFrame();
  JPanel pan1 = new JPanel();
  JPanel pan2 = new JPanel();
  JButton button1 = new JButton("Button One");
  JButton button2 = new JButton("Button Two");
  boolean containsPan1 = false;
  public void showFrame() {

  public PanelTest() {

  public void switchPanel() {
    JPanel toadd = (containsPan1)? pan1 : pan2;
    containsPan1 = !containsPan1;
    frame.getContentPane().add(toadd, BorderLayout.CENTER);

  public void actionPerformed(ActionEvent e) {

  public void fullScreen() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    GraphicsDevice gdev = gs[0];

  public static void main(String args[]) {
    PanelTest test = new PanelTest();

LVL 30

Expert Comment

ID: 10645153

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GUI builder for Eclipse? 8 27
Arrays.asList  VS  ArrayList 4 45
more than one jdk and one jre 1 33
factorial example 4 35
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now