1

I have a JPanel inside a JFrame, the panel is around 500 x 500 and is basically a game that relies a lot on mouse clicks. However a lot of my users use a 4k monitor and the game looks really small to them. So for a cheap-fix I'm thinking how I'd go about zoom-scaling the panel when you scale up the frame.

Here's basically what my code is now:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
frame.setLayout(new BorderLayout());
setFocusTraversalKeysEnabled(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel gamePanel = new JPanel();
gamePanel.setLayout(new BorderLayout());
gamePanel.add(this);
gamePanel.setPreferredSize(new Dimension(765, 503));

JMenuBar jmenubar = new JMenuBar();
frame.add(jmenubar); 
frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true); // can see the client
frame.setResizable(true); // resizeable frame

init();

Example Pictures

How it is right now:

enter image description here

How I want to be able to resize it:

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Phillip J
  • 57
  • 6
  • Several approaches are cited [here](http://stackoverflow.com/a/8404125/230513). – trashgod Mar 25 '17 at 02:31
  • *"However a lot of my users use a 4k monitor"* - Not this poor user :( – MadProgrammer Mar 25 '17 at 02:39
  • One of the ideas I might suggest is to devise a concept of "scale factor" which you can apply to your event handlers and rendering, instead of simply "scaling" the graphics which is going to look horrible (unless you scale down), this will allow you to modify the way in which content is rendered and allow you to provide a unified solution to all parts of the app – MadProgrammer Mar 25 '17 at 02:41
  • Another solution might be to use something like `JLayer`/`JXLayer` and allow it to scale your program, it will also scale the input events for your, for [example](http://stackoverflow.com/questions/21174997/how-to-add-mouselistener-to-item-on-java-swing-canvas/21175125#21175125). However, I would start by designing your app to be 4K @ 100% and then scale down at other resolutions – MadProgrammer Mar 25 '17 at 02:43

2 Answers2

1

Assuming you have implemented a custom drawing(overriding paintComponent method) for your gamePanel, you need to scale the Graphics2D object with in your custom drawing.

Graphics2D g2d = (Graphics2D) g;
g2d.scale(scale, scale);

Where scale is derived based on your frame size.

If needed, You can use Frame's ComponentListener and implement componentResized method to get the size of the frame.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
upendra
  • 111
  • 5
  • Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Mar 25 '17 at 02:34
  • Want to show you would go able scaling the events as well – MadProgrammer Mar 25 '17 at 02:38
0

I don't think there is an easy solution. Register a resize listener for the jframe, calculate the zoom factor based on the window size and use this zoom factor to change the size and position of the elements in the jpanel.

Aloso
  • 5,123
  • 4
  • 24
  • 41