Search code examples
javafullscreendrawstddraw

Java: Make Draw-program run in fullscreen


I am using the Draw.java class from the Princeton library for a small graphics program. Currently it runs in a window. I want to make it scaleable and full-screen. How can I achieve that? I looked into Full-Screen Exclusive Mode but am not quite sure on how to use it. I tried adding it into my program, but I can't seem to link the Draw-Window to the Full-Screen Frame, which leads to one window with the program and a blank full screen opening. I don't want to change my entire code using the Draw class unless it is necessary. Any ideas on how to make the Draw-Window Full-Screen?

Here is a code sample although I don't think it'll help much:

Draw draw = new Draw("TapReact");
int canvasWidth = 1350;
int canvasHeight = 800;
draw.setCanvasSize(canvasWidth, canvasHeight);

And and example for a full-screen implementation with Full-Screen Exclusive Mode:

GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
Window myWindow = new Window(new Frame());
graphicsDevice.setFullScreenWindow(myWindow);   

How can I link myWindow and draw, so that both display the same thing?

Help would be much appreciated, thank you.

Edit: I Already tried implementing a getFrame() method in Draw.java and using that frame with new Window(draw.getFrame()). I also tried implementing a setFullScreen() method in Draw.java which also didn't work.


Solution

  • UPDATED 2020-03-11

    In Draw.java:

        private boolean isFullscreen = false;
    
      /**
      *  Sets the undecorated fullscreen mode.
      */
      public void setFullscreen() {
          isFullscreen = true;
          frame.dispose();
          frame = new JFrame();
          frame.setUndecorated(true);
          frame.setResizable(false);
          frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
          frame.setVisible(true);
          width = frame.getWidth();
          height = frame.getHeight();
          init();
      }
    

    Also, you need to modify the second line of init() method in Draw.java:

      private void init() {
            if (frame != null) frame.setVisible(false);
            if (!isFullscreen) frame = new JFrame();  // This is the modified line
            offscreenImage = new BufferedImage(2*width, 2*height, BufferedImage.TYPE_INT_ARGB);
            onscreenImage  = new BufferedImage(2*width, 2*height, BufferedImage.TYPE_INT_ARGB);
            offscreen = offscreenImage.createGraphics();
            .........
    

    Then in your test class:

        Draw draw = new Draw("TapReact");
        draw.setFullscreen();
        while (true) {
          if (draw.isMousePressed() &&
              draw.mouseX() > 0.4 &&
              draw.mouseX() < 0.6 &&
              draw.mouseY() > 0.4 &&
              draw.mouseY() < 0.6) {
            draw.setPenColor(Draw.RED);
          } else {
            draw.setPenColor(Draw.BLACK);
          }
          draw.filledSquare(0.5, 0.5, 0.1);
        }