Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a menu for my game and I'm having a bit of a problem scaling my graphics when I re-size my JFrame. When the JFrame is scaled in a 4:3 ratio everything looks fine. But when the scale is not 4:3 the images starts to overlap each other. I do the calculations is my paintComponent.

Java
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Driver extends JPanel
{
	private static final long serialVersionUID = 1L;
	
	BufferedImage buff;
	Image images[] = new Image[6];
	JFrame jf;

	public static void main(String[] args)
	{
		Driver me = new Driver();
		me.start();
	}
	
	public void start()
	{
		try
		{
			images[0] = ImageIO.read(new File("res/Background.png"));	// 900x900
			
			images[1] = ImageIO.read(new File("res/1.png"));			// 500x100
			images[2] = ImageIO.read(new File("res/2.png"));			// 500x100
			images[3] = ImageIO.read(new File("res/3.png"));			// 500x100
			images[4] = ImageIO.read(new File("res/4.png"));			// 500x100
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		jf = new JFrame();
		
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setBounds(100, 100, 816, 638);								// JPanel start dimention 800x600
		//jf.setBounds(100, 100, 1040, 806);
		//jf.setBounds(100, 100, 1168, 902);
		jf.add(this);
		jf.setVisible(true);
	}
	
	public void paintComponent(Graphics g)
	{
		buff = new BufferedImage(jf.getWidth(), jf.getHeight(), BufferedImage.TYPE_INT_RGB);
		
		Graphics bbg = buff.getGraphics();
	    
		// Background
		bbg.drawImage(images[ 0 ], 0, 0, this.getWidth(), this.getHeight(), this);
	    
		int width = ( this.getWidth() / 8 ) * 6;
	    int height = ( this.getWidth() / 15 ) * 2;
	    int x = (this.getWidth() - width) / 2;
	    int y = (this.getHeight() / 15) * 3;
		
	 // Image 1
	    bbg.drawImage(images[ 1 ], x, y, width, height, this);
	    
	    width = ( this.getWidth() / 8 ) * 6;
	    height = ( this.getWidth() / 15 ) * 2;
	    x = (this.getWidth() - width) / 2;
	    y = (this.getHeight() / 15) * 6;
		
	 // Image 2
	    bbg.drawImage(images[ 2 ], x, y, width, height, this);
	    
	    width = ( this.getWidth() / 8 ) * 6;
	    height = ( this.getWidth() / 15 ) * 2;
	    x = (this.getWidth() - width) / 2;
	    y = (this.getHeight() / 15) * 9;
		
	 // Image 3
	    bbg.drawImage(images[ 3 ], x, y, width, height, this);
	    
	    width = ( this.getWidth() / 8 ) * 6;
	    height = ( this.getWidth() / 15 ) * 2;
	    x = (this.getWidth() - width) / 2;
	    y = (this.getHeight() / 15) * 12;
		
	 // Image 4
	    bbg.drawImage(images[ 4 ], x, y, width, height, this);
	    
	    
	g.drawImage(buff, 0, 0, this);
	}
}


<img alt="IMG example" src="https://lh3.googleusercontent.com/-Hsni_fqQqP8/U19U3vHhYwI/AAAAAAAAANg/AkLrkHjzxZY/s816/Game.png" />
<img alt="IMG example" src="https://lh3.googleusercontent.com/-Er2g8Bglu2o/U19VDF4vwZI/AAAAAAAAANs/e4H9k_22-C4/s1491/Game2.png" />
Posted

1 solution

If the ratio can be different from 4:3 then you have to use the actual ratio instead of constant factors, in your code.

[update]
if {ojw, ojh} are the old dimensions of the JPanel and {njw, njh} are the new ones, then every control should be scaled this way:
ncw = ocw * njw / ojw;
nch = och * njh / ojh;


(where {ncw, nch} are the control new dimensions and {ocw, och} are the old ones)
Of course you have to scale the control positions too.
[/update]
 
Share this answer
 
v2
Comments
wikus70 29-Apr-14 5:17am    
Well that's what my problem is. I don't know what the formula should be. Any ideas?
wikus70 29-Apr-14 5:42am    
Thank you very much. I found through debugging my code that I made a typo. During the height calculation I used getWidth() method instead of getHeight() method. But I like your calculation to :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900