简体   繁体   English

使用Java Swing将点绘制为椭圆

[英]Draw points as ovals using java swing

I have a point with [x, y] coordinate, for example Point p = new Point(1, 2) . 我有一个[x, y]坐标的Point p = new Point(1, 2) ,例如Point p = new Point(1, 2) I want to visualize it. 我想形象化它。 However, I want this point to an oval, like in the picture below. 但是,我希望这一点指向椭圆形,如下图所示。

在此处输入图片说明

How can I do that using java Swing ? 如何使用java Swing做到这一点?

Determine the size of the oval you want, for example 10 px. 确定所需的椭圆形的大小,例如10 px。 Then draw the oval with the x and y points starting at the Point (x and y), minus half the size . 然后绘制从Point (x和y)开始的x和y点的椭圆形,减去size一半。 Something like: 就像是:

public class PointsPanel extends JPanel {
    List<Point> points = new ArrayList<Point>();
    int size = 10;
    ...
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Point p : points) {
            int x = p.x - size/2;
            int y = p.y - size/2;
            g.drawOval(x, y, size, size);
        }
    }
}

For more general details on painting, see Performing Custom Painting . 有关绘画的更多常规细节,请参见执行自定义绘画 Also see the Graphics API for other drawXxx and fillXxx methods. 另请参阅Graphics API,以了解其他drawXxxfillXxx方法。

From the image you posted, what you need is a graph layout library like JUNG (not sure about current status of this project). 从您发布的图像中,您需要的是一个图布局库,例如JUNG (不确定该项目的当前状态)。 You can follow peeskillet's answer but need to take care of problems like overlapping ovals etc. 您可以按照peeskillet的回答进行操作,但需要注意椭圆形重叠等问题。

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

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