Background layout/image processing issues with Swing based text adventure GUI

Hello all,

I’ve been trying to make my text adventure function at multiple screen resolutions and found a way to return the screen size and modify the layout based on relative rather than absolute positioning. In order to do this, I took a .jpg file to use as my background image and attempted to stretch it out (so it would change in resolution as opposed to content) and used the size of the .jpg file to position my other components. The problem is, the background has gone completely wonky, changing content and even position based on what resolution the application is started up in. I’m really lost as to what is going on and would really appreciate any assistance you could provide. The code for my constructor method:


public MainProgram()
{
	
	Dimension backgroundSize = new Dimension(0,0);
	
	//Let's try setting the default close operation
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	
	//Add the necessary content pane and set the layout.
	JLayeredPane contentPane = new JLayeredPane();
	contentPane.setLayout(null);
	contentPane.setBackground(Color.DARK_GRAY);
	
	
	//Set the content pane to the layered pane
	this.setContentPane(contentPane);
	
	//Create Wounded Label
	JLabel title = new JLabel();
	title.setText("WOUNDED");
	title.setFont(new Font("Courier", Font.BOLD, 48));
	title.setVisible(true);
	contentPane.add(title, new Integer(1));
	
	//Create the TextFeild for user input.
	userInput = new JTextField("", 15);
	userInput.setText("Enter your choice here");
	System.out.println("Text area");
	userInput.addActionListener(this);
	userInput.setVisible(true);
	contentPane.add(userInput, new Integer(3));
	
	//Create Choice label
	JLabel choice = new JLabel();
	choice.setText("CHOICE:");
	choice.setFont(new Font("Courier", Font.PLAIN, 20));
	choice.setVisible(true);
	contentPane.add(choice, new Integer(1));
	
	//Get insets and screen size
	Insets insets = contentPane.getInsets();
	Dimension size;
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	double width = screenSize.getWidth();
	double height = screenSize.getHeight();
	int intHeight = (int)height;
	int intWidth = (int)width;
	
	//Create the icon for the background label
	//note, I took some forum advice here and I'm not 100% clear on what the graphics class does
	try{
	final ImageIcon wallPaper = new ImageIcon(getClass().getResource("/background.jpg"));	
	Image wallPaperImage = wallPaper.getImage();
	BufferedImage wp = new BufferedImage(wallPaperImage.getWidth(null), wallPaperImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
	Graphics wp2 = wp.createGraphics();
	wp2.drawImage(wallPaperImage, 0, 0, intWidth, intHeight, null);
	ImageIcon newIcon = new ImageIcon(wp);
	JLabel background =  new JLabel(newIcon);
	contentPane.add(background, new Integer(0));
	size = background.getSize();
	background.setBounds(0, 0, intWidth, intHeight);
	backgroundSize = background.getSize();
	}
	catch (Exception e)
	{
		System.out.println("Error, file not found");
	}
	
	//Create the JTextArea for the display of story.
	output = new JTextArea(10, 20);
	scrollPane = new JScrollPane(output);
	output.setFont(new Font("Courier", Font.PLAIN, 12));
	output.setText(story);
	output.setVisible(true);
	scrollPane.setVisible(true);
	contentPane.add(scrollPane, new Integer(3));
	
	//Uses absolute/ quasi relative positioning to lay out the components
	//Let's try to do it relative to the size of the background image
	
	double backgroundWidth = backgroundSize.getWidth();
	double backgroundHeight = backgroundSize.getHeight();
	int backgroundIntHeight = (int)backgroundHeight;
	int backgroundIntWidth = (int)backgroundWidth;
	
	
	size = title.getPreferredSize();
	title.setBounds( (backgroundIntWidth/4), (backgroundIntHeight/200), size.width, size.height);
	size = userInput.getPreferredSize();
	userInput.setBounds((backgroundIntWidth/4), ((backgroundIntHeight/2) +(backgroundIntHeight/5)), size.width, size.height);
	size = choice.getPreferredSize();
	choice.setBounds((backgroundIntWidth/8),  ((backgroundIntHeight/2) + (backgroundIntHeight/5)), size.width, size.height);
	size = output.getSize();
	scrollPane.setBounds((backgroundIntWidth/14),  (backgroundIntHeight/15), (backgroundIntWidth - (backgroundIntWidth/4)), (backgroundIntHeight - (int)(backgroundIntHeight/2.75)));
	
	
	
	
	
	/*
	//Create the essential choose button to signal that
	//the program should retrieve user input from userInput.
	JButton chooseButton = new JButton();
	chooseButton.setText("Choose");
	chooseButton.addActionListener(this);
	contentPane.add(chooseButton);
	*/
}


And the code for my main method:


public static void main(String[] args)
{
	MainProgram f = new MainProgram();
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	double width = screenSize.getWidth();
	double height = screenSize.getHeight();
	int intHeight = (int)height;
	int intWidth = (int)width;
	f.setSize((intWidth - 50),(intHeight - 50));
	f.setVisible(true);
}


If you want to see the bug in action (it’s not damaging, just… well, kind of gross looking), you can run the program here:
http://www.java-gaming.org/user-generated-content/members/254501/wounded--multi-resolution-version-.jar

Issue largely resolved!