如何随机显示画线的距离?

库尔特16

我正在尝试绘制以随机位置为中心的实心圆,并用一条直线连接这些圆。到中心的距离会显示在直线上,并且每当您调整框的大小时,圆都会重新显示在新的随机位置。我陷入了如何显示距离的问题?任何帮助表示赞赏,并提前。这是代码(我设法做到):

public class Test extends JFrame {

    public Test() {
        add(new LineConnectingTheTwoCircles());
    }

    // Panel class
    class LineConnectingTheTwoCircles extends JPanel {
        //Default constructor
        LineConnectingTheTwoCircles() {
        }
        /* Override paintComponent (getting access to the panel's Graphics
           class) */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);    
            int radius = 15;
            // getting coordinates of circle 1
            int x1 = (int) (Math.random() * (getWidth()));
            int y1 = (int) (Math.random() * (getHeight()));
            // getting coordinates of circle 2
            int x2 = (int) (Math.random() * (getWidth()));
            int y2 = (int) (Math.random() * (getWidth()));
            // Setting color and drawing a filled circles (1 & 2)
            g.setColor(Color.BLUE);
            g.fillOval(x1 - radius, y1 - radius, 2 * radius, 2 * radius);
            g.drawString("1", x1 - 25, y1);
            g.setColor(Color.RED);
            g.fillOval(x2 - radius, y2 - radius, 2 * radius, 2 * radius);
            g.drawString("2", x2 - 25, y2);    
            connectingTheTwoCircles(g, x1, y1, x2, y2);
        }

        // Connecting the two circles from the center
        private void connectingTheTwoCircles(Graphics g, int x1, int y1,
                int x2, int y2) {
            //Distance between the circles centered
            double D = Math.sqrt((Math.pow((y2 - y1), 2))
                    + (Math.pow((x2 - x1), 2)));
            //Getting the coordinates for the line l
            int x11 = x1;
            int y11 = y1;
            int x21 = x2;
            int y21 = y2;
            g.setColor(Color.BLACK);
            g.drawLine(x11, y11, x21, y21);                         
        }

        public double getDistance(double x1, double y1, double x2, double y2) {
            return Math.sqrt((Math.pow((y2 - y1), 2))
                    + (Math.pow((x2 - x1), 2)));
        }
    }

    public static void main(String[] args) {
        // Frame declaration
        Test frame = new Test();    
        /*
         * Invoking some methods, to set a title on the title bar, to specifier
         * the size of the frame to centre it on the screen, to tell the program
         * to terminate when the frame is closed and finally to display it
         */
        frame.setTitle("This is a test");
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);    
    }
}
alex2410

尝试下一个代码在线条中心绘制距离

  double distance = getDistance(x11, y11, x21, y21);
  g.drawString(distance+" ",
                x11> x21 ? x21 + (x11-x21)/2 : x11 + (x21 - x11)/2 ,
                y11> y21 ? y21 + (y11-y21)/2 : y11 + (y21 - y11)/2 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用随机距离矩阵构建图?

来自分类Dev

如何从距离和角度获取随机点?

来自分类Dev

如何随机显示片段?

来自分类Dev

如何随机显示片段?

来自分类Dev

如何随机显示注释

来自分类Dev

如何随机显示数组?

来自分类Dev

如何显示Pymol中残基之间的距离

来自分类Dev

如何显示Pymol中残基之间的距离

来自分类Dev

MatLab:如何随机显示图片?

来自分类Dev

如何在圆之间画线?

来自分类Dev

如何在画布上画线?

来自分类Dev

如何在画布上画线

来自分类Dev

在Opengl中画线不显示C ++

来自分类Dev

如何显示标记,距离地图有一定距离

来自分类Dev

如何显示CloudKit中距离今天最近的帖子?

来自分类Dev

如何在 MeshLab 中显示 Hausdorff 距离的热图?

来自分类Dev

如何在随机屏幕位置显示按钮

来自分类Dev

如何选择随机5 TR并显示给用户?

来自分类Dev

如何随机显示动态壁纸中的图像

来自分类Dev

如何显示随机启动画面(Android)

来自分类Dev

如何在PHP中显示随机结果

来自分类Dev

如何显示随机的东西而不重叠

来自分类Dev

如何让类别以随机顺序显示帖子 | WordPress的

来自分类Dev

如何在y轴上随机放置对象,并使每个对象之间的距离始终大于特定值?

来自分类Dev

随机显示

来自分类Dev

显示Levenshtein距离的结果

来自分类Dev

如何在图像上画线/画图?

来自分类Dev

如何在OpenCV Camera Preview上画线?

来自分类Dev

甜菜两线如何画线成圈?