use of java.awt.Composite in project vassal by vassalengine.
the class ZoneHighlight method draw.
/*
* Stage 1 - Only Plain Style and Border Coverage is implemented
*/
public void draw(Graphics2D g2d, Shape s, double scale) {
if ((color != null && opacity > 0) || STYLE_IMAGE.equals(style)) {
final Stroke oldStroke = g2d.getStroke();
final Color oldColor = g2d.getColor();
final Composite oldComposite = g2d.getComposite();
final Paint oldPaint = g2d.getPaint();
if (!STYLE_PLAIN.equals(style)) {
g2d.setPaint(getPaint());
} else {
g2d.setColor(color);
}
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity / 100.0f));
if (COVERAGE_FULL.equals(coverage)) {
g2d.fill(s);
} else {
final Stroke stroke = new BasicStroke((float) (width * scale), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
g2d.setStroke(stroke);
g2d.draw(s);
}
g2d.setColor(oldColor);
g2d.setStroke(oldStroke);
g2d.setComposite(oldComposite);
g2d.setPaint(oldPaint);
}
}
use of java.awt.Composite in project com.revolsys.open by revolsys.
the class Graphics2DTextStyleRenderer method drawText.
@Override
public void drawText(final String label, final Geometry geometry) {
final Graphics2DViewRenderer view = this.view;
double dx = this.dx;
double dy = this.dy;
if (Property.hasValue(label) && geometry != null) {
final TextStyle style = this.style;
final String textPlacementType = style.getTextPlacementType();
final PointDoubleXYOrientation point = view.getPointWithOrientation(geometry, textPlacementType);
if (point != null) {
double orientation;
final String orientationType = style.getTextOrientationType();
if ("none".equals(orientationType)) {
orientation = 0;
} else {
orientation = point.getOrientation();
if (orientation > 270) {
orientation -= 360;
}
}
orientation += style.getTextOrientation();
final Graphics2D graphics = this.graphics;
final Paint paint = graphics.getPaint();
final Composite composite = graphics.getComposite();
final AffineTransform savedTransform = graphics.getTransform();
try {
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
final double[] coordinates = this.coordinates;
point.copyCoordinates(coordinates);
view.toViewCoordinates(coordinates);
style.setTextStyle(view, graphics);
final FontMetrics fontMetrics = graphics.getFontMetrics();
double maxWidth = 0;
final String[] lines = label.split("[\\r\\n]");
for (final String line : lines) {
final Rectangle2D bounds = fontMetrics.getStringBounds(line, graphics);
final double width = bounds.getWidth();
maxWidth = Math.max(width, maxWidth);
}
final int descent = fontMetrics.getDescent();
final int ascent = fontMetrics.getAscent();
final int leading = fontMetrics.getLeading();
final double maxHeight = lines.length * (ascent + descent) + (lines.length - 1) * leading;
final String verticalAlignment = style.getTextVerticalAlignment();
if ("top".equals(verticalAlignment)) {
} else if ("middle".equals(verticalAlignment)) {
dy -= maxHeight / 2;
} else {
dy -= maxHeight;
}
String horizontalAlignment = style.getTextHorizontalAlignment();
double screenX = coordinates[0];
double screenY = coordinates[1];
final String textPlacement = textPlacementType;
if ("auto".equals(textPlacement) && view != null) {
if (screenX < 0) {
screenX = 1;
dx = 0;
horizontalAlignment = "left";
}
final double viewWidth = view.getViewWidthPixels();
if (screenX + maxWidth > viewWidth) {
screenX = (int) (viewWidth - maxWidth - 1);
dx = 0;
horizontalAlignment = "left";
}
if (screenY < maxHeight) {
screenY = 1;
dy = 0;
}
final double viewHeight = view.getViewHeightPixels();
if (screenY > viewHeight) {
screenY = viewHeight - 1 - maxHeight;
dy = 0;
}
}
graphics.translate(screenX, screenY);
if (orientation != 0) {
graphics.rotate(-Math.toRadians(orientation), 0, 0);
}
graphics.translate(dx, dy);
for (final String line : lines) {
graphics.translate(0, ascent);
final AffineTransform lineTransform = graphics.getTransform();
final Rectangle2D bounds = fontMetrics.getStringBounds(line, graphics);
final double width = bounds.getWidth();
final double height = bounds.getHeight();
if ("right".equals(horizontalAlignment)) {
graphics.translate(-width, 0);
} else if ("center".equals(horizontalAlignment) || "auto".equals(horizontalAlignment)) {
graphics.translate(-width / 2, 0);
}
graphics.translate(dx, 0);
graphics.scale(1, 1);
if (Math.abs(orientation) > 90) {
graphics.rotate(Math.PI, maxWidth / 2, -height / 4);
}
final int textBoxOpacity = style.getTextBoxOpacity();
final Color textBoxColor = style.getTextBoxColor();
if (textBoxOpacity > 0 && textBoxColor != null) {
graphics.setPaint(textBoxColor);
final double cornerSize = Math.max(height / 2, 5);
final RoundRectangle2D.Double box = new RoundRectangle2D.Double(bounds.getX() - 3, bounds.getY() - 1, width + 6, height + 2, cornerSize, cornerSize);
graphics.fill(box);
}
final double radius = style.getTextHaloRadius();
final Unit<Length> unit = style.getTextSizeUnit();
final double textHaloRadius = view.toDisplayValue(Quantities.getQuantity(radius, unit));
if (textHaloRadius > 0) {
final Stroke savedStroke = graphics.getStroke();
final Stroke outlineStroke = new BasicStroke((float) (textHaloRadius + 1), BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
graphics.setColor(style.getTextHaloFill());
graphics.setStroke(outlineStroke);
final Font font = graphics.getFont();
final FontRenderContext fontRenderContext = graphics.getFontRenderContext();
final TextLayout textLayout = new TextLayout(line, font, fontRenderContext);
final Shape outlineShape = textLayout.getOutline(Graphics2DViewRenderer.IDENTITY_TRANSFORM);
graphics.draw(outlineShape);
graphics.setStroke(savedStroke);
}
graphics.setColor(style.getTextFill());
if (textBoxOpacity > 0 && textBoxOpacity < 255) {
graphics.setComposite(AlphaComposite.SrcOut);
graphics.drawString(line, (float) 0, (float) 0);
graphics.setComposite(AlphaComposite.DstOver);
graphics.drawString(line, (float) 0, (float) 0);
} else {
graphics.setComposite(AlphaComposite.SrcOver);
graphics.drawString(line, (float) 0, (float) 0);
}
graphics.setTransform(lineTransform);
graphics.translate(0, leading + descent);
}
} finally {
graphics.setTransform(savedTransform);
graphics.setPaint(paint);
graphics.setComposite(composite);
}
}
}
}
use of java.awt.Composite in project josm by openstreetmap.
the class StyledMapRenderer method drawRepeatImage.
/**
* Draw an image along a way repeatedly.
*
* @param way the way
* @param pattern the image
* @param disabled If this should be drawn with a special disabled style.
* @param offset offset from the way
* @param spacing spacing between two images
* @param phase initial spacing
* @param opacity the opacity
* @param align alignment of the image. The top, center or bottom edge can be aligned with the way.
*/
public void drawRepeatImage(IWay<?> way, MapImage pattern, boolean disabled, double offset, double spacing, double phase, float opacity, LineImageAlignment align) {
final int imgWidth = pattern.getWidth();
final double repeat = imgWidth + spacing;
final int imgHeight = pattern.getHeight();
int dy1 = (int) ((align.getAlignmentOffset() - .5) * imgHeight);
int dy2 = dy1 + imgHeight;
OffsetIterator it = new OffsetIterator(mapState, way.getNodes(), offset);
MapViewPath path = new MapViewPath(mapState);
if (it.hasNext()) {
path.moveTo(it.next());
}
while (it.hasNext()) {
path.lineTo(it.next());
}
double startOffset = computeStartOffset(phase, repeat);
Image image = pattern.getImage(disabled);
path.visitClippedLine(repeat, (inLineOffset, start, end, startIsOldEnd) -> {
final double segmentLength = start.distanceToInView(end);
if (segmentLength < 0.1) {
// avoid odd patterns when zoomed out.
return;
}
if (segmentLength > repeat * 500) {
// simply skip drawing so many images - something must be wrong.
return;
}
AffineTransform saveTransform = g.getTransform();
g.translate(start.getInViewX(), start.getInViewY());
double dx = end.getInViewX() - start.getInViewX();
double dy = end.getInViewY() - start.getInViewY();
g.rotate(Math.atan2(dy, dx));
// The start of the next image
// It is shifted by startOffset.
double imageStart = -((inLineOffset - startOffset + repeat) % repeat);
while (imageStart < segmentLength) {
int x = (int) imageStart;
int sx1 = Math.max(0, -x);
int sx2 = imgWidth - Math.max(0, x + imgWidth - (int) Math.ceil(segmentLength));
Composite saveComposite = g.getComposite();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
g.drawImage(image, x + sx1, dy1, x + sx2, dy2, sx1, 0, sx2, imgHeight, null);
g.setComposite(saveComposite);
imageStart += repeat;
}
g.setTransform(saveTransform);
});
}
use of java.awt.Composite in project josm by openstreetmap.
the class GpxDrawHelper method drawAll.
/**
* Draw all enabled GPX elements of layer.
* @param g the common draw object to use
* @param mv the meta data to current displayed area
* @param visibleSegments segments visible in the current scope of mv
* @param clipBounds the clipping rectangle for the current view
* @since 14748 : new parameter clipBounds
*/
public void drawAll(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, Bounds clipBounds) {
final Stopwatch stopwatch = Stopwatch.createStarted();
checkCache();
// STEP 2b - RE-COMPUTE CACHE DATA *********************
if (!computeCacheInSync) {
// don't compute if the cache is good
calculateColors();
// update the WaiPoint.drawline attributes
visibleSegments.clear();
visibleSegments.addAll(listVisibleSegments(clipBounds));
}
fixColors(visibleSegments);
// backup the environment
Composite oldComposite = g.getComposite();
Stroke oldStroke = g.getStroke();
Paint oldPaint = g.getPaint();
// set hints for the render
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, Config.getPref().getBoolean("mappaint.gpx.use-antialiasing", false) ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
if (lineWidth > 0) {
g.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
}
// global enabled or select via color
boolean useHeatMap = ColorMode.HEATMAP == colored;
// default global alpha level
float layerAlpha = 1.00f;
// extract current alpha blending value
if (oldComposite instanceof AlphaComposite) {
layerAlpha = ((AlphaComposite) oldComposite).getAlpha();
}
// use heatmap background layer
if (useHeatMap) {
drawHeatMap(g, mv, visibleSegments);
} else {
// use normal line style or alpha-blending lines
if (!alphaLines) {
drawLines(g, mv, visibleSegments);
} else {
drawLinesAlpha(g, mv, visibleSegments, layerAlpha);
}
}
// override global alpha settings (smooth overlay)
if (alphaLines || useHeatMap) {
g.setComposite(AlphaComposite.SrcOver.derive(0.25f * layerAlpha));
}
// normal overlays
drawArrows(g, mv, visibleSegments);
drawPoints(g, mv, visibleSegments);
// restore environment
g.setPaint(oldPaint);
g.setStroke(oldStroke);
g.setComposite(oldComposite);
// show some debug info
if (Logging.isDebugEnabled() && !visibleSegments.isEmpty()) {
Logging.debug(stopwatch.toString("gpxdraw::draw") + "(" + "segments= " + visibleSegments.size() + ", per 10000 = " + Utils.getDurationString(10_000 * stopwatch.elapsed() / visibleSegments.size()) + ")");
}
}
use of java.awt.Composite in project josm by openstreetmap.
the class OsmDataLayer method createHatchTexture.
/**
* Initialize the hatch pattern used to paint the non-downloaded area
*/
public static void createHatchTexture() {
BufferedImage bi = new BufferedImage(HATCHED_SIZE, HATCHED_SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
big.setColor(getBackgroundColor());
Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
big.setComposite(comp);
big.fillRect(0, 0, HATCHED_SIZE, HATCHED_SIZE);
big.setColor(getOutsideColor());
big.drawLine(-1, 6, 6, -1);
big.drawLine(4, 16, 16, 4);
hatched = bi;
}
Aggregations