如何在Java中绘制二维图形?

3

我有两个不同的列表。每个列表都包含x和y值对(它们具有正数和负数值)。如何在二维坐标轴上绘制它们?我想为每个值放置,第一个列表的点是蓝色的,第二个列表的点是红色的。

我的列表类型:

List<List<Double>>

List<Double> inside of List<...> has 2 variables, first of it for x value and the second one is for y value.

然而,我只需要知道如何在Java(桌面应用程序)中绘制二维图形并在任意位置放置点,改进变量的代码不那么重要。
PS:
我想要更加简单的那种图形,类似于:
3个回答

4

1
假设您正在使用带有面板的Swing,您可以使用以下内容:
public class JImagePanelExample extends JPanel {

    private BufferedImage image;
    private Graphics2D drawingBoard;
    private int x, y; // Image position in the panel

    // Let's assume image is a chart and you need to draw lines
    public JImagePanelExample(BufferedImage image, int x, int y) {

        super();
        this.image = image;

        // Retrieving a mean to draw lines
        drawingBoard = image.createGraphics();

        // Draw what you need to draw (see other methods too)
        drawingBoard.drawLine(0, 10, 35, 55);

    }

    // Called by Swing to draw the image in the panel
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, x, y, null);
    }

}

如果您不想使用Swing,只需要进行2D绘制,请专注于BufferedImageGraphics2D


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接