Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java graphics resize
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:
 
	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 22-Apr-13 3:11am
dteece1263
Edited 22-Apr-13 4:41am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100