How to put JFrame in FullScreen and automatically resize the content

I want to go full screen and keep everything in order.

How should I put a JFrame in full screen AND change everything inside: images, created drawings, etc. (e.g. scale it to fit the screen).

The problem is that I am making a full-screen application, but I don’t know on which screen it will be displayed.

This will put the frame in full screen, but the content will not be scaled

frame.dispose(); frame.setUndecorated(true); frame.setLocation(0, 0); frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize()); frame.setVisible(true); frame.repaint(); 
+4
source share
3 answers

If this is really what you want to do (see warnings from other answers), it is not too difficult to do (but takes a little time to understand). This basically includes the JPanel extension, and then overwriting the paint method.

Here is an example I came up with:

 import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; public class CustomPanel extends JPanel{ Component myComponent; public CustomPanel(){ super(); setLayout(null); } /** * Only allows one component to be added */ @Override public Component add(Component c){ super.add(c); c.setLocation(0, 0); c.setSize(c.getPreferredSize()); myComponent = c; return c; } @Override public void paint(final Graphics g){ Dimension d = this.getSize(); Dimension p = myComponent.getPreferredSize(); // Paints the child component to a image BufferedImage newImg = new BufferedImage(p.width, p.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = newImg.createGraphics(); super.paint(g2d); // Resizes the image if necessary Image img; if(d.height > p.height && d.width > p.width){ System.out.println("Scaled"); float changePercentage = 0; if(d.height/p.height > d.width/p.width){ changePercentage = (float)d.width/(float)p.width; } else{ changePercentage = (float)d.height/(float)p.height; } System.out.println(changePercentage); int newHeight = ((Float)(p.height * changePercentage)).intValue(); int newWidth = ((Float)(p.width * changePercentage)).intValue(); img = newImg.getScaledInstance(newWidth, newHeight, 0); } else{ System.out.println("Not Scaled"); img = newImg; } // Paints the image of the child component to the screen. g.drawImage(img, 0, 0, null); } public static void main(String[] args) { // TODO Auto-generated method stub SwingUtilities.invokeLater(new Runnable(){public void run(){ JFrame frame = new JFrame("Zoom Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); CustomPanel buffer = new CustomPanel(); JPanel content = new JPanel(); content.add(new JLabel("Bogus")); content.setBackground(Color.red); buffer.add(content); frame.setContentPane(buffer); frame.setVisible(true); new CustomPanel(); }}); } } 
+2
source

Depends on what you want to scale.

  • If you draw his graphics using Java2D, just figure out how much material you need to scale and use Graphics2D.scale () to scale gfx accordingly.

  • If this is something with a Swing layout, use the layout manager to create an adaptive layout.

  • If this is something else, more on your problem

+5
source

A few days ago, I just worked with a full-screen Java application. Take a look at the following link. if that was your requirement, I can help you to some extent.

https://docs.google.com/open?id=0B9U-BwYu62ZaeDM3SWZhaTdSYzQ

+1
source

All Articles