Java Code Examples for java.awt.Graphics2D#getStroke()

The following examples show how to use java.awt.Graphics2D#getStroke() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Underline.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
void drawUnderline(Graphics2D g2d,
                   float thickness,
                   float x1,
                   float x2,
                   float y) {

    Stroke saveStroke = g2d.getStroke();
    g2d.setStroke(stroke);

    Line2D.Float drawLine = new Line2D.Float(x1, y, x2, y);
    g2d.draw(drawLine);

    drawLine.y1 += DEFAULT_THICKNESS;
    drawLine.y2 += DEFAULT_THICKNESS;
    drawLine.x1 += DEFAULT_THICKNESS;

    g2d.draw(drawLine);

    g2d.setStroke(saveStroke);
}
 
Example 2
Source File: ZyEdgeLabel.java    From binnavi with Apache License 2.0 6 votes vote down vote up
@Override
protected void paintBox(final Graphics2D gfx, final double x, final double y,
    final double width1, final double height2) {

  // final Graph2D g = (Graph2D) this.getEdge().getGraph();

  final int roundedX = (int) (x - (m_width / 2));
  final int roundedY = (int) (y - (m_height / 2));

  final BasicStroke oldStroke = (BasicStroke) gfx.getStroke();
  gfx.setStroke(new BasicStroke(oldStroke.getLineWidth()));

  gfx.setColor(m_backGroundColor);

  gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.80f));
  gfx.fillRoundRect(roundedX, roundedY, m_roundedWidth, m_roundedHeight, 5, 5);
  gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.95f));

  gfx.setColor(borderColor);
  gfx.drawRoundRect(roundedX, roundedY, m_roundedWidth, m_roundedHeight, SHADOW_SIZE, SHADOW_SIZE);
  gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));

  gfx.setStroke(oldStroke);
}
 
Example 3
Source File: Underline.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
void drawUnderline(Graphics2D g2d,
                   float thickness,
                   float x1,
                   float x2,
                   float y) {

    Stroke saveStroke = g2d.getStroke();
    g2d.setStroke(stroke);

    Line2D.Float drawLine = new Line2D.Float(x1, y, x2, y);
    g2d.draw(drawLine);

    drawLine.y1 += DEFAULT_THICKNESS;
    drawLine.y2 += DEFAULT_THICKNESS;
    drawLine.x1 += DEFAULT_THICKNESS;

    g2d.draw(drawLine);

    g2d.setStroke(saveStroke);
}
 
Example 4
Source File: Trail.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws the trail on the panel.
 * @param g
 */
public void draw(DrawingPanel panel, Graphics g) {
  if(numpts==0) {
    return;
  }
  Graphics2D g2 = (Graphics2D) g;
  g2.setColor(color);
  // transform from world to pixel coordinates
  Shape s = generalPath.createTransformedShape(panel.getPixelTransform());
  if(drawingStroke!=null) {
    Stroke stroke = g2.getStroke();
    g2.setStroke(drawingStroke);
    g2.draw(s);
    g2.setStroke(stroke);
  } else {
    g2.draw(s);
  }
}
 
Example 5
Source File: SupportLayerRenderer.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
@Override
public void performRendering(RenderSettings pSettings, Graphics2D pG2d) {
    // RenderSettings settings = getRenderSettings(pVirtualBounds);
    if (!pSettings.isLayerVisible()) {
        return;
    }
    Point2D.Double mapPos = new Point2D.Double(pSettings.getMapBounds().getX(), pSettings.getMapBounds().getY());
    Stroke s = pG2d.getStroke();
    Color b = pG2d.getColor();
    List<Village> visibleVillages = new LinkedList<>();
    for (int i = 0; i < pSettings.getVillagesInX(); i++) {
        for (int j = 0; j < pSettings.getVillagesInY(); j++) {
            Village v = pSettings.getVisibleVillage(i, j);
            if (v != null) {
                visibleVillages.add(v);
            }
        }
    }
    pG2d.setStroke(new BasicStroke(2.0F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
    renderSupports(mapPos, visibleVillages, pSettings, pG2d);
    pG2d.setStroke(s);
    pG2d.setColor(b);
}
 
Example 6
Source File: SwingTextRenderer.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private final void renderOutline( IPrimitiveRenderer ipr,
		LineAttributes lia, Rectangle2D.Double r2d )
{
	if ( lia != null && lia.isVisible( ) && lia.getColor( ) != null )
	{
		Graphics2D g2d = (Graphics2D) ( (IDeviceRenderer) ipr ).getGraphicsContext( );
		Stroke sPrevious = null;
		final ColorDefinition cd = lia.getColor( );
		final Stroke sCurrent = ( (SwingRendererImpl) ipr ).getCachedStroke( lia );
		if ( sCurrent != null ) // SOME STROKE DEFINED?
		{
			sPrevious = g2d.getStroke( );
			g2d.setStroke( sCurrent );
		}
		g2d.setColor( (Color) _sxs.getColor( cd ) );
		g2d.draw( r2d );
		if ( sPrevious != null ) // RESTORE PREVIOUS STROKE
		{
			g2d.setStroke( sPrevious );
		}
	}
}
 
Example 7
Source File: Underline.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
void drawUnderline(Graphics2D g2d,
                   float thickness,
                   float x1,
                   float x2,
                   float y) {

    Stroke saveStroke = g2d.getStroke();
    g2d.setStroke(stroke);

    Line2D.Float drawLine = new Line2D.Float(x1, y, x2, y);
    g2d.draw(drawLine);

    drawLine.y1 += DEFAULT_THICKNESS;
    drawLine.y2 += DEFAULT_THICKNESS;
    drawLine.x1 += DEFAULT_THICKNESS;

    g2d.draw(drawLine);

    g2d.setStroke(saveStroke);
}
 
Example 8
Source File: BlockWidget.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void paintWidget() {
    super.paintWidget();
    Graphics2D g = this.getGraphics();
    Stroke old = g.getStroke();
    g.setColor(Color.BLUE);
    Rectangle r = new Rectangle(this.getPreferredBounds());
    r.width--;
    r.height--;
    if (this.getBounds().width > 0 && this.getBounds().height > 0) {
        g.setStroke(new BasicStroke(2));
        g.drawRect(r.x, r.y, r.width, r.height);
    }

    Color titleColor = Color.BLACK;
    g.setColor(titleColor);
    g.setFont(titleFont);

    String s = "B" + blockNode.toString();
    Rectangle2D r1 = g.getFontMetrics().getStringBounds(s, g);
    g.drawString(s, r.x + 5, r.y + (int) r1.getHeight());
    g.setStroke(old);
}
 
Example 9
Source File: TurnSlashSymbol.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void paint (Graphics2D g,
                      Params params,
                      Point location,
                      Alignment alignment)
{
    MyParams p = (MyParams) params;
    Point loc = alignment.translatedPoint(AREA_CENTER, p.rect, location);
    MusicFont.paint(g, p.layout, loc, AREA_CENTER);

    Stroke oldStroke = g.getStroke();
    g.setStroke(p.stroke);
    g.drawLine(loc.x, location.y, loc.x, location.y + p.rect.height);
    g.setStroke(oldStroke);
}
 
Example 10
Source File: SplineDisplay.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void paintControlPoint(Graphics2D g2, Point2D control) {
    double origin_x = xPositionToPixel(control.getX());
    double origin_y = yPositionToPixel(control.getY());
    double pos = control == control1 ? 0.0 : 1.0;
    
    Ellipse2D outer = getDraggableArea(control);
    Ellipse2D inner = new Ellipse2D.Double(origin_x + 2.0 - CONTROL_POINT_SIZE / 2.0,
                                           origin_y + 2.0 - CONTROL_POINT_SIZE / 2.0,
                                           8.0, 8.0);
    
    Area circle = new Area(outer);
    circle.subtract(new Area(inner));
    
    Stroke stroke = g2.getStroke();
    g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                                 5, new float[] { 5, 5 }, 0));
    g2.setColor(new Color(1.0f, 0.0f, 0.0f, 0.4f));
    g2.drawLine(0, (int) origin_y, (int) origin_x, (int) origin_y);
    g2.drawLine((int) origin_x, (int) origin_y, (int) origin_x, getHeight());
    g2.setStroke(stroke);
    
    if (selected == control) {
        g2.setColor(new Color(1.0f, 1.0f, 1.0f, 1.0f));
    } else {
        g2.setColor(new Color(0.8f, 0.8f, 0.8f, 0.6f));
    }
    g2.fill(inner);
    
    g2.setColor(new Color(0.0f, 0.0f, 0.5f, 0.5f));
    g2.fill(circle);
    
    g2.drawLine((int) origin_x, (int) origin_y,
                (int) xPositionToPixel(pos), (int) yPositionToPixel(pos));
}
 
Example 11
Source File: Shapes.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
public static void debug(Graphics2D g, Color c, Shape shape) {
    Color origColor = g.getColor();
    Stroke origStroke = g.getStroke();

    g.setColor(c);
    g.setStroke(new BasicStroke(5));
    g.draw(shape);

    g.setColor(origColor);
    g.setStroke(origStroke);
}
 
Example 12
Source File: DiagramCanvas.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void drawValueIndicator(Graphics2D g2d) {
    Diagram.RectTransform transform = diagram.getTransform();
    Point2D a = transform.transformB2A(dragPoint, null);
    double x = a.getX();
    if (x < selectedGraph.getXMin()) {
        x = selectedGraph.getXMin();
    }
    if (x > selectedGraph.getXMax()) {
        x = selectedGraph.getXMax();
    }
    final Stroke oldStroke = g2d.getStroke();
    final Color oldColor = g2d.getColor();

    double y = getY(selectedGraph, x);
    Point2D b = transform.transformA2B(new Point2D.Double(x, y), null);

    g2d.setStroke(new BasicStroke(1.0f));
    g2d.setColor(diagram.getForegroundColor());
    Ellipse2D.Double marker = new Ellipse2D.Double(b.getX() - 4.0, b.getY() - 4.0, 8.0, 8.0);
    g2d.draw(marker);

    g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{6, 6}, 12));
    g2d.setColor(diagram.getForegroundColor());
    final Rectangle graphArea = diagram.getGraphArea();
    g2d.draw(new Line2D.Double(b.getX(), graphArea.y + graphArea.height, b.getX(), b.getY()));
    g2d.draw(new Line2D.Double(graphArea.x, b.getY(), b.getX(), b.getY()));

    DecimalFormat decimalFormat = new DecimalFormat("0.#####E0");
    String text = selectedGraph.getYName() + ": x = " + decimalFormat.format(x) + ", y = " + decimalFormat.format(y);

    g2d.setStroke(oldStroke);
    g2d.setColor(oldColor);

    drawTextBox(g2d, text, graphArea.x + 6, graphArea.y + 6 + 16, new Color(255, 255, 255, 128));
}
 
Example 13
Source File: GraphicsUtils.java    From tensorflow with Apache License 2.0 5 votes vote down vote up
/**
 * Augments the input image with a labeled rectangle (e.g. bounding box) with coordinates: (x1, y1, x2, y2).
 *
 * @param image Input image to be augmented with labeled rectangle.
 * @param cid Unique id used to select the color of the rectangle. Used only if the colorAgnostic is set to false.
 * @param title rectangle title
 * @param x1 top left corner for the bounding box
 * @param y1 top left corner for the bounding box
 * @param x2 bottom right corner for the bounding box
 * @param y2 bottom right corner for the bounding box
 * @param colorAgnostic If set to false the cid is used to select the bounding box color. Uses the
 *                      AGNOSTIC_COLOR otherwise.
 */
public static void drawBoundingBox(BufferedImage image, int cid, String title, int x1, int y1, int x2, int y2,
		boolean colorAgnostic) {

	Graphics2D g = image.createGraphics();
	g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

	Color labelColor = colorAgnostic ? AGNOSTIC_COLOR : GraphicsUtils.getClassColor(cid);
	g.setColor(labelColor);

	g.setFont(DEFAULT_FONT);
	FontMetrics fontMetrics = g.getFontMetrics();

	Stroke oldStroke = g.getStroke();
	g.setStroke(new BasicStroke(LINE_THICKNESS));
	g.drawRect(x1, y1, (x2 - x1), (y2 - y1));
	g.setStroke(oldStroke);

	Rectangle2D rect = fontMetrics.getStringBounds(title, g);

	g.setColor(labelColor);
	g.fillRect(x1, y1 - fontMetrics.getAscent(),
			(int) rect.getWidth() + 2 * TITLE_OFFSET, (int) rect.getHeight());

	g.setColor(getTextColor(labelColor));
	g.drawString(title, x1 + TITLE_OFFSET, y1);
}
 
Example 14
Source File: GridPanel.java    From Rails with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void paintBorder(Component c,Graphics g, int x, int y, int width,int height) {
    Graphics2D g2d = (Graphics2D)g;
    if (isHighlighted) {
        g2d.setColor(highlightedBorderColor);
    } else {
        g2d.setColor(borderColor);
    }
    Stroke oldStroke = g2d.getStroke();
    if (padTop > 0) {
        g2d.setStroke(new BasicStroke(padTop));
        g2d.fillRect(x, y, width, padTop);
    }
    if (padLeft > 0) {
        g2d.setStroke(new BasicStroke(padLeft));
        g2d.fillRect(x, y, padLeft, height);
    }
    if (padBottom > 0) {
        g2d.setStroke(new BasicStroke(padBottom));
        g2d.fillRect(x, y+height-padBottom, width, padBottom);
    }
    if (padRight > 0) {
        g2d.setStroke(new BasicStroke(padRight));
        g2d.fillRect(x+width-padRight, y, padRight, height);
    }
    g2d.setStroke(oldStroke);
}
 
Example 15
Source File: Decoration.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void drawTextAndEmbellishments(Label label,
                                       Graphics2D g2d,
                                       float x,
                                       float y) {

    label.handleDraw(g2d, x, y);

    if (!strikethrough && stdUnderline == null && imUnderline == null) {
        return;
    }

    float x1 = x;
    float x2 = x1 + (float)label.getLogicalBounds().getWidth();

    CoreMetrics cm = label.getCoreMetrics();
    if (strikethrough) {
        Stroke savedStroke = g2d.getStroke();
        g2d.setStroke(new BasicStroke(cm.strikethroughThickness,
                                      BasicStroke.CAP_BUTT,
                                      BasicStroke.JOIN_MITER));
        float strikeY = y + cm.strikethroughOffset;
        g2d.draw(new Line2D.Float(x1, strikeY, x2, strikeY));
        g2d.setStroke(savedStroke);
    }

    float ulOffset = cm.underlineOffset;
    float ulThickness = cm.underlineThickness;

    if (stdUnderline != null) {
        stdUnderline.drawUnderline(g2d, ulThickness, x1, x2, y + ulOffset);
    }

    if (imUnderline != null) {
        imUnderline.drawUnderline(g2d, ulThickness, x1, x2, y + ulOffset);
    }
}
 
Example 16
Source File: EventView.java    From whyline with MIT License 5 votes vote down vote up
protected void paintSelectionBorder(Graphics2D g) {

		Stroke stroke = g.getStroke();
		g.setStroke(UI.SELECTED_STROKE);
		drawRoundBoundaries(UI.getHighlightColor(), g, UI.getRoundedness(), UI.getRoundedness());
		g.setStroke(stroke);

	}
 
Example 17
Source File: RoundedPanel.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
@Override
protected void paintComponent(Graphics g) {

	super.paintComponent(g);

	int width = getWidth();
	int height = getHeight();

	Graphics2D g2 = (Graphics2D) g;

	GraphicsUtils.setExcellentRenderingHints(g2);

	g2.setColor(Color.black);
	g2.fillRoundRect(0, 0, width, height, cornerRadius, cornerRadius);

	Stroke original = g2.getStroke();
	g2.setColor(Color.black);
	g2.setStroke(new BasicStroke(0));
	g2.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius);
	g2.setStroke(original);

}
 
Example 18
Source File: FreeForm.java    From dsworkbench with Apache License 2.0 4 votes vote down vote up
public void renderPreview(Graphics2D g2d) {
    if (points.size() < 1) {
        return;
    }
    //store properties
    Stroke sBefore = g2d.getStroke();
    Color cBefore = g2d.getColor();
    Composite coBefore = g2d.getComposite();
    Font fBefore = g2d.getFont();
    //draw
    g2d.setStroke(getStroke());
    g2d.setColor(drawColor);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, drawAlpha));

    GeneralPath p = new GeneralPath();
    Point2D.Double pp = points.get(0);
    p.moveTo(pp.x, pp.y);
    for (int i = 0; i <= points.size() - 1; i++) {
        pp = points.get(i);
        p.lineTo(pp.x, pp.y);
    }

    if (filled) {
        g2d.fill(p);
    } else {
        g2d.draw(p);
    }

    if (drawName) {
        g2d.setColor(getTextColor());
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTextAlpha()));
        g2d.setFont(fBefore.deriveFont((float) getTextSize()));
        Rectangle2D textBounds = g2d.getFontMetrics().getStringBounds(getFormName(), g2d);
        java.awt.Rectangle bounds = p.getBounds();
        g2d.drawString(getFormName(), (int) Math.rint(bounds.getX() + bounds.getWidth() / 2 - textBounds.getWidth() / 2), (int) Math.rint(bounds.getY() + bounds.getHeight() / 2 + textBounds.getHeight() / 2));
    }
    g2d.setStroke(sBefore);
    g2d.setColor(cBefore);
    g2d.setComposite(coBefore);
    g2d.setFont(fBefore);
}
 
Example 19
Source File: CrosshairOverlay.java    From buffer_bci with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws a crosshair vertically on the plot.
 *
 * @param g2  the graphics target.
 * @param dataArea  the data area.
 * @param x  the x-value in Java2D space.
 * @param crosshair  the crosshair.
 */
protected void drawVerticalCrosshair(Graphics2D g2, Rectangle2D dataArea,
        double x, Crosshair crosshair) {

    if (x >= dataArea.getMinX() && x <= dataArea.getMaxX()) {
        Line2D line = new Line2D.Double(x, dataArea.getMinY(), x,
                dataArea.getMaxY());
        Paint savedPaint = g2.getPaint();
        Stroke savedStroke = g2.getStroke();
        g2.setPaint(crosshair.getPaint());
        g2.setStroke(crosshair.getStroke());
        g2.draw(line);
        if (crosshair.isLabelVisible()) {
            String label = crosshair.getLabelGenerator().generateLabel(
                    crosshair);
            RectangleAnchor anchor = crosshair.getLabelAnchor();
            Point2D pt = calculateLabelPoint(line, anchor, 5, 5);
            float xx = (float) pt.getX();
            float yy = (float) pt.getY();
            TextAnchor alignPt = textAlignPtForLabelAnchorV(anchor);
            Shape hotspot = TextUtilities.calculateRotatedStringBounds(
                    label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
            if (!dataArea.contains(hotspot.getBounds2D())) {
                anchor = flipAnchorH(anchor);
                pt = calculateLabelPoint(line, anchor, 5, 5);
                xx = (float) pt.getX();
                yy = (float) pt.getY();
                alignPt = textAlignPtForLabelAnchorV(anchor);
                hotspot = TextUtilities.calculateRotatedStringBounds(
                       label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
            }
            g2.setPaint(crosshair.getLabelBackgroundPaint());
            g2.fill(hotspot);
            g2.setPaint(crosshair.getLabelOutlinePaint());
            g2.draw(hotspot);
            TextUtilities.drawAlignedString(label, g2, xx, yy, alignPt);
        }
        g2.setPaint(savedPaint);
        g2.setStroke(savedStroke);
    }
}
 
Example 20
Source File: MeterNeedle.java    From ccu-historian with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Draws the needle.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area.
 * @param rotate  the rotation point.
 * @param angle  the angle.
 */
public void draw(Graphics2D g2, Rectangle2D plotArea, Point2D rotate,
                 double angle) {

    Paint savePaint = g2.getColor();
    Stroke saveStroke = g2.getStroke();

    drawNeedle(g2, plotArea, rotate, Math.toRadians(angle));

    g2.setStroke(saveStroke);
    g2.setPaint(savePaint);

}