java怎么生成距离相等的点 谢谢大家。
比如
  1. 2. 3
    4. 5. 6
      7. 8. 9
    1. 2. 3
  4. 5. 6
大约意思就是这个, 就是1,2,4三点距离相等
2,4,5三点距离也相等 
 有人能给个代码吗?

解决方案 »

  1.   

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.util.Timer;
    import java.util.TimerTask;import javax.swing.JFrame;
    import javax.swing.JPanel;public class Frame5 extends JFrame {

    private Point2D[] points;
    private MyPanel pnlMain;
    private Timer timer;
    private TimerTask task;
    private Point pos;
    private int timeout;

    public Frame5() {
    initialize();
    }

    private void initialize() {
    points = new Point2D[15];
    pos = new Point(0,0);
    timeout = 0;
    //生成传说中的貌似等距离点
    for (int i=0;i<3;i++) {
    for (int j=0;j<3;j++) {
    points[i*3+j] = new Point2D(j*2+i, i*1.7320508076);
    }
    }
    for (int i=3;i<5;i++) {
    for (int j=0;j<3;j++) {
    points[i*3+j] = new Point2D(j*2+(4-i), i*1.7320508076);
    }
    }
    for (Point2D p:points) {
    System.out.println(p.toString());
    }
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 300);
    this.setLocationRelativeTo(null);
    Container cont = this.getContentPane();
    pnlMain = new MyPanel();
    cont.add(pnlMain);
    timer = new Timer();
    task = new TimerTask() {
    public void run() {
    timeout++;
    pos.x++;
    pnlMain.repaint();
    if (timeout > 200) {
    timer.cancel();
    }
    }
    };
    timer.schedule(task, 0, 100);
    }

    public static void main(String[] args) {
    Frame5 frame = new Frame5();
    frame.setVisible(true);
    }

    class MyPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.GREEN);
    for (Point2D point : points) {
    g.fillRect((int)Math.round(point.x*10)+pos.x, 
    (int)Math.round(point.y*10)+pos.y, 3, 3);
    }
    }
    }
    }我又来了,楼主看下吧,
    这个程序要配合你之前那个程序的Point2D类才行
      

  2.   

    好人  感动ing
    怎么个配合我之前的程序,其实我以前的那些绿色的点点不都是圆形的吗?
    就是现在想改成这种等距离点出发的。
    后面的不改,还有就是我想在画面上加个文本框,让ship[0]的角度可以变化。
      

  3.   

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.Timer;
    import java.util.TimerTask;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class Frame5 extends JFrame{
    private static final long serialVersionUID = 1L;
    /** Simulation Basic Data **/
    private int maximumVESSELS = 16;
    private int simFIN = 100;// int simFIN = 11521 ; /** 8 Days **/
    private double dtMIN = 1.0; /** minute **/
    private double pi = 3.14159265358979323846;
    private double degToRAD = pi / 180.0;
    private ShipCondition[] shipSET;
    private Timer timer;
    private TimerTask task;
    private int clock;
    private MyPanel pnlMain;
    private JButton btnInterrupt;
    private boolean movable = false;

    public Frame5() {
    initialize();
    }

    private void initialize() {
    clock = 0;
    shipSET = new ShipCondition[maximumVESSELS];
    for( int i=0;i<maximumVESSELS;i++){
    shipSET[i] = new ShipCondition() ;
    }
    for( int is = 0 ; is < maximumVESSELS ; is++ ){
    shipSET[is].vesselID = is ;
    shipSET[is].metaWeightGOAL = 10.0 ;
    shipSET[is].goalSigA = 5.0 ;  
    shipSET[is].goalSigB = 1.0 ;  
    shipSET[is].metaWeightCONTRACT = 1.0 ;
    shipSET[is].contractSigA = 10.0 ;
    shipSET[is].contractSigB = 1.0 ;
    shipSET[is].metaWeightEXPAND = 1.0 ;
    shipSET[is].expandSigA = -10.0 ;
    shipSET[is].expandSigB = 1.0 ;
    shipSET[is].metaWeightClusterAVOID = 100.0 ;
    shipSET[is].clusterAvoidSigA = -10.0 ;
    shipSET[is].clusterAvoidSigB = 3.0 ;
    shipSET[is].decisionMD = 6.0 ;
    shipSET[is].dcpaJUDGE = 0.75 ;
    shipSET[is].headOnDEG = 50.0 ;
    shipSET[is].courseRadOLD = pi/2 ; /* Dummy */
    shipSET[is].speedKtOLD = 15.0 ;   
    shipSET[is].courseRadNOW = pi/2 ; /* Dummy */
    shipSET[is].speedKtNOW = 15.0 ;
    shipSET[is].courseNaviRAD = pi/2 ; /* Dummy */
    shipSET[is].speedNaviKT = 15.0 ;
    shipSET[is]. goalRad=pi/2;
    }
    double shipRad=360.0/((double)maximumVESSELS-1)*degToRAD;
    shipSET[0].xMileOLD = 0;
    shipSET[0].yMileOLD = 3.464;
    shipSET[0].xMileNOW = 0;
    shipSET[0].yMileNOW = 3.464;
    for (int i=0;i<3;i++) {
    for (int j=0;j<3;j++) {
    shipSET[i*3+j+1].xMileOLD = j*2+i;
    shipSET[i*3+j+1].yMileOLD = i*1.7320508076;
    shipSET[i*3+j+1].xMileNOW = shipSET[i*3+j+1].xMileOLD;
    shipSET[i*3+j+1].yMileNOW = shipSET[i*3+j+1].yMileOLD;
    }
    }
    for (int i=3;i<5;i++) {
    for (int j=0;j<3;j++) {
    shipSET[i*3+j+1].xMileOLD = j*2+(4-i);
    shipSET[i*3+j+1].yMileOLD = i*1.7320508076;
    shipSET[i*3+j+1].xMileNOW = shipSET[i*3+j+1].xMileOLD;
    shipSET[i*3+j+1].yMileNOW = shipSET[i*3+j+1].yMileOLD;
    }
    }
    // for(int i=1;i<maximumVESSELS;i++){
    // shipSET[i].xMileOLD = shipSET[0].xMileOLD+2.0*Math.cos(shipRad*(double)i);
    // shipSET[i].yMileOLD = shipSET[0].yMileOLD+2.0*Math.sin(shipRad*(double)i);
    // shipSET[i].xMileNOW = shipSET[0].xMileNOW+2.0*Math.cos(shipRad*(double)i);
    // shipSET[i].yMileNOW = shipSET[0].yMileNOW+2.0*Math.sin(shipRad*(double)i);
    // }
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 300);
    Container cont = this.getContentPane();
    cont.setLayout(new BorderLayout());
    pnlMain = new MyPanel();
    btnInterrupt = new JButton("中断");
    btnInterrupt.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    movable = true;
    pnlMain.requestFocus();
    }
    });
    cont.add(btnInterrupt, BorderLayout.NORTH);
    cont.add(pnlMain, BorderLayout.CENTER);
    }

    public void transform() {
    // Random rand = new Random(2146351065) ;
    timer = new Timer();
    task = new TimerTask() {
    public void run() {
    if (clock >= simFIN) {
    timer.cancel();
    return;
    }
    if(clock>=20&&clock<25) 
    shipSET[0].goalRad=pi/3;
    if(clock>=25&&clock<70)
    shipSET[0].goalRad=pi/2;
    if(clock>=70&&clock<75)
    shipSET[0].goalRad=pi;
    if(clock>=75)
    shipSET[0].goalRad=pi/2;

    for( int givenSHIP= 0 ; givenSHIP < maximumVESSELS ; givenSHIP ++ ){
    /*** Navigation -> Navigation (status = 0 --> 0) ***/
    /********************************************************/
    /***** Ship Cluster Behavior Model Calculation *****/
    /********************************************************/
    if (movable && givenSHIP == 0) {
    continue;
    }
    DecisionMakingReturn dmCoSp = 
    DecisionMakingBody.agentDecisionMaking(shipSET, givenSHIP, dtMIN);

    /*** Quick Response ***/
    shipSET[givenSHIP].xMileNEW = 
    shipSET[givenSHIP].xMileNOW + dmCoSp.dmSpeedKt 
    * dtMIN / 60.0 * Math.sin( dmCoSp.dmCourseRad );
    shipSET[givenSHIP].yMileNEW = 
    shipSET[givenSHIP].yMileNOW + dmCoSp.dmSpeedKt
    * dtMIN / 60.0 * Math.cos( dmCoSp.dmCourseRad ) ;
    shipSET[givenSHIP].courseRadNEW = dmCoSp.dmCourseRad ;
    shipSET[givenSHIP].speedKtNEW = dmCoSp.dmSpeedKt ;
    pnlMain.repaint((int)Math.round(shipSET[givenSHIP].xMileOLD*10),
    (int)Math.round(shipSET[givenSHIP].yMileOLD*10)+50, 3, 3);
    pnlMain.repaint((int)Math.round(shipSET[givenSHIP].xMileNEW*10),
    (int)Math.round(shipSET[givenSHIP].yMileNEW*10)+50, 3, 3);
    }/** FOR givenSHIP **/
    double collisionJudgeDIST = 0.1 ;
    for( int i = 0 ; i < maximumVESSELS ; i++ ) {
    int flagCC = 0 ;
    if( shipSET[i].vesselSTATUS == 0 ){
    for( int j = i+1 ; j < maximumVESSELS ; j++ ){
    if( shipSET[j].vesselSTATUS == 0 ){
    double x1 = shipSET[i].xMileNEW ;
    double y1 = shipSET[i].yMileNEW ;
    double x2 = shipSET[j].xMileNEW ;
    double y2 = shipSET[j].yMileNEW ;
    Point2D p1, p2 ;
    p1 = new Point2D( x1, y1 ) ;
    p2 = new Point2D( x2, y2 ) ;
    double dist = p1.distance2D( p2 ) ;
    if( dist < collisionJudgeDIST ) flagCC = 1 ;
    }
    }
    }
    }
    /** State Transition **/
    for( int i = 0 ; i < maximumVESSELS ; i++ ){
    if( shipSET[i].vesselSTATUS == 0 ){
    shipSET[i].xMileOLD = shipSET[i].xMileNOW ;
    shipSET[i].yMileOLD = shipSET[i].yMileNOW ;
    shipSET[i].courseRadOLD = shipSET[i].courseRadNOW;
    shipSET[i].speedKtOLD = shipSET[i].speedKtNOW ;
    shipSET[i].xMileNOW = shipSET[i].xMileNEW ;
    shipSET[i].yMileNOW = shipSET[i].yMileNEW ;
    shipSET[i].courseRadNOW = shipSET[i].courseRadNEW ;
    shipSET[i].speedKtNOW = shipSET[i].speedKtNEW ;
    }
    }
    clock++;
    }
    };
    timer.schedule(task, 0,100);
    } public static void main(String[] args){
    Frame5 ka = new Frame5();
    ka.setVisible(true);
    ka.transform();
    }

    class MyPanel extends JPanel {
    private static final long serialVersionUID = 1L;

    public MyPanel() {
    addKeyListener(new KeyAdapter(){
    public void keyPressed(KeyEvent e) {//事件监听
    if (movable) {
    int code=e.getKeyCode();
    switch (code) {
    case KeyEvent.VK_UP:
    // shipSET[0].yMileOLD = shipSET[0].yMileNEW;
    shipSET[0].yMileNEW -= 0.1;
    repaint((int)Math.round(shipSET[0].xMileNEW*10), 
    (int)Math.round(shipSET[0].yMileNEW*10)+50,
    3, 4);//控制重绘区域
    break;
    case KeyEvent.VK_DOWN:
    // shipSET[0].yMileOLD = shipSET[0].yMileNEW;
    shipSET[0].yMileNEW += 0.1;
    repaint((int)Math.round(shipSET[0].xMileNEW*10), 
    (int)Math.round(shipSET[0].yMileNEW*10)+49,
    3, 4);//控制重绘区域
    break;
    case KeyEvent.VK_LEFT:
    // shipSET[0].xMileOLD = shipSET[0].xMileNEW;
    shipSET[0].xMileNEW -= 0.1;
    repaint((int)Math.round(shipSET[0].xMileNEW*10), 
    (int)Math.round(shipSET[0].yMileNEW*10)+50,
    4, 3);//控制重绘区域
    break;
    case KeyEvent.VK_RIGHT:
    // shipSET[0].xMileOLD = shipSET[0].xMileNEW;
    shipSET[0].xMileNEW += 0.1;
    repaint((int)Math.round(shipSET[0].xMileNEW*10)-1, 
    (int)Math.round(shipSET[0].yMileNEW*10)+50,
    4, 3);//控制重绘区域
    break;
    }
    }
    }
    });
    } @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.GREEN);
    for (ShipCondition point : shipSET) {
    g.fillRect((int)Math.round(point.xMileNEW*10),
    (int)Math.round(point.yMileNEW*10)+50, 3, 3);
    }
    g.setColor(Color.RED);
    g.fillRect((int)Math.round(shipSET[0].xMileNEW*10),
    (int)Math.round(shipSET[0].yMileNEW*10)+50, 3, 3);
    }
    }
    }这是按你原来的那个改的了
      

  4.   

     double shipRad=360.0/((double)maximumVESSELS-1)*degToRAD;
            for(int i=1;i<maximumVESSELS;i++){
                shipSET[i].xMileOLD = shipSET[0].xMileOLD+5.0*Math.cos(shipRad*(double)i);
                shipSET[i].yMileOLD = shipSET[0].yMileOLD+5.0*Math.sin(shipRad*(double)i);
                shipSET[i].xMileNOW =shipSET[0].xMileNOW+5.0*Math.cos(shipRad*(double)i);
                shipSET[i].yMileNOW = shipSET[0].yMileNOW+5.0*Math.sin(shipRad*(double)i);
            }   
    这个不是我已开始的那个生成点点的算法吗?这个是生成圆形的 我现在想变成就是那样的
    像大雁飞行那样的
      

  5.   

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(500, 300);
            
            Container cont = this.getContentPane();
            cont.setLayout(new BorderLayout());
            setTitle("操船シミュレータ");
            DocumentListener listener = new ClockFieldListener();      
            pnlMain = new MyPanel();
            pnlMain.add(new JLabel("shiprad"));
           
            textField1=new JTextField(10);
            pnlMain.add(textField1);
            textField1.getDocument().addDocumentListener(listener);              
             btnInterrupt = new JButton("Interrupt");
            btnInterrupt.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e) 
                {
                    movable = true;
                    pnlMain.requestFocus();
                }
            }
            );
            cont.add(btnInterrupt, BorderLayout.NORTH);
            cont.add(pnlMain, BorderLayout.CENTER);
            }

        class ClockFieldListener implements DocumentListener
    {
    public void insertUpdate(DocumentEvent event){shipSET[0].speedNaviKT=Integer.parseInt(textField1.getText().trim());}
    public void removeUpdate(DocumentEvent event){shipSET[0].speedNaviKT=Integer.parseInt(textField1.getText().trim());}
    public void changedUpdate(DocumentEvent event){}
    }
      
        
       public void transform() {
        //Random rand = new Random(2146351065) ;
            timer = new Timer();
            task = new TimerTask() { 
            public void run() {
                    if (clock >= simFIN) {
                        timer.cancel();
                        return;
                   }
    for( int givenSHIP= 0 ; givenSHIP < maximumVESSELS ; givenSHIP ++ ){
                    /*** Navigation -> Navigation (status = 0 --> 0) ***/
                        /********************************************************/
                        /***** Ship Cluster Behavior Model Calculation *****/
                        /********************************************************/
                        if (movable && givenSHIP == 0) {
                            continue;
                        }
                        DecisionMakingReturn dmCoSp = 
                            DecisionMakingBody.agentDecisionMaking(shipSET, givenSHIP, dtMIN);
    /*** Quick Response ***/
                        shipSET[givenSHIP].xMileNEW = 
                            shipSET[givenSHIP].xMileNOW + dmCoSp.dmSpeedKt 
                            * dtMIN / 60.0 * Math.sin( dmCoSp.dmCourseRad );
                        shipSET[givenSHIP].yMileNEW = 
                            shipSET[givenSHIP].yMileNOW + dmCoSp.dmSpeedKt
                            * dtMIN / 60.0 * Math.cos( dmCoSp.dmCourseRad ) ;
                        shipSET[givenSHIP].courseRadNEW = dmCoSp.dmCourseRad ;
                        shipSET[givenSHIP].speedKtNEW = dmCoSp.dmSpeedKt ;
                        pnlMain.repaint((int)Math.round(shipSET[givenSHIP].xMileOLD*10),
                                (int)Math.round(shipSET[givenSHIP].yMileOLD*10)+50, 3, 3);
                        pnlMain.repaint((int)Math.round(shipSET[givenSHIP].xMileNEW*10),
                                (int)Math.round(shipSET[givenSHIP].yMileNEW*10)+50, 3, 3);
                    }/** FOR givenSHIP **/
                    double collisionJudgeDIST = 0.1 ;
                    for( int i = 0 ; i < maximumVESSELS ; i++ ) {
                        int flagCC = 0 ;
                        if( shipSET[i].vesselSTATUS == 0 ){
                            for( int j = i+1 ; j < maximumVESSELS ; j++ ){
                                if( shipSET[j].vesselSTATUS == 0 ){
                                    double x1 = shipSET[i].xMileNEW ;
                                    double y1 = shipSET[i].yMileNEW ;
                                    double x2 = shipSET[j].xMileNEW ;
                                    double y2 = shipSET[j].yMileNEW ;
                                    Point2D p1, p2 ;
                                    p1 = new Point2D( x1, y1 ) ;
                                    p2 = new Point2D( x2, y2 ) ;
                                    double dist = p1.distance2D( p2 ) ;
                                    if( dist < collisionJudgeDIST ) flagCC = 1 ;
                                }
                            }
                        }
                    }
                    /** State Transition **/
                    for( int i = 0 ; i < maximumVESSELS ; i++ ){
                        if( shipSET[i].vesselSTATUS == 0 ){
                            shipSET[i].xMileOLD = shipSET[i].xMileNOW ;
                            shipSET[i].yMileOLD = shipSET[i].yMileNOW ;
                            shipSET[i].courseRadOLD = shipSET[i].courseRadNOW;
                            shipSET[i].speedKtOLD = shipSET[i].speedKtNOW ;
                            shipSET[i].xMileNOW = shipSET[i].xMileNEW ;
                            shipSET[i].yMileNOW = shipSET[i].yMileNEW ;
                            shipSET[i].courseRadNOW = shipSET[i].courseRadNEW ;
                            shipSET[i].speedKtNOW = shipSET[i].speedKtNEW ;
                        }
                    }
                    clock++;
                }
            };
            timer.schedule(task, 0,300);
       }
    public static void main(String[] args){
            kaku ka = new kaku();
            ka.setVisible(true);
            ka.transform();
        }
    class MyPanel extends JPanel {
            private static final long serialVersionUID = 1L;
           public MyPanel() {
            
                addKeyListener(new KeyAdapter(){
                    public void keyPressed(KeyEvent e) {
                        if (movable) {
                            int code=e.getKeyCode();
                            switch (code) {
                            case KeyEvent.VK_UP:
    //                            shipSET[0].yMileOLD = shipSET[0].yMileNEW;
                                shipSET[0].yMileNEW -= 0.1;
                                repaint((int)Math.round(shipSET[0].xMileNEW*10), 
                                        (int)Math.round(shipSET[0].yMileNEW*10)+50,
                                        3, 4);
                                break;
                            case KeyEvent.VK_DOWN:
    //                            shipSET[0].yMileOLD = shipSET[0].yMileNEW;
                                shipSET[0].yMileNEW += 0.1;
                                repaint((int)Math.round(shipSET[0].xMileNEW*10), 
                                        (int)Math.round(shipSET[0].yMileNEW*10)+49,
                                        3, 4);
                                break;
                            case KeyEvent.VK_LEFT:
    //                            shipSET[0].xMileOLD = shipSET[0].xMileNEW;
                                shipSET[0].xMileNEW -= 0.1;
                                repaint((int)Math.round(shipSET[0].xMileNEW*10), 
                                        (int)Math.round(shipSET[0].yMileNEW*10)+50,
                                        4, 3);
                                break;
                            case KeyEvent.VK_RIGHT:
    //                            shipSET[0].xMileOLD = shipSET[0].xMileNEW;
                                shipSET[0].xMileNEW += 0.1;
                                repaint((int)Math.round(shipSET[0].xMileNEW*10)-1, 
                                        (int)Math.round(shipSET[0].yMileNEW*10)+50,
                                        4, 3);
                                break;
                            }
                        }
                    }
                });
            }        @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.GREEN);
                for (ShipCondition point : shipSET) {
                    g.fillRect((int)Math.round(point.xMileNEW*10),
                            (int)Math.round(point.yMileNEW*10)+50, 3, 3);
                }
                g.setColor(Color.RED);
                g.fillRect((int)Math.round(shipSET[0].xMileNEW*10),
                        (int)Math.round(shipSET[0].yMileNEW*10)+50, 3, 3);
            }
        }
    }
    感谢你 sunyi  帮我看看我这个程序  我加个文本框 想把输入的数字传递到ship[0].speedKtNOW这里  可是无论我怎么弄都失败的呢  麻烦你了
      

  6.   

    就给0的值。可是我穿不过去 如果get。text该如何使用呢
      

  7.   


    我用你的程序测的是能传过去的啊
    我把shipSET[0].speedNaviKT打印出来,是有值的
      

  8.   

    你是怎么打印的呢  我这个画图的时候不能加入打印语句啊,
    你说的text.gettext() 这个如果要用的话  应该加到初始化里面吗  还是循环里面
    我是想中断后,可以在对话框中输入数字,然后改变0的速度或者角度,这个该如何实现呢
      

  9.   

    你好 sunyi
    传说中的大雁那个算法
    能简单说一下吗?
    我没太懂
            for (int i=0;i<3;i++) {
                for (int j=0;j<3;j++) {
                    shipSET[i*3+j+1].xMileOLD = j*2+i;
                    shipSET[i*3+j+1].yMileOLD = i*1.7320508076;
                    shipSET[i*3+j+1].xMileNOW = shipSET[i*3+j+1].xMileOLD;
                    shipSET[i*3+j+1].yMileNOW = shipSET[i*3+j+1].yMileOLD;
                }
            }
            for (int i=3;i<5;i++) {
                for (int j=0;j<3;j++) {
                    shipSET[i*3+j+1].xMileOLD = j*2+(4-i);
                    shipSET[i*3+j+1].yMileOLD = i*1.7320508076;
                    shipSET[i*3+j+1].xMileNOW = shipSET[i*3+j+1].xMileOLD;
                    shipSET[i*3+j+1].yMileNOW = shipSET[i*3+j+1].yMileOLD;
                }