Java - Different Resolutions, Same Coordinates - java

I would like the user of my software to resize the window that is presented to them when they launch the application. I can make the graphics of the game or application resize fine but I can't quite get the coordinates to be the same on every different size. If I have a collision detection box at 50,50 with the dimensions of 50,50. Of course the graphics are at those coordinates. But when I resize it the box stays in the same place but the graphic moves according to resizing.

When you resize the window, the size of the window changes, but the coordinates do not change.
What you need to do is register a listener to changing size, get the current size and change the coordinates of your detection box.

Related

Java frame for displaying always fullscreen game?

Currently I have JFrame that is always set to fullscreen, JPanel with a resolution that can be changed and a BufferedImage which is drawn onto the JPanel and is a fixed resolution of my game. I feel like this is very inefficient since there can be like three different resolutions, the fullscreen used for the JFrame being a screen resolution, a custom resolution for JPanel set by the user and the game's resolution could be different to both where I would be resizing twice before drawing.
How do modern games like Battlefield allow you to change resolution, they're always fullscreen and they're not just resizing in the window when they resize since I have a dual screen with one a TV and it actually shows me changed resolution due to changing the resolution of the game eventhough my TV can take the max of 1920 x 1080 but it's still fullscreen? All I need is really drawing to pixels, I don't even need adding buttons from swing or anything like that, I'll do that myself. My game is should always be fullscreen. I've been making my game and I'm really comfortable with using JFrame and JPanel but I really want to switch to something else if it's more efficient. What do you think I could do?
you could try this:
Toolkit.getDefaultToolkit().getScreenResolution();
or
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(app);
Also take a look at this post:
In JAVA, changing resolution with setDisplayMode for fullscreen application
Rendering to a BufferedImage of fixed size is somewhat defeating the purpose, unless you explicitly want scaling (e.g. because you can't render the full res fast enough).
All you need to do is resize your back buffer when the frame size changes (and render into it according to its current size, of course).
You can detect the changes easily by using either a WindowListener attached to the JFrame, or a ComponentListener attached to the contained panel. For flexibility, I personally would prefer the ComponentListener on the panel, because that enables you to use the same panel and listener for either windowed or full screen mode. Be cautious though that the listener will be called on the Swing event dispatch thread, so depending on the threading architecture of your game you may need some thread synchronization to communicate the change safely.
The way games like battlefield do it is with 3D world coordinates that are independent of the screen resolution using OpenGL or DirectX. Developers feed world coordinates into a rendering engine that then converts to screen coordinates. This is done with whatever the current screen resolution is set to. You will notice though that the HUD size will change depending on your selection of resolution. This is because the HUD is usually a 2D construct that is tied directly to screen coordinates. If you want to do some 3D stuff I would recommend LWJGL. Your only option with Swing is to look into using AffineTransform to scale your display. This however is inefficient compared to 3D and could be slower than you would desire.

How to smoothly resize LibGdx window

I'm trying to change my game window size. My tactic is to resize height when width is changed preserving aspect ratio (and viceversa). But when calling setDisplayMode, window blinks and is an ugly effect.
I'd like to access to lwjgl object to change the window properties (then I could change viewport settings accordingly in LibGdx). Does exists a way to change window size in LibGdx without setDisplayMode? Or at least, does exists a way to access LwjGL object inside the application?
Thanks

Is there a way to set the position of the application window in LWJGL/LibGDX?

LibGDX uses LWJGL as its backend, so LwjglApplicationConfiguration is used to set the properties of the window (such as resizable, width, height, etc) --- but there doesn't seem to be any setting to be able to set the window's position on the screen.
My game runs on a 1280x720 resolution. My laptop's resolution is 1366x768. When I run the game, a small portion of the game window is hidden at the bottom of the screen out of view. This is easily fixed by dragging the window up, but it would be better if dragging the window weren't necessary, and this could be handled by being able to set the window position.
So if there is any way to set the window's position so that it launches exactly where I want it to be, that would be great. Thanks!
You can set the position of the windo with
config.x = 0;
config.y = 0;

'image stretch' proxy for slow Swing component?

I have an application that displays plots using JFreeChart. As it happens, these plots are big and JFreeChart is not terribly fast, resulting in atrocious redraw times, particularly when resizing the plot.
What I am after is a way to stretch an image representing the plot while resizing (a bit like the iPhone will present a screenshot of a stopped application while it gets started again), and perform a full redraw only after the user has released the mouse (i.e. once the final size of the plot is known).
The interaction features of the chart sould stay intact (when not resizing, obviously).
Is there a generic solution / Swing wrapper for this? (there is no reason why it should be JFreeChart-specific).
Cheers
No concrete answer, just a possible strategy
on starting the resize, paint the plot into a BufferedImage and show and resize that image instead of the plot
on stopping resize remove the image and show the plot again
in JDK 7, you can use a JLayer for the image/manipulation.
Edit
Alternatively (for JLayer), you could use a CardLayout: showing one card with the image while resizing and the another card with the plot while not resizing. SwingX ImagePainter can do the image scaling during the resize

Java applet scale

I have a java game that I have made, is it a Canvas in a JFrame to run as an application and an applet.
When running as an applet, I want it so that when the user resizes the window, the game scales to fit the new size window.
Is there anyway of resizing the canvas and all the images or shapes that I have drawn on it to fit the new size window, or do I have to go though and change the code on all my objects and the positions they draw?
Any help would be very useful :)
You would have to add scaling to your painting. A transformation applied to your graphics might be the easiest way. You would determine the current size of the Canvas and scale based on the standard size.

Categories