在 Java 中渲染矢量形状

发布于 2024-08-07 18:26:54 字数 146 浏览 6 评论 0原文

我有一系列形状(大约 50 个),每个形状有 5 个点和一种颜色(具有 alpha 透明度)。我想将这些形状渲染到像素网格上。

我是一名业余编程人员,所以我不知道应该如何去做。

有人可以给我一个起点,或者一些伪代码吗?

提前致谢。

I have a series of shapes (Approx. 50), that each have 5 points, and a color (with alpha transparency). I want to render these shapes onto a pixel grid.

I program as an amateur, so I have no idea how I should go about doing this.

Can someone give me a starting point, or some pseudo-code?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坦然微笑 2024-08-14 18:26:54
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.List;
import java.util.ArrayList;
import java.awt.Point;
import javax.swing.JFrame;
import java.awt.Color;
import java.util.Random;
import java.awt.Polygon;
import java.awt.Shape;


public class GraphicsTest extends JFrame {

    private List<ColoredShape> shapes;
    private static final int NUM_SHAPES = 50;
    private static final int NUM_POINTS_PER_SHAPE = 5;

    private static final int WIDTH = 640;
    private static final int HEIGHT = 480;

    private Random randomGenerator;

    public GraphicsTest(String title) {
        super(title);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);


        randomGenerator = new Random();

        initShapes();
    }

    private void initShapes() {
        shapes = new ArrayList<ColoredShape>(NUM_SHAPES);
        for (int i = 0; i < NUM_SHAPES; i++) {
            Point[] points = getRandomPoints();
            Color color = getRandomColor();

            shapes.add(i, new ColoredShape(points, color));
        }

    }


    private Point[] getRandomPoints() {
        Point[] points = new Point[NUM_POINTS_PER_SHAPE];
        for (int i = 0; i < points.length; i++) {
            int x = randomGenerator.nextInt(WIDTH);
            int y = randomGenerator.nextInt(HEIGHT);
            points[i] = new Point(x, y);
        }
        return points;
    }

    /**
    * @return a Color with random values for alpha, red, green, and blue values
    */
    private Color getRandomColor() {
        float alpha = randomGenerator.nextFloat();
        float red = randomGenerator.nextFloat();
        float green = randomGenerator.nextFloat();
        float blue = randomGenerator.nextFloat();

        return new Color(red, green, blue, alpha);
    }


    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        for (ColoredShape shape : shapes) {
            g2.setColor(shape.getColor());
            g2.fill(shape.getOutline());
        }
    }


    public static void main(String[] args) {
        GraphicsTest b = new GraphicsTest("Testing polygons");
    }


    private class ColoredShape {

        private Polygon outline;
        private Color color;

        public ColoredShape(Point[] points, Color color) {
            this.color = color;
            // Would be better to separate out into xpoints, ypoints, npoints
            // but I'm lazy
            outline = new Polygon();
            for (Point p : points) {
                outline.addPoint((int) p.getX(), (int) p.getY());
            }
        }

        public Color getColor() {
            return color;
        }

        public Shape getOutline() { return outline; }

    }


}

输出我的机器

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.List;
import java.util.ArrayList;
import java.awt.Point;
import javax.swing.JFrame;
import java.awt.Color;
import java.util.Random;
import java.awt.Polygon;
import java.awt.Shape;


public class GraphicsTest extends JFrame {

    private List<ColoredShape> shapes;
    private static final int NUM_SHAPES = 50;
    private static final int NUM_POINTS_PER_SHAPE = 5;

    private static final int WIDTH = 640;
    private static final int HEIGHT = 480;

    private Random randomGenerator;

    public GraphicsTest(String title) {
        super(title);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);


        randomGenerator = new Random();

        initShapes();
    }

    private void initShapes() {
        shapes = new ArrayList<ColoredShape>(NUM_SHAPES);
        for (int i = 0; i < NUM_SHAPES; i++) {
            Point[] points = getRandomPoints();
            Color color = getRandomColor();

            shapes.add(i, new ColoredShape(points, color));
        }

    }


    private Point[] getRandomPoints() {
        Point[] points = new Point[NUM_POINTS_PER_SHAPE];
        for (int i = 0; i < points.length; i++) {
            int x = randomGenerator.nextInt(WIDTH);
            int y = randomGenerator.nextInt(HEIGHT);
            points[i] = new Point(x, y);
        }
        return points;
    }

    /**
    * @return a Color with random values for alpha, red, green, and blue values
    */
    private Color getRandomColor() {
        float alpha = randomGenerator.nextFloat();
        float red = randomGenerator.nextFloat();
        float green = randomGenerator.nextFloat();
        float blue = randomGenerator.nextFloat();

        return new Color(red, green, blue, alpha);
    }


    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        for (ColoredShape shape : shapes) {
            g2.setColor(shape.getColor());
            g2.fill(shape.getOutline());
        }
    }


    public static void main(String[] args) {
        GraphicsTest b = new GraphicsTest("Testing polygons");
    }


    private class ColoredShape {

        private Polygon outline;
        private Color color;

        public ColoredShape(Point[] points, Color color) {
            this.color = color;
            // Would be better to separate out into xpoints, ypoints, npoints
            // but I'm lazy
            outline = new Polygon();
            for (Point p : points) {
                outline.addPoint((int) p.getX(), (int) p.getY());
            }
        }

        public Color getColor() {
            return color;
        }

        public Shape getOutline() { return outline; }

    }


}

Output on my machine

混吃等死 2024-08-14 18:26:54

Java 包含一个非常好的 2d 图形 API。我建议你看看 http://java.sun.com/ docs/books/tutorial/2d/index.html 有关基础知识的教程。

Java has a pretty nice 2d graphics API included with it. I suggest you take a look at http://java.sun.com/docs/books/tutorial/2d/index.html for a tutorial on the basics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文