use of java.awt.Composite in project josm by JOSM.
the class GpxDrawHelper method drawLinesAlpha.
/**
* Draw GPX lines by using alpha blending
* @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 layerAlpha the color alpha value set for that operation
*/
private void drawLinesAlpha(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, float layerAlpha) {
// 1st. backup the paint environment ----------------------------------
Composite oldComposite = g.getComposite();
Stroke oldStroke = g.getStroke();
Paint oldPaint = g.getPaint();
// 2nd. determine current scale factors -------------------------------
// adjust global settings
final int globalLineWidth = Utils.clamp(lineWidth, 1, 20);
// cache scale of view
final double zoomScale = mv.getDist100Pixel() / 50.0f;
// 3rd. determine current paint parameters -----------------------------
// alpha value is based on zoom and line with combined with global layer alpha
float theLineAlpha = (float) Utils.clamp((0.50 / zoomScale) / (globalLineWidth + 1), 0.01, 0.50) * layerAlpha;
final int theLineWith = (int) (lineWidth / zoomScale) + 1;
// 4th setup virtual paint area ----------------------------------------
// set line format and alpha channel for all overlays (more lines -> few overlap -> more transparency)
g.setStroke(new BasicStroke(theLineWith, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g.setComposite(AlphaComposite.SrcOver.derive(theLineAlpha));
// last used / calculated entries
Point lastPaintPnt = null;
// for all points
for (WayPoint trkPnt : visibleSegments) {
// transform coordinates
final Point paintPnt = mv.getPoint(trkPnt);
// skip single points
if (lastPaintPnt != null && trkPnt.drawLine && !lastPaintPnt.equals(paintPnt)) {
// set different color
g.setColor(trkPnt.customColoring);
// draw it
g.drawLine(lastPaintPnt.x, lastPaintPnt.y, paintPnt.x, paintPnt.y);
}
lastPaintPnt = paintPnt;
}
// @last restore modified paint environment -----------------------------
g.setPaint(oldPaint);
g.setStroke(oldStroke);
g.setComposite(oldComposite);
}
use of java.awt.Composite in project josm by JOSM.
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;
}
use of java.awt.Composite in project josm by JOSM.
the class GeoImageLayer method paint.
@Override
public void paint(Graphics2D g, MapView mv, Bounds bounds) {
int width = mv.getWidth();
int height = mv.getHeight();
Rectangle clip = g.getClipBounds();
if (useThumbs) {
if (!thumbsLoaded) {
startLoadThumbs();
}
if (null == offscreenBuffer || // reuse the old buffer if possible
offscreenBuffer.getWidth() != width || offscreenBuffer.getHeight() != height) {
offscreenBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
updateOffscreenBuffer = true;
}
if (updateOffscreenBuffer) {
Graphics2D tempG = offscreenBuffer.createGraphics();
tempG.setColor(new Color(0, 0, 0, 0));
Composite saveComp = tempG.getComposite();
// remove the old images
tempG.setComposite(AlphaComposite.Clear);
tempG.fillRect(0, 0, width, height);
tempG.setComposite(saveComp);
for (ImageEntry e : data.searchImages(bounds)) {
paintImage(e, mv, clip, tempG);
}
for (ImageEntry img : this.data.getSelectedImages()) {
// Make sure the selected image is on top in case multiple images overlap.
paintImage(img, mv, clip, tempG);
}
updateOffscreenBuffer = false;
}
g.drawImage(offscreenBuffer, 0, 0, null);
} else {
for (ImageEntry e : data.searchImages(bounds)) {
if (e.getPos() == null) {
continue;
}
Point p = mv.getPoint(e.getPos());
icon.paintIcon(mv, g, p.x - icon.getIconWidth() / 2, p.y - icon.getIconHeight() / 2);
}
}
for (ImageEntry e : data.getSelectedImages()) {
if (e != null && e.getPos() != null) {
Point p = mv.getPoint(e.getPos());
Dimension imgDim = getImageDimension(e);
if (e.getExifImgDir() != null) {
Vector3D imgRotation = ImageViewerDialog.getInstance().getRotation(e);
drawDirectionArrow(g, p, e.getExifImgDir() + (imgRotation != null ? Utils.toDegrees(imgRotation.getPolarAngle()) : 0d), imgDim);
}
if (useThumbs && e.hasThumbnail()) {
g.setColor(new Color(128, 0, 0, 122));
g.fillRect(p.x - imgDim.width / 2, p.y - imgDim.height / 2, imgDim.width, imgDim.height);
} else {
selectedIcon.paintIcon(mv, g, p.x - imgDim.width / 2, p.y - imgDim.height / 2);
}
}
}
}
use of java.awt.Composite in project knime-base by knime.
the class DrawingUtils method drawBlock.
/**
* Draws a filled shape without a border.
* @param g2 the graphic object
* @param shape the shape to fill
* @param paint the filling color or TexturePaint
* @param alpha the transparency
*/
public static void drawBlock(final Graphics2D g2, final Shape shape, final Paint paint, final float alpha) {
if (shape == null) {
return;
}
// save the original settings
final Paint origPaint = g2.getPaint();
final Composite originalComposite = g2.getComposite();
// draw the color block
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2.setPaint(paint);
g2.fill(shape);
// set the old settings
g2.setPaint(origPaint);
g2.setComposite(originalComposite);
}
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);
}
}
}
}
Aggregations