GLabeledBox, take 1


Here’s the code for my first version of the GLabeledBox class.  This is what I mean by starting with a simple version and building up.  This just provides the necessary GObject overrides, and trows a fixed size box and a fixed text message up on the screen at a fixed location.

Very unusable for my intended purpose, but it works- I have the basic infrastructure up in very little time.  If inheriting from GObject as opposed to GLabel or GRect was not a feasible strategy, there’s a good chance that even in this minimal version of GLabeledBox, this would be apparent. I could change strategy without having too much invested in the current design.  Code itself behind a cut.

This code requires the acm library, information on the library and a download are found here:
http://cs.stanford.edu/people/eroberts/jtf/

/*Class to draw a box with a text label centered inside*/

import java.awt.Graphics;
import acm.graphics.GObject;
import acm.graphics.GRect;
import acm.graphics.GLabel;
import acm.graphics.GRectangle;

public class GLabeledBox extends GObject{
	private GRect box;
	private GLabel label;

	GLabeledBox(){
		box = new GRect(100, 100, 100, 100);
		label = new GLabel("Hello!", 100, 100);
	}

	@Override
	public GRectangle getBounds() {
		return box.getBounds();
	}

	@Override
	public void paint(Graphics arg0) {
		box.paint(arg0);
		label.paint(arg0);

	}
}
Advertisements

One Response to GLabeledBox, take 1

  1. Pingback: GLabeledBox Part 2 | Behind Blue Eyes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: