Search in sources :

Example 11 with Polygon

use of java.awt.Polygon in project JMRI by JMRI.

the class AnalogClock2Display method paint.

@Override
public void paint(Graphics g) {
    // overridden Paint method to draw the clock
    g.setColor(color.getValue());
    g.translate(centreX, centreY);
    // Draw the clock face
    g.drawImage(clockFace, -faceSize / 2, -faceSize / 2, faceSize, faceSize, this);
    // Draw the JMRI logo
    g.drawImage(scaledLogo, -logoWidth / 2, -faceSize / 4, logoWidth, logoHeight, this);
    // Draw hour hand rotated to appropriate angle
    // Calculation mimics the AffineTransform class calculations in Graphics2D
    // Grpahics2D and AffineTransform not used to maintain compatabilty with Java 1.1.8
    double minuteAngleRadians = Math.toRadians(minuteAngle);
    for (int i = 0; i < scaledMinuteX.length; i++) {
        rotatedMinuteX[i] = (int) (scaledMinuteX[i] * Math.cos(minuteAngleRadians) - scaledMinuteY[i] * Math.sin(minuteAngleRadians));
        rotatedMinuteY[i] = (int) (scaledMinuteX[i] * Math.sin(minuteAngleRadians) + scaledMinuteY[i] * Math.cos(minuteAngleRadians));
    }
    scaledMinuteHand = new Polygon(rotatedMinuteX, rotatedMinuteY, rotatedMinuteX.length);
    double hourAngleRadians = Math.toRadians(hourAngle);
    for (int i = 0; i < scaledHourX.length; i++) {
        rotatedHourX[i] = (int) (scaledHourX[i] * Math.cos(hourAngleRadians) - scaledHourY[i] * Math.sin(hourAngleRadians));
        rotatedHourY[i] = (int) (scaledHourX[i] * Math.sin(hourAngleRadians) + scaledHourY[i] * Math.cos(hourAngleRadians));
    }
    scaledHourHand = new Polygon(rotatedHourX, rotatedHourY, rotatedHourX.length);
    g.fillPolygon(scaledHourHand);
    g.fillPolygon(scaledMinuteHand);
    // Draw AM/PM indicator in slightly smaller font than hour digits
    int amPmFontSize = (int) (faceSize * .075);
    if (amPmFontSize < 1) {
        amPmFontSize = 1;
    }
    Font amPmSizedFont = new Font("Serif", Font.BOLD, amPmFontSize);
    g.setFont(amPmSizedFont);
    FontMetrics amPmFontM = g.getFontMetrics(amPmSizedFont);
    g.drawString(amPm, -amPmFontM.stringWidth(amPm) / 2, faceSize / 5);
}
Also used : FontMetrics(java.awt.FontMetrics) Polygon(java.awt.Polygon) Font(java.awt.Font)

Example 12 with Polygon

use of java.awt.Polygon in project JMRI by JMRI.

the class SpeedoDial method scaleFace.

// Method called on resizing event - sets various sizing variables
// based on the size of the resized panel and scales the logo/hands
public void scaleFace() {
    int panelHeight = this.getSize().height;
    int panelWidth = this.getSize().width;
    size = Math.min(panelHeight, panelWidth);
    faceSize = (int) (size * .97);
    if (faceSize == 0) {
        faceSize = 1;
    }
    // Had trouble getting the proper sizes when using Images by themselves so
    // use the NamedIcon as a source for the sizes
    int logoScaleWidth = faceSize / 6;
    int logoScaleHeight = (int) ((float) logoScaleWidth * (float) jmriIcon.getIconHeight() / jmriIcon.getIconWidth());
    scaledLogo = logo.getScaledInstance(logoScaleWidth, logoScaleHeight, Image.SCALE_SMOOTH);
    scaledIcon.setImage(scaledLogo);
    logoWidth = scaledIcon.getIconWidth();
    logoHeight = scaledIcon.getIconHeight();
    scaleRatio = faceSize / 2.7F / minuteHeight;
    for (int i = 0; i < minuteX.length; i++) {
        scaledMinuteX[i] = (int) (minuteX[i] * scaleRatio);
        scaledMinuteY[i] = (int) (minuteY[i] * scaleRatio);
    }
    scaledMinuteHand = new Polygon(scaledMinuteX, scaledMinuteY, scaledMinuteX.length);
    centreX = panelWidth / 2;
    centreY = panelHeight / 2;
    return;
}
Also used : Polygon(java.awt.Polygon)

Example 13 with Polygon

use of java.awt.Polygon in project JMRI by JMRI.

the class SpeedoDial method paint.

@Override
public void paint(Graphics g) {
    super.paint(g);
    if (!(g instanceof Graphics2D)) {
        throw new IllegalArgumentException("Graphics object passed is not the correct type");
    }
    Graphics2D g2 = (Graphics2D) g;
    // overridden Paint method to draw the speedo dial
    g2.translate(centreX, centreY);
    // Draw the dial outline scaled to the panel size with a dot in the middle and
    // center ring for the pointer
    g2.setColor(Color.white);
    g2.fillOval(-faceSize / 2, -faceSize / 2, faceSize, faceSize);
    g2.setColor(Color.black);
    g2.drawOval(-faceSize / 2, -faceSize / 2, faceSize, faceSize);
    int dotSize = faceSize / 40;
    g2.fillOval(-dotSize * 2, -dotSize * 2, 4 * dotSize, 4 * dotSize);
    // Draw the JMRI logo
    g2.drawImage(scaledLogo, -logoWidth / 2, -faceSize / 4, logoWidth, logoHeight, this);
    // Currently selected units are plotted every 10 units with major and minor
    // tick marks around the outer edge of the dial
    // Other units are plotted in a differrent color, smaller font with dots
    // in an inner ring
    // Scaled font size for primary units
    int fontSize = faceSize / 10;
    if (fontSize < 1) {
        fontSize = 1;
    }
    Font sizedFont = new Font("Serif", Font.PLAIN, fontSize);
    g2.setFont(sizedFont);
    FontMetrics fontM = g2.getFontMetrics(sizedFont);
    // Draw the speed markers for the primary units
    int dashSize = size / 60;
    setTicks();
    // Add minor tick marks
    for (float i = 150; i < 391; i = i + priMinorTick) {
        g2.drawLine(dotX((float) faceSize / 2, i), dotY((float) faceSize / 2, i), dotX((float) faceSize / 2 - dashSize, i), dotY((float) faceSize / 2 - dashSize, i));
    }
    // Add major tick marks and digits
    int j = 0;
    for (float i = 150; i < 391; i = i + priMajorTick) {
        g2.drawLine(dotX((float) faceSize / 2, i), dotY((float) faceSize / 2, i), dotX((float) faceSize / 2 - 3 * dashSize, i), dotY((float) faceSize / 2 - 3 * dashSize, i));
        String speed = Integer.toString(10 * j);
        int xOffset = fontM.stringWidth(speed);
        int yOffset = fontM.getHeight();
        // offset by 210 degrees to start in lower left quadrant and work clockwise
        g2.drawString(speed, dotX((float) faceSize / 2 - 6 * dashSize, j * priMajorTick - 210) - xOffset / 2, dotY((float) faceSize / 2 - 6 * dashSize, j * priMajorTick - 210) + yOffset / 4);
        j++;
    }
    // Add dots and digits for secondary units
    // First make a smaller font
    fontSize = faceSize / 15;
    if (fontSize < 1) {
        fontSize = 1;
    }
    sizedFont = new Font("Serif", Font.PLAIN, fontSize);
    g2.setFont(sizedFont);
    fontM = g2.getFontMetrics(sizedFont);
    g2.setColor(Color.green);
    j = 0;
    for (float i = 150; i < 391; i = i + secTick) {
        g2.fillOval(dotX((float) faceSize / 2 - 10 * dashSize, i), dotY((float) faceSize / 2 - 10 * dashSize, i), 5, 5);
        if (((j & 1) == 0) || (units == Speed.KPH)) {
            // kph are plotted every 20 when secondary, mph every 10
            String speed = Integer.toString(10 * j);
            int xOffset = fontM.stringWidth(speed);
            int yOffset = fontM.getHeight();
            // offset by 210 degrees to start in lower left quadrant and work clockwise
            g2.drawString(speed, dotX((float) faceSize / 2 - 13 * dashSize, j * secTick - 210) - xOffset / 2, dotY((float) faceSize / 2 - 13 * dashSize, j * secTick - 210) + yOffset / 4);
        }
        j++;
    }
    // Draw secondary units string
    g2.drawString(secString, dotX((float) faceSize / 2 - 5 * dashSize, 45) - fontM.stringWidth(secString) / 2, dotY((float) faceSize / 2 - 5 * dashSize, 45) + fontM.getHeight() / 4);
    g2.setColor(Color.black);
    // Draw pointer rotated to appropriate angle
    // Calculation mimics the AffineTransform class calculations in Graphics2D
    // Graphics2D and AffineTransform not used to maintain compatabilty with Java 1.1.8
    double speedAngleRadians = Math.toRadians(speedAngle);
    for (int i = 0; i < scaledMinuteX.length; i++) {
        rotatedMinuteX[i] = (int) (scaledMinuteX[i] * Math.cos(speedAngleRadians) - scaledMinuteY[i] * Math.sin(speedAngleRadians));
        rotatedMinuteY[i] = (int) (scaledMinuteX[i] * Math.sin(speedAngleRadians) + scaledMinuteY[i] * Math.cos(speedAngleRadians));
    }
    scaledMinuteHand = new Polygon(rotatedMinuteX, rotatedMinuteY, rotatedMinuteX.length);
    g2.fillPolygon(scaledMinuteHand);
    // Draw primary units indicator in slightly smaller font than speed digits
    int unitsFontSize = (int) ((float) faceSize / 10 * .75);
    if (unitsFontSize < 1) {
        unitsFontSize = 1;
    }
    Font unitsSizedFont = new Font("Serif", Font.PLAIN, unitsFontSize);
    g2.setFont(unitsSizedFont);
    FontMetrics unitsFontM = g2.getFontMetrics(unitsSizedFont);
    //        g2.drawString(unitsString, -amPmFontM.stringWidth(unitsString)/2, faceSize/5 );
    g2.drawString(priString, dotX((float) faceSize / 2 - 5 * dashSize, -225) - unitsFontM.stringWidth(priString) / 2, dotY((float) faceSize / 2 - 5 * dashSize, -225) + unitsFontM.getHeight() / 4);
    // Show numeric speed
    String speedString = Integer.toString(speedDigits);
    int digitsFontSize = (int) (fontSize * 1.5);
    Font digitsSizedFont = new Font("Serif", Font.PLAIN, digitsFontSize);
    g2.setFont(digitsSizedFont);
    FontMetrics digitsFontM = g2.getFontMetrics(digitsSizedFont);
    // draw a box around the digital speed
    int pad = (int) (digitsFontSize * 0.2);
    int h = (int) (digitsFontM.getAscent() * 0.8);
    int w = digitsFontM.stringWidth("999");
    if (pad < 2) {
        pad = 2;
    }
    g2.setColor(Color.LIGHT_GRAY);
    g2.fillRect(-w / 2 - pad, 2 * faceSize / 5 - h - pad, w + pad * 2, h + pad * 2);
    g2.setColor(Color.DARK_GRAY);
    g2.drawRect(-w / 2 - pad, 2 * faceSize / 5 - h - pad, w + pad * 2, h + pad * 2);
    g2.setColor(Color.BLACK);
    g2.drawString(speedString, -digitsFontM.stringWidth(speedString) / 2, 2 * faceSize / 5);
}
Also used : FontMetrics(java.awt.FontMetrics) Polygon(java.awt.Polygon) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 14 with Polygon

use of java.awt.Polygon in project jdk8u_jdk by JetBrains.

the class CSSBorder method paintBorder.

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    if (!(g instanceof Graphics2D)) {
        return;
    }
    Graphics2D g2 = (Graphics2D) g.create();
    int[] widths = getWidths();
    // Position and size of the border interior.
    int intX = x + widths[LEFT];
    int intY = y + widths[TOP];
    int intWidth = width - (widths[RIGHT] + widths[LEFT]);
    int intHeight = height - (widths[TOP] + widths[BOTTOM]);
    // Coordinates of the interior corners, from NW clockwise.
    int[][] intCorners = { { intX, intY }, { intX + intWidth, intY }, { intX + intWidth, intY + intHeight }, { intX, intY + intHeight } };
    // Draw the borders for all sides.
    for (int i = 0; i < 4; i++) {
        Value style = getBorderStyle(i);
        Polygon shape = getBorderShape(i);
        if ((style != Value.NONE) && (shape != null)) {
            int sideLength = (i % 2 == 0 ? intWidth : intHeight);
            // "stretch" the border shape by the interior area dimension
            shape.xpoints[2] += sideLength;
            shape.xpoints[3] += sideLength;
            Color color = getBorderColor(i);
            BorderPainter painter = getBorderPainter(i);
            double angle = i * Math.PI / 2;
            // Restore initial clip
            g2.setClip(g.getClip());
            g2.translate(intCorners[i][0], intCorners[i][1]);
            g2.rotate(angle);
            g2.clip(shape);
            painter.paint(shape, g2, color, i);
            g2.rotate(-angle);
            g2.translate(-intCorners[i][0], -intCorners[i][1]);
        }
    }
    g2.dispose();
}
Also used : Color(java.awt.Color) CssValue(javax.swing.text.html.CSS.CssValue) LengthValue(javax.swing.text.html.CSS.LengthValue) BorderWidthValue(javax.swing.text.html.CSS.BorderWidthValue) Value(javax.swing.text.html.CSS.Value) ColorValue(javax.swing.text.html.CSS.ColorValue) Polygon(java.awt.Polygon) Graphics2D(java.awt.Graphics2D)

Example 15 with Polygon

use of java.awt.Polygon in project jdk8u_jdk by JetBrains.

the class CSSBorder method getBorderShape.

/**
     * Return border shape for {@code side} as if the border has zero interior
     * length.  Shape start is at (0,0); points are added clockwise.
     */
private Polygon getBorderShape(int side) {
    Polygon shape = null;
    int[] widths = getWidths();
    if (widths[side] != 0) {
        shape = new Polygon(new int[4], new int[4], 0);
        shape.addPoint(0, 0);
        shape.addPoint(-widths[(side + 3) % 4], -widths[side]);
        shape.addPoint(widths[(side + 1) % 4], -widths[side]);
        shape.addPoint(0, 0);
    }
    return shape;
}
Also used : Polygon(java.awt.Polygon)

Aggregations

Polygon (java.awt.Polygon)39 Point (java.awt.Point)15 Graphics2D (java.awt.Graphics2D)11 Color (java.awt.Color)6 Rectangle (java.awt.Rectangle)6 GridPoint (doc.GridPoint)5 BasicStroke (java.awt.BasicStroke)5 Font (java.awt.Font)5 Paint (java.awt.Paint)5 FontMetrics (java.awt.FontMetrics)4 AffineTransform (java.awt.geom.AffineTransform)4 GradientPaint (java.awt.GradientPaint)2 Shape (java.awt.Shape)2 ShapeSpanIterator (sun.java2d.pipe.ShapeSpanIterator)2 BasePoint (gdsc.core.match.BasePoint)1 PolygonRoi (ij.gui.PolygonRoi)1 Roi (ij.gui.Roi)1 AlphaComposite (java.awt.AlphaComposite)1 Component (java.awt.Component)1 Dimension (java.awt.Dimension)1