Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a java program that will display 4 black squares in a certain pattern, and have them scale proportionally with the frame if it is resized. I have drawn the first two squares, however I am unable to draw squares on the right side or bottom of the frame while still making them scale when the frame is resized. Any help would be appreciated:

Java
	public void paint(Graphics g)
	{
		g.setColor(Color.white);
		int w = getWidth();
		int h = getHeight();
		g.fillRect(0,0,w,h);

		Graphics2D g2 = (Graphics2D)g;

		g2.setColor(Color.black);
		Rectangle r1 = new Rectangle(0, h/3, w/3, h/3);
		g2.fill(r1);

		g2.setColor(Color.black);
		Rectangle r2 = new Rectangle(w/3, h/500, w/3, h/3);
		g2.fill(r2);

		g2.setColor(Color.black);
		Rectangle r3 = new Rectangle(w/2, h/3, w/3, h/3);
		g2.fill(r3);
/*
		g2.setColor(Color.black);
		Rectangle r4 = new Rectangle(0, h/2, w/3, h/2);
		g2.fill(r4);*/
	}


	// copied from the W2MouseEvents for convenience
	// (so we can run the program from Test2Panel class too!)
	public static void main(String[] args)
	{
		Test2Frame w = new Test2Frame();
		w.setVisible(true);
	}
}
Posted
Updated 22-Apr-13 3:41am
v2

1 solution

Hi, if I understood, you want to update the frame when the window is resized.
You must use the ComponentListener Interface

and in componentResized(ComponentEvent e) method, you must update yours h and w variables and then call repaint method.

Best reguards
Filipe
 
Share this answer
 

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