Home Java Example Java Swing Graphics2D Show Graphics on full Screen
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Show Graphics on full Screen

Advertisement
To display the graphics, we have used the class DisplayMode which provides the bit depth, height, width, and refresh rate of a GraphicsDevice.

Show Graphics on full Screen

     

This section illustrates you how to show graphics on full screen.

To display the graphics, we have used the class DisplayMode which provides the bit depth, height, width, and refresh rate of a GraphicsDevice. The graphics devices are described by the class GraphicsDevice and contains the objects of  class GraphicsConfiguration and specify these objects into different configurations.

To organize the graphics on a window or canvas, we used the class BufferStrategy.  To returns the screen on which the graphics will displayed, the method getDefaultScreenDevice() is used and to return the current mode of graphics device, method getDisplayMode() is used.

To displays the full screen mode, the method setFullScreenWindow(f) is used.  If the graphics device supports the display changes, the method  isDisplayChangeSupported() returns true.

Then creates a new strategy with the number of buffers for multi-buffering specified  by the method createBufferStrategy(5). It is useful to render the performance. The getBufferStrategy() returns the BufferStrategy used by its component. If a BufferStrategy has not been created or disposed, it will return null.

 The method getDrawGraphics() of class BufferStrategy displays the graphics. 

Here is the code of ShowFullScreen.java

import java.awt.*;
import java.awt.image.BufferStrategy;

public class ShowFullScreen {
  private static int counter = 0;
  private static final int size = 40;

 private static DisplayMode displayMode[] = new DisplayMode[] {
  new DisplayMode(640480320), new DisplayMode(640480160),
  new DisplayMode(64048080) };

  private static DisplayMode getBestDisplayMode(GraphicsDevice device) {
  for (int z = 0, n = displayMode.length; z < n; z++) {
  DisplayMode[] displayMode = device.getDisplayModes();
  for (int j = 0, k = displayMode.length; j < k; j++) {
  if (displayMode[j].getWidth() == displayMode[z].getWidth()
  && displayMode[j].getHeight() ==displayMode[z].getHeight()
  && displayMode[j].getBitDepth(==displayMode[z].getBitDepth()){
  return displayMode[z];
  }
  }
  }
  return null;
  }
 public static void main(String args[]) {
  GraphicsEnvironment ge = GraphicsEnvironment.
  getLocalGraphicsEnvironment();

  GraphicsDevice gd = ge.getDefaultScreenDevice();
  DisplayMode dm = gd.getDisplayMode();
  try {
  Frame f = new Frame("Show full screen");
 gd.setFullScreenWindow(f);
  if (gd.isDisplayChangeSupported()) {
  gd.setDisplayMode(getBestDisplayMode(gd));
  }
  f.createBufferStrategy(5); 
  Rectangle rect = f.getBounds();
  BufferStrategy bs = f.getBufferStrategy();
  while (!done()) {
  Graphics g = null;
  try {
  g = bs.getDrawGraphics();
  if ((counter <= 5)) { 
  g.setColor(Color.white);
  g.fillRect(00650,550);
  }
  g.setColor(Color.red);
  g.drawLine(counter, counter * 5, rect.width,
  rect.height);
  bs.show();
  finally {
  if (g != null) {
  g.dispose();
  }
  }
  try {
  Thread.sleep(150);
  catch (Exception e) {
  }
  }
  finally {}
  System.exit(0);
  }
 private static boolean done() {
  return (counter++ == size);
  }
}

Output will be displayed as:

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 11, 2008

Related Tutorials

Ask Questions?    Discuss: Show Graphics on full Screen  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com