net.sf.gluebooster.java.booster.basic.gui.awt.Painter2DSimple Maven / Gradle / Ivy
package net.sf.gluebooster.java.booster.basic.gui.awt;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import net.sf.gluebooster.java.booster.essentials.awt.Painter2D;
import net.sf.gluebooster.java.booster.essentials.meta.objects.PaintDescription;
import net.sf.gluebooster.java.booster.essentials.utils.AWTBoostUtils;
/**
* A simple implementation of a painter.
*
* @author cbauer
*
*/
public class Painter2DSimple implements Painter2D {
/**
* The type to draw circles.
*/
private static final int TYPE_DRAW_CIRCLE = 0;
/**
* The type to draw strings.
*/
private static final int TYPE_DRAW_STRING = 1;
/**
* Type of this painter
*/
private int type = TYPE_DRAW_CIRCLE;
/**
* size of circles
*/
private int size = 10;
/**
* A margin in pixels from the drawn elements.
*/
private int margin = 2;
private Painter2DSimple(int type) {
this.type = type;
}
/**
* Creates a painter that creates circles
*
* @return the created painter
*/
public static Painter2DSimple createCirclePainter(int size) {
Painter2DSimple result = new Painter2DSimple(TYPE_DRAW_CIRCLE);
result.size = size;
return result;
}
/**
* Creates a painter that draws string
*
* @return the created painter
*/
public static Painter2DSimple createStringPainter() {
Painter2DSimple result = new Painter2DSimple(TYPE_DRAW_STRING);
return result;
}
@Override
public Rectangle2D getBounds2D(Object objectToPaint) throws Exception {
switch (type) {
case TYPE_DRAW_CIRCLE:
return new Rectangle(0, 0, size + 2 * margin, size + 2 * margin);
case TYPE_DRAW_STRING:
if (objectToPaint == null) {
return new Rectangle(margin, margin);
} else {
Rectangle result = AWTBoostUtils.getDefaultTextBounds("" + objectToPaint, null);
result.width = result.width + 2 * margin;
result.height = result.height + 2 * margin;
return result;
}
default:
throw new IllegalStateException("type not supported: " + type);
}
}
@Override
public void paint(PaintDescription paintDescription) throws Exception {
switch (type) {
case TYPE_DRAW_CIRCLE:
paintDescription.getGraphics().drawOval(paintDescription.getX(), paintDescription.getY(), size, size);
break;
case TYPE_DRAW_STRING:
String string = "";
if (paintDescription.getObjectToBePainted() != null) {
string = paintDescription.getObjectToBePainted().toString();
}
// AWTBoostUtils.drawStringCentered(string, paintDescription.getGraphics(), paintDescription.getX(), paintDescription.getY());
Graphics graphics = paintDescription.getGraphics();
graphics.drawString(string, margin + paintDescription.getX(), margin + paintDescription.getY() + graphics.getFontMetrics().getHeight());
break;
default:
throw new IllegalStateException("type not supported: " + type);
}
}
}