Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Stretch Fullscreen?  (Read 4255 times)
0 Members and 1 Guest are viewing this topic.
Offline Raildex

Senior Newbie





« Posted 2013-06-23 18:56:08 »

Hey there!
My current JPanel/JFrame has a size of 960x540.
Is there a possiblity to stretch the whole Frame to fullscreen without having to recalculate imagesizes and positions?
Offline BurntPizza
« Reply #1 - Posted 2013-06-23 19:11:07 »

Is there a possiblity to stretch the whole Frame to fullscreen without having to recalculate imagesizes and positions?

That depends on the layout manager you are using. I don't think any of them will simply scale all the components, including images, but you could try this:
1  
frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());

If you want to make it actually fullscreen, where you can't see the title bar or OS taskbar, then take a look here.

I haven't messed with Swing in a while, but that's what I remember.
Offline Raildex

Senior Newbie





« Reply #2 - Posted 2013-06-23 19:22:30 »

I don't want to make a normal fullscreen.
I want a STRETCHED fullscreen... hard to explain.
like this picture:

Widowed: like it is now.
Fullscreen(red) how it is, when I use fullscreen,
Coordinate 1920x1080 IS 1920x1080.
Stretched fullscreen (green) how I want it to be.
Coordinate 1920x1080 is now 960x540!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #3 - Posted 2013-06-23 19:50:46 »

Yea, I get it, everything should simply scale 2x.
Well, if you are drawing to a BufferedImage or similar then drawing that to the JPanel (you should be), then simply scale the BufferedImage to the JPanel's size when you draw it:
1  
2  
3  
4  
5  
6  
/*I am assuming that this paint() is for the JPanel, but it could be for the JFrame, if you skip the JPanel; and that buffer is a 960 by 540 image*/
public void paint(Graphics g) {
    /*do stuff here if required*/

    g.drawImage(buffer, 0, 0, 960, 540, 0, 0, 1920, 1080, null);
}

I highly recommend using frame.getWidth() and .getHeight() instead of hardcoding 1920 and 1080.

Source

Now, the buffer is still 960x540, but gets scaled to 1920x1080 on the fly. Is this what you wanted?
Offline Raildex

Senior Newbie





« Reply #4 - Posted 2013-06-23 19:56:17 »

yeah, scaling is the word I was searching for.

I'm not drawing onto a bufferedImage, how do I do that?
Offline BurntPizza
« Reply #5 - Posted 2013-06-23 20:12:07 »

How are you doing it currently?
Offline Raildex

Senior Newbie





« Reply #6 - Posted 2013-06-23 20:13:42 »

Well, I simple create an Graphics2D-Object with
Graphics2D g;
g = (Graphics2D) getGraphics();

Offline BurntPizza
« Reply #7 - Posted 2013-06-23 20:20:35 »

It might depend on what (where) you are calling getGraphics() on, but I think this will work:
Replace that line with:
1  
2  
3  
4  
BufferedImage buffer = new BufferedImage(960,540,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffer.createGraphics();
/*do rendering here*/
g.dispose();

Always remember to dispose the Graphics object when you are done with it.

Then in your JPanel or JFrame's paint() method:
1  
2  
3  
public void paint(Graphics g) {
    g.drawImage(buffer, 0, 0, buffer.getWidth(), buffer.getHeight(), 0, 0, getWidth(), getHeight(), null);
}
Offline Raildex

Senior Newbie





« Reply #8 - Posted 2013-06-23 20:40:31 »

Well, I'm not using the paintComponent() Method.
I always send the Graphics object as parameter to the desired Screen and then to the object that has to be drawn.
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  
public void run()
  {
    initialize();

    while (isRunning)
    {
      beginTime = System.currentTimeMillis();
      framesSkipped = 0;   // resetting the frames skipped
     update();
      render();
      // calculate how long did the cycle take
     timeDiff = System.currentTimeMillis() - beginTime;
      // calculate sleep time
     sleepTime = (int) (FRAME_PERIOD - timeDiff);
      if (sleepTime > 0)
      {
        // if sleepTime > 0 we're OK
       try
        {
          // send the thread to sleep for a short period
         // very useful for battery saving
         Thread.sleep(sleepTime);
        }
        catch (InterruptedException e)
        {
        }
      }
      while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS)
      {
        // we need to catch up
       // update without rendering
       update();
        // add frame period to check if in next frame
       sleepTime += FRAME_PERIOD;
        framesSkipped++;
      }
    }
    setVisible(false);
  }
 

public void render()
  {
    gameScreenManager.render(g);
  }

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  
package Core.GameState;

import Core.GameCore;
import Game.Screens.TitleScreen;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class GameScreenManager {
   
   public ArrayList<GameScreen> gameScreens;
   public static int currentState;
   public GameScreen screen;
   public static final int SCREEN_TITLE = 0;
   public static final int SCREEN_LEVEL = 1;
   
   public GameScreenManager() {
     
      gameScreens = new ArrayList<GameScreen>();
      currentState = SCREEN_TITLE;
    screen = new TitleScreen(this);
      gameScreens.add(screen);
   }
   
   public void setState(int state) {
      currentState = state;
      gameScreens.get(currentState).init();
   }
   
   public void update() {
      gameScreens.get(currentState).update();
   }
   
   public void render(Graphics2D g) {
      gameScreens.get(currentState).render(g);
   }
   
   public void keyPressed(int k) {
      gameScreens.get(currentState).keyPressed(k);
   }
   
   public void keyReleased(int k) {
      gameScreens.get(currentState).keyReleased(k);
   }
   
}



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  
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Game.Screens;

import Core.GameCore;
import Core.GameState.GameScreen;
import Core.GameState.GameScreenManager;
import Core.OptionsManager;
import Graphics.ScreenObject;
import Graphics.UserInterface.VolumeBar;
import Sound.BackgroundMusic;
import Sound.SoundLibrary;
import World.Layer1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

/**
 *
 * @author Nils
 */

public class TitleScreen extends GameScreen
{
  //music

  BackgroundMusic titleMusic;
  String musicFile = "/Music/Title.mp3";
  Layer1 bg;
  BufferedImage bgImage;
  ScreenObject title;
  ScreenObject lara;
  Color selected;
  Color deselected;
  int currentScreen;
  String[] options = new String[]
  {
    "Start", "Load", "Options", "Quit"
  };
  private int currentChoice;
  private VolumeBar vb;

  public TitleScreen(GameScreenManager manager)
  {
    this.gameStateManager = manager;
    init();
  }

  @Override
  public void init()
  {
    GameCore.options.volume = 100;
    GameCore.options.targetting = true;
    GameCore.options.graphics = 0;
    GameCore.g.setRenderingHints(OptionsManager.graphicslow);
    soundlib = new SoundLibrary("Title");
    titleMusic = new BackgroundMusic(musicFile);
    titleMusic.start();
    selected = new Color(222, 217, 216);
    deselected = new Color(80, 69, 67);
    currentChoice = 0;
    currentScreen = 0;
    bg = new Layer1("/Graphics/Backgrounds/Cloud.jpg", 1);
    title = new ScreenObject("/Graphics/Pictures/TitleLogo.png");
    lara = new ScreenObject("/Graphics/Pictures/TitleLara.png");
    lara.setPosition(40, 100);
    title.setPosition((Core.GameCore.WIDTH / 2) - title.image.getWidth() / 2,
            100);
  }

  @Override
  public void update()
  {
    bg.setVector(-0.4, 0);
    bg.update();
    if (currentScreen == 0)
    {
      //Titlescreen
   }
    if (currentScreen == 1)
    {
    }
    if (currentScreen == 2)
    {
      //Options
     vb.update();
    }
  }

  @Override
  public void render(Graphics2D g)
  {
    bg.render(g);
  }

  @Override
  public void keyPressed(int k)
  {
[...]
  }

  @Override
  public void keyReleased(int k)
  {
  }
}


this is a chain, how Graphics g is used.

When I put buffer.drawImage();
right after
1  
gameScreens.get(currentState).render(g);

nothing is shown.

Offline BurntPizza
« Reply #9 - Posted 2013-06-23 20:53:49 »

Alright, so change this
1  
2  
3  
4  
public void render()
  {
    gameScreenManager.render(g);
  }

to this:
1  
2  
3  
4  
5  
public void render()
  {
    gameScreenManager.render(buffer.createGraphics());
    g.drawImage(buffer, 0, 0, buffer.getWidth(), buffer.getHeight(), 0, 0, getWidth(), getHeight(), null);
  }


and have the
1  
BufferedImage buffer;

in that class, and this in your initialize() method:
1  
buffer = new BufferedImage(960,540,BufferedImage.TYPE_INT_RGB);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Raildex

Senior Newbie





« Reply #10 - Posted 2013-06-23 21:05:51 »

Ok, it now shows everything as normal.

I have changed the line to this:
1  
g.drawImage(buffer, 0, 0, OptionsManager.currentDimension.width, OptionsManager.currentDimension.height, 0, 0, getWidth(), getHeight(), null);


this is my OptionsManager-Class

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  
package Core;

import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.Toolkit;

public class OptionsManager
{

  public static int volume = 100;
  public static int graphics = 0;
  public static boolean targetting = true;
  public static boolean fullscreen = false;
  public static Dimension currentDimension = new Dimension(960, 540);
  public static Dimension fullscreenDimension = Toolkit.getDefaultToolkit().getScreenSize();
  public static Dimension windowDimension = new Dimension(960, 540);

  public OptionsManager()
  {
  }

  public static void toggleFullscreen()
  {
    if (fullscreen == false)
    {
      GameCore.frame.setSize(fullscreenDimension);
      OptionsManager.currentDimension = fullscreenDimension;
      fullscreen = true;
      System.out.println(fullscreen);
    }
    else if (fullscreen == true)
    {
      GameCore.frame.setSize(windowDimension);
      OptionsManager.currentDimension = windowDimension;
      fullscreen = false;
      System.out.println(fullscreen);
    }
  }
}


but it still doesn't scale the Image :/
Only the frame changes it's size.
Offline Screem
« Reply #11 - Posted 2013-06-23 21:13:48 »

If you want to render an image with a different width/height from the image itself, do this:
g.drawImage(BufferedImage yourImage, int x, int y, int width, int height, ImageObserver imageObserver);


ImageObserver can be null and width and height should be the width and height of your display if you want it to scale to your screen size.

Offline BurntPizza
« Reply #12 - Posted 2013-06-23 21:15:30 »

So the image stays the same size within the frame, even when you resize the frame?
You might (probably) need to call
1  
2  
g.dispose();
g = (Graphics2D) getGraphics();

whenever the frame changes size to "refresh" the graphics object.
Offline Raildex

Senior Newbie





« Reply #13 - Posted 2013-06-23 21:21:57 »

So the image stays the same size within the frame, even when you resize the frame?
You might (probably) need to call
1  
2  
g.dispose();
g = (Graphics2D) getGraphics();

whenever the frame changes size to "refresh" the graphics object.
The image is still small, when changing screensizes :/
Offline BurntPizza
« Reply #14 - Posted 2013-06-23 21:25:36 »

:\
Do a quick
1  
System.out.println("Width: "+getWidth()+", Height: "+getHeight());

before
1  
g.drawImage(buffer, 0, 0, OptionsManager.currentDimension.width, OptionsManager.currentDimension.height, 0, 0, getWidth(), getHeight(), null);

to make sure that the frame is returning the correct numbers.
Offline Raildex

Senior Newbie





« Reply #15 - Posted 2013-06-23 21:31:00 »

The Panel and Frame sizes are all correct...
Offline Screem
« Reply #16 - Posted 2013-06-23 21:32:17 »

*Ahem*

Try to draw the image using this method instead:
g.drawImage(BufferedImage yourImage, int x, int y, int width, int height, ImageObserver imageObserver);


As I said before, ImageObserver can be null and the width and height should be the width and height of your display.

Offline Raildex

Senior Newbie





« Reply #17 - Posted 2013-06-23 21:35:37 »

Oh, that works. Huh
I should read javadoc first when using methods Roll Eyes

Thanks to you guys :]

Just another question:

before I used a bufferedImage, I used RenderingHints.
But now with the bufferedImage, they don't work.
Some workaround to make them work again?
Offline BurntPizza
« Reply #18 - Posted 2013-06-23 21:47:25 »

Are  you setting the hints on the BufferedImage's graphics object? 'Cause since that's where the rendering happening, and that's where the hints should be a applied.
Offline Raildex

Senior Newbie





« Reply #19 - Posted 2013-06-23 21:55:48 »

I guess not.
My render-method looks like above:
1  
2  
3  
4  
5  
public void render()
  {
    gameScreenManager.render(buffer.createGraphics());
    g.drawImage(buffer, 0, 0, OptionsManager.currentDimension.width, OptionsManager.currentDimension.height,null);
  }



Works now, I created a new Graphics-Object.
Offline BurntPizza
« Reply #20 - Posted 2013-06-23 22:01:26 »

Well wherever you had g.addRenderingHints(), use buffer.createGraphics() instead of g.

Also
*Ahem*

Try to draw the image using this method instead:
g.drawImage(BufferedImage yourImage, int x, int y, int width, int height, ImageObserver imageObserver);


As I said before, ImageObserver can be null and the width and height should be the width and height of your display.

Sorry, yes that method works fine, for some reason I was under the impression that the method I recommended was the one, although I just checked and it is actually the inverse option! *whoops*
Offline Raildex

Senior Newbie





« Reply #21 - Posted 2013-06-23 22:08:30 »

yeah, thanks alot to you guys :]]
Offline BurntPizza
« Reply #22 - Posted 2013-06-23 22:15:30 »

No problem, see ya around  Smiley
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.

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (7 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (45 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!