简体   繁体   English

在圆心画一条线并连接另外三个圆形成一个正方形

[英]draw line at center of circle and join another three circle to form a square

I'm trying to draw line from center of one circle to another placed at distance of 50.我试图从一个圆的中心画一条线到另一个距离为 50 的圆。

Also how to determine that four circle at the four corner is joined by lines and form a square where we can write text.还有如何确定四个角的四个圆由线连接起来形成一个可以写文字的正方形。

Similar diagram I would like to have.我想要的类似图。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class MousePanel extends JPanel implements MouseListener,ActionListener{

    private int a,b,a2,b2;
    private int count=0;
public MousePanel(){
    super();
    addMouseListener(this);   
}

public void paint(Graphics g){


    for(int x=5;x<=1000; x=x+50){
        for(int y=5; y<=1000;y=y+50){
    g.drawOval(x,y,15,15);
            System.out.println(x+" "+y);
        }

    }
    if(count==2){
        if(a2>a){
        g.drawLine(a, b, a+50, b);
        }
        else{
            g.drawLine(a, b, a, b+50);
        }
    count=0;
    a=0;
    b=0;
    a2=0;
    b2=0;
    }
}

public void mouseClicked(MouseEvent mouse){   

    count++;

    if(count==1){
        a=mouse.getX();

        b=mouse.getY();

        System.out.println(a+" "+b);
    }

    if(count==2){
        a2 = mouse.getX();
        b2 = mouse.getY();
         System.out.println(a2+" "+b2);
    }

    repaint();
    }

public void mouseEntered(MouseEvent mouse){ }   
public void mouseExited(MouseEvent mouse){ }
public void mousePressed(MouseEvent mouse){ }
public void mouseReleased(MouseEvent mouse){ }

public static void main(String arg[]){
    JFrame frame = new JFrame("MousePanel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(640,640);

    MousePanel panel = new MousePanel();
    frame.setContentPane(panel);
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}
}

This is my code.这是我的代码。 Here I have just made the coicle.在这里,我刚刚制作了卷曲。

I don't know how to answer your first question, but I may help you with your second.我不知道如何回答你的第一个问题,但我可以帮你解决第二个问题。 To determine if the centers of four circles are the corners of a square, the corners must be a certain distance from each other.要确定四个圆的中心是否是正方形的角,角之间必须有一定的距离。 First, select a corner.首先,选择一个角。 Then measure the distance the other points are away from the selected one.然后测量其他点与选定点的距离。 Assuming the square has a length of X, two of the distances will be X and one of them will be the sqrt(2)*X.假设正方形的长度为 X,其中两个距离为 X,其中一个为 sqrt(2)*X。

To determine the distance between two points, you use pythagorean theorem.要确定两点之间的距离,请使用勾股定理。

xDistance = point1.x - point2.x;
yDistance = point1.y - point2.y;
distance = sqrt(xDistance*xDistance + yDistance*yDistance);

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

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