0

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.

Brixzz
  • 21
  • 7

1 Answers1

0

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);
    }
Alex Sveshnikov
  • 4,214
  • 1
  • 10
  • 26
  • Thanks! That was actually what I wanted to try next, but my Draw.java class doesn't compile anymore even though I didn't change anything. I even re-downloaded it from the Princeton university website. – Brixzz Mar 09 '20 at 14:00
  • Creating a setFullScreenMode() in Draw.java didn't help either. I only got one screen now, but nothing gets displayed on it. – Brixzz Mar 09 '20 at 14:34
  • Updated answer results in a blank screen too (but without the working Window opening in the background) – Brixzz Mar 09 '20 at 14:53
  • Okay, adding ´draw.getFrame().setExtendedState(JFrame.MAXIMIZED_BOTH); ´ and keeping a fix canvas size (1350, 800) worked for combining both windows and drawing the objects - but the click-listener doesn't work properly anymore. Also the 1350 x 800 canvas is displayed inside the full-screen window (you can see the different shades of white) Thanks for your help! – Brixzz Mar 09 '20 at 15:11
  • I downloaded Draw.java and managed to get it to full screen with and without window decorations. Do you need window decorations? Can you provide an example of not working click-listeners? – Alex Sveshnikov Mar 09 '20 at 17:47
  • I only managed to get it to full screen with window decorations, otherwise I got an exception when trying to run the program (Something about the frame being visible if I remember correctly). As for the click-listeners: I basically made my own buttons by drawing a rectangle, putting a text in the middle of it and using the following code to register clicks: – Brixzz Mar 10 '20 at 08:24
  • while(true) { if(draw.isMousePressed() && draw.mouseX() > 0.5 - 0.15 && draw.mouseX() < 0.5 + 0.15 && draw.mouseY() > 0.7 - 0.05 && draw.mouseY() < 0.7 + 0.05){ StdAudio.play(shortClick); break; } } Sorry, I don't know how to post code in the comments – Brixzz Mar 10 '20 at 08:27
  • Okay, now I have an undecorated window. I had to call the setUndecorated(true) method directly after initializing the frame. But I still have the two shades of white and my button clicks don't get registered properly – Brixzz Mar 10 '20 at 08:55
  • Updated the answer. – Alex Sveshnikov Mar 11 '20 at 05:49
  • Thank you so much! After also removing the menu bar at the top of the screen everything works perfectly now! :) – Brixzz Mar 11 '20 at 13:04