简体   繁体   English

如何使用 Java Graphics 绘制带边框的三角形

[英]How to draw a triangle with border with Java Graphics

I'm trying to draw a triangle with a border using the Graphics.drawPolygon() method我正在尝试使用Graphics.drawPolygon()方法绘制一个带边框的三角形

The triangle is properly drawn, but how can I calculate the 3 points of the border?三角形已正确绘制,但如何计算边框的 3 个点?

I already did it with a circle, but I can't seem to find a solution for triangle.我已经用圆圈做了,但我似乎无法找到三角形的解决方案。

A requirement of the instructor as that it cannot use Graphics2D .教师的要求,因为它不能使用Graphics2D

My code:我的代码:

if (xPoints != null && yPoints != null) {
    int[] nXPoints = new int[] { xPoints[0] - borderThickness, xPoints[1] - borderThickness,
            xPoints[2] - borderThickness };
    int[] nYPoints = new int[] { yPoints[0] - borderThickness, yPoints[1] - borderThickness,
            yPoints[2] - borderThickness };

    g.setColor(borderColor);
    g.fillPolygon(nXPoints, nYPoints, 3);

    g.setColor(fillColor);
    g.fillPolygon(xPoints, yPoints, 3);
}

Edit: Expected result编辑:预期结果

预期结果

Use the Graphics methods drawPolygon() to render the outline and fillPolygon() to fill its interior;使用Graphics方法drawPolygon()来渲染轮廓和fillPolygon()以填补其内部; both have the required signature, as shown here .两者都具有所需的签名,如图所示这里

图片1

Because "operations that draw the outline of a figure operate by traversing an infinitely thin path between pixels with a pixel-sized pen," cast the graphics context to Graphics2D so that you can use draw() and fill() on the corresponding Shape .因为“绘制图形轮廓的操作是通过使用像素大小的笔遍历像素之间的无限细路径来操作的”,所以将图形上下文转换为Graphics2D以便您可以在相应的Shape上使用draw()fill() This will allow you to specify the outline using setStroke() , illustrated here .这将允许你指定使用大纲setStroke()说明在这里

图像2

I need it to have a custom thickness…I also don't want to use Graphics2D .我需要它有一个自定义的厚度……我也不想使用Graphics2D

Custom thickness is not supported in the Graphics API. Graphics API支持自定义厚度。 As suggested here , the actual graphics context received by paintComponent() is an instance of Graphics2D , which does support custom stroke geometry.作为建议在这里,所收到的实际图形上下文paintComponent()是一个实例Graphics2D ,它支持自定义行程的几何形状。

The things is teacher haven't taught me Graphics2D , so I'm not supposed to use it.事情是老师没有教我Graphics2D ,所以我不应该使用它。

Then simply paint the larger triangle and then the smaller.然后简单地绘制较大的三角形,然后绘制较小的三角形。 If this isn't working, then you have an error in you calculation of the larger triangle, and you should edit your question to include a complete example .如果这不起作用,那么您在计算较大三角形时会出错,您应该编辑您的问题以包含完整示例

I'm looking for a way to do it without Graphics2D …a guy has interpreted the question properly in this comment .我正在寻找一种没有Graphics2D ……一个人在这个评论中正确地解释了这个问题。

As @Marco13 observes , you need a triangle that is larger than the original one by the borderThickness .正如@Marco13 所观察到的,您需要一个比原始三角形大borderThickness You can use AffineTransform to do the scaling.您可以使用AffineTransform进行缩放。 While Graphics can't fill() an arbitrary Shape , such as one created by AffineTransform , it can clip the rendering as required.虽然Graphics不能fill()任意Shape ,例如由AffineTransform创建的AffineTransform ,但它可以根据需要剪辑渲染。 In the example below, a unit triangle is translated and scaled to the center of an N x N panel;在下面的示例中,单位三角形被平移并缩放到 N x N 面板的中心; a copy is enlarged by delta .副本被delta放大。 Note how rendering is clipped first to the larger background figure and then to the smaller foreground figure.请注意渲染是如何首先裁剪到较大的背景图形,然后再裁剪到较小的前景图形。

图形边框

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/a/39812618/230513
 */
public class GraphicsBorder {

    private static class GraphicsPanel extends JPanel {

        private static final int N = 256;
        private static final Color FILL = new Color(0x990099);
        private static final Color BORDER = new Color(0x66FFB2);
        private final Shape fore;
        private final Shape back;

        public GraphicsPanel(Polygon polygon, int delta) {
            AffineTransform a1 = new AffineTransform();
            a1.translate(N / 2, N / 2);
            a1.scale(N / 3, N / 3);
            fore = a1.createTransformedShape(polygon);
            AffineTransform a2 = new AffineTransform();
            a2.translate(N / 2, N / 2 - delta / 3);
            a2.scale(N / 3 + delta, N / 3 + delta);
            back = a2.createTransformedShape(polygon);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(BORDER);
            g.setClip(back);
            g.fillRect(0, 0, N, N);
            g.setColor(FILL);
            g.setClip(fore);
            g.fillRect(0, 0, N, N);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(N, N);
        }
    }

    private void display() {
        JFrame f = new JFrame("GraphicsBorder");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Polygon p = new Polygon();
        p.addPoint(0, -1);
        p.addPoint(1, 1);
        p.addPoint(-1, 1);
        f.add(new GraphicsPanel(p, 16));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new GraphicsBorder()::display);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM