Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java2D toggle fullscree / windowed.  (Read 803 times)
0 Members and 1 Guest are viewing this topic.
Offline brollysan

Junior Member


Medals: 1



« Posted 2013-03-19 16:39:26 »

After reading some guides and books I managed to put together my own screen-manager that implements the Buffer-strategy page-flipping double-buffer. The problem is I would like my game to have the option to toggle between fullscreen and windowed mode but my current screenmanager cannot handle this at all and I am completely lost as to what to do next.

In the example below I draw a string in fullscreen mode, then exit fullscreen mode and the string is gone from my frame. I know this is an active rendering model but do I have to re-render everything when I exit fullscreen (or go fullscreen from windowed)? I do know that I am not actually drawing on the frame, it is just used as a container of sorts and it seems to me that the content of the buffer-strategy is lost when I exit fullscreen.

I know a lot of you are knowledgeable (I have been lurking here since I started java a few months ago) and would appreciate any help as to how to manage this. The end-game is going to be pretty complex with inventory menus, game menus, combatmenus etc and having to actively babysit the bufferstrategy so the content is not lost (by accident or the fullscreen toggle) seems like an immense pain to implement.

Thank you.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
package gfx;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class ScreenManager {

   private JFrame frame;
   private GraphicsDevice gd;
   private DisplayMode defaultMode;
   private DisplayMode[] supportedModes;
   
   
   // Use with frame from elsewhere
  public ScreenManager(JFrame frame) {
      this();
      this.frame = frame;

   }


   // Used with a frame that is tied to instance
  public ScreenManager() {
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      this.gd = ge.getDefaultScreenDevice();
      this.defaultMode = new DisplayMode(800, 600, 16, 60);
      this.setSupportedModes();
      this.frame = new JFrame();
   }

   // Get the supported displayrates from current graphicsdevice
  private void setSupportedModes() {
      this.supportedModes = gd.getDisplayModes();
   }

   // Check if the supplied displaymode is supported by current device
  public boolean isSupportedDisplayMode(DisplayMode odm) {
      for (DisplayMode dm : this.supportedModes) {
         if (dm.getHeight() == odm.getHeight()
               && dm.getWidth() == odm.getWidth()
               && dm.getBitDepth() == odm.getBitDepth()
               || odm.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI
               && dm.getRefreshRate() == odm.getBitDepth()
               || odm.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN)
            return true;

      }
      return false;
   }
   
   public void setFullScreen(DisplayMode displayMode) {
      this.setFullScreen(displayMode, frame);
   }

   // Set fullscreen if supported displaymode, else default displaymode
  public void setFullScreen(DisplayMode displayMode, JFrame frame) {
      if (gd.isFullScreenSupported()) {
         // Fullscreen on visible frame not allowed
        frame.setVisible(false);
         // Remove decoration and unresiable
        frame.setUndecorated(true);
         frame.setResizable(false);
         frame.setIgnoreRepaint(true);
         // Set frame as fullscreenwindow
        gd.setFullScreenWindow(frame);
         // Set default if requested not supported or null
        if (displayMode == null || !isSupportedDisplayMode(displayMode))
            gd.setDisplayMode(defaultMode);

         else
            gd.setDisplayMode(displayMode);
         // Create bufferstrategy
        frame.createBufferStrategy(2);
      }
   }
   
   // Make windowed
  public void setWindowed() {
      // Windowed from fullscreen if fullscreen, otherwise we are probably
     // windowed already
     if (gd.getFullScreenWindow() != null) {
         // gd.getFullScreenWindow().dispose();
        gd.setFullScreenWindow(null);
         // frame.setUndecorated(false);
        frame.setVisible(true);
      }
   }

   // Get the drawing graphics of this ScreenManagers bufferstrategy
  public Graphics2D getGraphics() {
      Window frame = gd.getFullScreenWindow();
      if (frame != null) {
         BufferStrategy bufferStrategy = frame.getBufferStrategy();
         return (Graphics2D) bufferStrategy.getDrawGraphics();
      }
     
      return null;
   }

   public void update() {
      Window frame = gd.getFullScreenWindow();
      if (frame != null) {
         BufferStrategy bufferStrategy = frame.getBufferStrategy();
         if (!bufferStrategy.contentsLost())
            bufferStrategy.show();
      }

      Toolkit.getDefaultToolkit().sync();
   }

   // Display in readable format, eg 800x600x32@60
  public String displayModeToString(DisplayMode dm) {
      return dm.getWidth() + "x" + dm.getHeight() + "x" + dm.getBitDepth()
            + "@" + dm.getRefreshRate();
   }


   public static void main(String[] args) throws InterruptedException {
      JFrame frame = new JFrame();
      frame.setSize(800, 600);
      ScreenManager sm = new ScreenManager(frame);
      sm.setFullScreen(new DisplayMode(1680, 1050, 32, 60));
      Graphics2D g2d = sm.getGraphics();
      g2d.setColor(Color.red);
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      Font font = new Font("Serif", Font.PLAIN, 96);
      g2d.setFont(font);
      g2d.drawString("jade", 40, 120);
      g2d.dispose();
      sm.update();
      Thread.sleep(3000);
      sm.setWindowed();


   }


}
Offline Regenuluz
« Reply #1 - Posted 2013-03-19 17:55:16 »

You need to redraw everything when you change display mode/size. Smiley
Offline brollysan

Junior Member


Medals: 1



« Reply #2 - Posted 2013-03-19 18:41:35 »

You need to redraw everything when you change display mode/size. Smiley

I was hoping to avoid this but if I must I must Cheesy

I was also wondering if bufferedstrategy can be applied to nonfullscreen modes? If I just display stuff on a JFRame, can that JFrame be accelerated with a bufferstrategy even if it is not fullscreen? No direct answer to this on the internet.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Regenuluz
« Reply #3 - Posted 2013-03-19 20:14:49 »

Yes, yes you can. Smiley

1  
2  
3  
4  
5  
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
   this.createBufferStrategy(2);
   bs = this.getBufferStrategy();
}


Taken from one of my class files that extend JPanel.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (35 views)
2014-04-15 18:08:23

BurntPizza (31 views)
2014-04-15 03:46:01

UprightPath (46 views)
2014-04-14 17:39:50

UprightPath (29 views)
2014-04-14 17:35:47

Porlus (46 views)
2014-04-14 15:48:38

tom_mai78101 (67 views)
2014-04-10 04:04:31

BurntPizza (127 views)
2014-04-08 23:06:04

tom_mai78101 (227 views)
2014-04-05 13:34:39

trollwarrior1 (192 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!