Solved

Java Fullscreen Application

Posted on 2004-03-21
4
2,914 Views
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);

            screen.setFullScreenWindow(frame);
            screen.setDisplayMode(dmode);

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

            Thread.sleep(6000);
            Rectangle bounds = frame.getBounds();
            javax.swing.SwingUtilities.paintComponent(g, pan2, conpane, bounds);
            bufstrat.show();

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

Thanx.
0
Comment
Question by:Ulfsark
  • 3
4 Comments
 

Author Comment

by:Ulfsark
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();
            frame.setUndecorated(true);
            return frame;
      }

/*****************************************************************************/

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

Expert Comment

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

Accepted Solution

by:
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() {
    frame.pack();
    frame.setVisible(true);
  }

  public PanelTest() {
    pan1.setBackground(Color.RED);
    pan2.setBackground(Color.BLUE);
    pan1.add(button1);
    pan2.add(button2);
    button1.addActionListener(this);
    button2.addActionListener(this);
    switchPanel();
  }

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

  public void actionPerformed(ActionEvent e) {
    switchPanel();
  }

  public void fullScreen() {
    frame.setUndecorated(true);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    GraphicsDevice gdev = gs[0];
    gdev.setFullScreenWindow(frame);
  }

  public static void main(String args[]) {
    PanelTest test = new PanelTest();
    test.fullScreen();
    //test.showFrame();
  }

}
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10645153
:°)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 45
ejb mdb examples 1 19
java mysql insert application 14 41
netbeans can not run self signed app 8 9
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

756 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