Skip to content

Commit

Permalink
Add opacity of the graphic interior (relative to border opacity)
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Dec 13, 2023
1 parent 5c398f7 commit a99bdfe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public abstract class AbstractGraphic extends DefaultUUID implements Graphic {
protected List<Point2D> pts;
protected Paint colorPaint = DEFAULT_COLOR;
protected Float lineThickness = DEFAULT_LINE_THICKNESS;
protected Float relativeInteriorOpacity = DEFAULT_INTERIOR_OPACITY;
protected Boolean labelVisible = DEFAULT_LABEL_VISIBLE;
protected Boolean filled = DEFAULT_FILLED;
protected Integer classID;
Expand All @@ -98,6 +99,7 @@ protected AbstractGraphic(AbstractGraphic graphic) {
setPointNumber(graphic.pointNumber);
setColorPaint(graphic.colorPaint);
setLineThickness(graphic.lineThickness);
setRelativeInteriorOpacity(relativeInteriorOpacity);
setLabelVisible(graphic.labelVisible);
setFilled(graphic.filled);
setClassID(graphic.classID);
Expand Down Expand Up @@ -215,6 +217,21 @@ public void setLineThickness(Float lineThickness) {
}
}

@XmlAttribute(name = "insideOpacity")
@Override
public Float getRelativeInteriorOpacity() {
return relativeInteriorOpacity;
}

@Override
public void setRelativeInteriorOpacity(Float relativeInteriorOpacity) {
if (!Objects.equals(this.relativeInteriorOpacity, relativeInteriorOpacity)) {
this.relativeInteriorOpacity =
Optional.ofNullable(relativeInteriorOpacity).orElse(DEFAULT_INTERIOR_OPACITY);
fireDrawingChanged();
}
}

@XmlAttribute(name = "showLabel")
@Override
public Boolean getLabelVisible() {
Expand Down Expand Up @@ -421,6 +438,10 @@ public void paint(Graphics2D g2d, AffineTransform transform) {
g2d.draw(drawingShape);

if (getFilled()) {
if (relativeInteriorOpacity < 1.0f && colorPaint instanceof Color color) {
int alpha = (int) (relativeInteriorOpacity * color.getAlpha());
g2d.setPaint(new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha));
}
g2d.fill(drawingShape);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
public interface Graphic extends UUIDable, GUIEntry, KeyActionValue, Copyable<Graphic> {
Color DEFAULT_COLOR = Color.YELLOW;
Float DEFAULT_LINE_THICKNESS = 1f;
Float DEFAULT_INTERIOR_OPACITY = 1f;
Boolean DEFAULT_LABEL_VISIBLE = Boolean.TRUE;
Boolean DEFAULT_FILLED = Boolean.FALSE;
Boolean DEFAULT_SELECTED = Boolean.FALSE;
Expand Down Expand Up @@ -90,6 +91,14 @@ default List<Measurement> getMeasurementList() {
*/
Float getLineThickness();

/**
* Returns the interior opacity value relative to the border color opacity (default value:
* <b>1.0</b>).
*
* @return The interior opacity value (between 0.0 and 1.0)
*/
Float getRelativeInteriorOpacity();

/**
* Returns a build Stroke object regarding the line thickness value.
*
Expand Down Expand Up @@ -196,6 +205,8 @@ default Boolean isLastPointValid() {

void setLineThickness(Float lineThickness);

void setRelativeInteriorOpacity(Float relativeInteriorOpacity);

void setPaint(Color newPaintColor);

void setPointNumber(Integer pointNumber);
Expand Down

0 comments on commit a99bdfe

Please sign in to comment.