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);
}
}
[…] ← GLabeledBox, take 1 […]