Object powerLoc = painter.getAttributeValue(Wiring.ATTR_GATE);
Direction facing = painter.getAttributeValue(StdAttr.FACING);
boolean flip = (facing == Direction.SOUTH || facing == Direction.WEST)
== (powerLoc == Wiring.GATE_TOP_LEFT);
int degrees = Direction.WEST.toDegrees() - facing.toDegrees();
if (flip) degrees += 180;
double radians = Math.toRadians((degrees + 360) % 360);
Graphics2D g = (Graphics2D) painter.getGraphics().create();
g.rotate(radians, bds.getX() + 20, bds.getY() + 20);