}
@Override
public void paintInstance(InstancePainter painter) {
Graphics g = painter.getGraphics();
Bounds bds = painter.getBounds();
Direction facing = painter.getAttributeValue(StdAttr.FACING);
Object selectLoc = painter.getAttributeValue(Plexers.ATTR_SELECT_LOC);
BitWidth select = painter.getAttributeValue(Plexers.ATTR_SELECT);
boolean enable = painter.getAttributeValue(Plexers.ATTR_ENABLE).booleanValue();
int selMult = selectLoc == Plexers.SELECT_TOP_RIGHT ? -1 : 1;
int outputs = 1 << select.getWidth();
// draw stubs for select and enable ports
GraphicsUtil.switchToWidth(g, 3);
boolean vertical = facing == Direction.NORTH || facing == Direction.SOUTH;
int dx = vertical ? selMult : 0;
int dy = vertical ? 0 : -selMult;
if (outputs == 2) { // draw select wire
if (painter.getShowState()) {
g.setColor(painter.getPort(outputs).getColor());
}
Location pt = painter.getInstance().getPortLocation(outputs);
g.drawLine(pt.getX(), pt.getY(), pt.getX() + 2 * dx, pt.getY() + 2 * dy);
}
if (enable) {
Location en = painter.getInstance().getPortLocation(outputs + 1);
int len = outputs == 2 ? 6 : 4;
if (painter.getShowState()) {
g.setColor(painter.getPort(outputs + 1).getColor());
}
g.drawLine(en.getX(), en.getY(), en.getX() + len * dx, en.getY() + len * dy);
}
GraphicsUtil.switchToWidth(g, 1);
// draw a circle indicating where the select input is located
Multiplexer.drawSelectCircle(g, bds, painter.getInstance().getPortLocation(outputs));
// draw "0"
int x0;
int y0;
int halign;
if (facing == Direction.WEST) {
x0 = 3;
y0 = 15;
halign = GraphicsUtil.H_LEFT;
} else if (facing == Direction.NORTH) {
x0 = 10;
y0 = 15;
halign = GraphicsUtil.H_CENTER;
} else if (facing == Direction.SOUTH) {
x0 = 10;
y0 = bds.getHeight() - 3;
halign = GraphicsUtil.H_CENTER;
} else {
x0 = bds.getWidth() - 3;
y0 = 15;
halign = GraphicsUtil.H_RIGHT;
}
g.setColor(Color.GRAY);
GraphicsUtil.drawText(g, "0", bds.getX() + x0, bds.getY() + y0,
halign, GraphicsUtil.V_BASELINE);
// draw trapezoid, "Decd", and ports
g.setColor(Color.BLACK);
Plexers.drawTrapezoid(g, bds, facing.reverse(), outputs == 2 ? 10 : 20);
GraphicsUtil.drawCenteredText(g, "Decd",
bds.getX() + bds.getWidth() / 2,
bds.getY() + bds.getHeight() / 2);
painter.drawPorts();
}