The program runs with its size according to screen resolution or size of the computer. When I run it in a computer with specific size of its monitor it will change accordingly. My problem is the positioning and size of a JPanel, or any object inside the frame, to adopt on the change of screen size.
So whenever I will present my program on any monitor with different sizes the components will still be organized and placed as what I've originally designed it. But here I'm testing it with one JPanel named displayMenu. In which it displays a panel colored in green.
package saves.project;
import com.sun.awt.AWTUtilities;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.BorderFactory;
import javax.imageio.ImageIO;
public class Homepage extends JFrame{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Border grayline = BorderFactory.createLineBorder(Color.GRAY);;
int width = screenSize.width, height = screenSize.height;
public Homepage() throws IOException{
super("Homepage");
displayMenu();
displayBackground();
}
public static BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(image, 0, 0, width, height, null);
g2d.dispose();
return bi;
}
public void displayBackground() throws IOException{
JPanel pBackground = new JPanel();
pBackground.setSize(screenSize);
pBackground.setLayout(new FlowLayout());
add(pBackground);
BufferedImage header = ImageIO.read(new File("res\\bg.jpg"));
BufferedImage resizedImage = resize(header,width,height);
ImageIcon image = new ImageIcon(resizedImage);
JLabel lblheader = new JLabel(image, JLabel.CENTER);
pBackground.add(lblheader);
}
public void displayMenu() {
JPanel pTitle = new JPanel();
pTitle.setLayout(null);
pTitle.setBounds(width/3, (height/2)+20, width/2, height/2);
pTitle.setBackground(Color.GREEN);
add(pTitle);
}
public void CreateAndShowGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setVisible(true);
}
}
Also, it looks like my background was not totally spread throughout the frame. There is a white line on the top but the rest is the background. What should I gonna do? Thanks for help!