Java Tutorial/2D Graphics/Full Screen

Материал из Java эксперт
Перейти к: навигация, поиск

Determine if full-screen mode is supported directly

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    if (gs.isFullScreenSupported()) {
      // Full-screen mode is supported
    } else {
      // Full-screen mode will be simulated
    }
  }
}





Enter full screen mode

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    Button btn = new Button("OK");
    btn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        gs.setFullScreenWindow(null);
      }
    });
    Frame frame = new Frame(gs.getDefaultConfiguration());
    Window win = new Window(frame);
    win.add(btn, BorderLayout.CENTER);
    try {
      gs.setFullScreenWindow(win);
      win.validate();
    } finally {
      gs.setFullScreenWindow(null);
    }
  }
}





Full screen mode

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class MainClass extends Window {
  private BufferedImage pic;
  public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice screen = ge.getDefaultScreenDevice();
    if (!screen.isFullScreenSupported()) {
      System.out.println("Full screen mode not supported");
      System.exit(1);
    }
    try {
      BufferedImage loadedpic = ImageIO.read(new File("your.jpg"));
      screen.setFullScreenWindow(new MainClass(loadedpic));
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
  }
  public MainClass(BufferedImage pic) {
    super(new Frame());
    this.pic = pic;
    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.drawImage(pic, 0, 0, getWidth(), getHeight(), this);
  }
}





Leave full-screen mode (Return to normal windowed mode)

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
  public static void main(String[] argv) throws Exception {
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     GraphicsDevice gs = ge.getDefaultScreenDevice();
     gs.setFullScreenWindow(null);
  }
}