cs106a – Assignment #6 – Extra Task #4

The complete specification of assignment #6 can be found as part of the stream at iTunes.

Adjust the font size as the application size changes

One of the wonderful features of this program is that it redraws itself to fill the available space if you change the size of the window. If you make it too small, however, the labels run together and become unreadable. You could eliminate this problem by choosing a font size that allows each label to fit in the space available.

Check if the label is wider than the column, if so adjust the font size:

	private void adjustSizeOfLabel(GLabel label) {
		double dx = getWidth() * 0.85 / NDECADES;
		double labelWidth = label.getWidth();
		if (labelWidth > dx) {
			Font font = label.getFont();
			label.setFont(font.deriveFont((float) (dx * font.getSize() / labelWidth)));
		}		
	}

	private void drawLabel(NameSurferEntry entry, int index, double x, double y) {
		...
		adjustSizeOfLabel(label);
		...
	}

	private void drawGrid() {
			...
			GLabel label = new GLabel(" " + year, x, yText);
			adjustSizeOfLabel(label);
			add(label);
			...
	}

The code for this assignment is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.