节点知道Java中其他节点的相对位置

节点知道Java中其他节点的相对位置,java,nodes,Java,Nodes,我正在做一个Java编程的小练习,我创建了两个类,它们只需绘制一个黑色背景,然后从中心开始,以90度旋转的方式绘制彼此相距一个半径的圆(代码在文章底部) 现在,仅仅用一个“随机”种子来确定旅行的方向,这总是向下的。每一次。所以我现在想做的是,试着把我的圆显示在其他圆的位置,并让它们倾向于向另一个圆弯曲运动(不存在于父圆以外的另一个圆上)。我可以处理这一部分,但我需要一种在我的“节点”或“圆”之间传递位置的有效方法。我可以用x,y构建一个字符串数组,并在每次构建种子时通读该列表,但这似乎是一种低效

我正在做一个Java编程的小练习,我创建了两个类,它们只需绘制一个黑色背景,然后从中心开始,以90度旋转的方式绘制彼此相距一个半径的圆(代码在文章底部)

现在,仅仅用一个“随机”种子来确定旅行的方向,这总是向下的。每一次。所以我现在想做的是,试着把我的圆显示在其他圆的位置,并让它们倾向于向另一个圆弯曲运动(不存在于父圆以外的另一个圆上)。我可以处理这一部分,但我需要一种在我的“节点”或“圆”之间传递位置的有效方法。我可以用x,y构建一个字符串数组,并在每次构建种子时通读该列表,但这似乎是一种低效的方法。我更愿意找到一种方法,一个圆圈可以环顾四周,找到附近的其他圆圈。有没有办法做到这一点?我不介意一些阅读和家庭作业,我主要是在寻找一个好的策略

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FlowerOfLife extends JFrame {
    private Circle Origin = null;



    public FlowerOfLife() {
        // Default layout manager is BorderLayout.  
        // Adding graphic component to center section will expand  
        // it to fill the available space so it does not need to  
        // provide any size hints for this parent container now.  
        GraphicPanel graphicPanel = new GraphicPanel();
        add(graphicPanel);     // default center section  
        setDefaultCloseOperation(EXIT_ON_CLOSE);  
        setSize(400,400);  
        setLocation(200,200);  
        setVisible(true);   
    }

    /** 
     * Container method draws container and passes graphics context 
     * on to components for them to draw themselves. You can draw 
     * over everything in the content pane with this method... 
     */  
    public void paint(Graphics gr)  
    {  
        Graphics2D g = (Graphics2D)gr;
        // see what happens when you remove/move this next line  
        super.paint(g);  

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
                            RenderingHints.VALUE_ANTIALIAS_ON);  
        int w = getWidth();  
        int h = getHeight();
        g.setPaint(Color.white);//First there is one...
        Origin = new Circle(g, w/2, h/2, ((w+h)/2)/35);
        g.setPaint(Color.white.darker());

        Circle[] circleArray = new Circle[100];
        for(int i=0;i<circleArray.length;i++){
            circleArray[i] = new Circle(g,i>0?circleArray[i-1]:Origin);
        }


    }
    public static void main(String[] args)  
    {  
        new FlowerOfLife();  
    } 

}

class Circle{
    public int x;
    public int y;
    public int r;
    private Circle next;
    private Circle last;
    private int    seed;

    public Circle(Graphics2D g, Circle c){ 
        c.next = this;
        this.last = c;
        this.r  = c.r; //Copy radius
        //We set these now, and modify the necessary ones.
        this.x = c.x;
        this.y = c.y;
        this.r = c.r;

        //Move the new circle into position based on seed.
        this.seed = (int) (Math.random()*4);
        if(this.seed==0){//Stay here, and grow a little...
//          this.r+=1;
        }else if(this.seed==1){//Move left, by r.
            this.x-=c.r;    
        }else if(this.seed==2){//Move up,   by r.
            this.y+=c.r;
        }else if(this.seed==3){//Move right,by r.
            this.x+=c.r;
        }else{//(this.seed==4) //Move down, by r.
            this.y-=c.r;
        }
        sleep((int)(Math.random())*9000+100);//Random(ish) life.
        //Draw the new circle
        g.draw(new Ellipse2D.Double(x,y,r*2,r*2));


    }
    public Circle(Graphics2D g,int x, int y, int r){
        /**
         *  The ellipse draws from the designated point, down and right.
         * The below permits the center to be declared in a more natural
         * way. Used for the first declaration.
        **/
        this.last = null; //Parent.
        this.x = x-r;
        this.y = y-r;
        this.r = r;
        this.seed = 0;
        g.draw(new Ellipse2D.Double(x,y,r*2,r*2));
    }
    private void sleep(int ms){
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


class GraphicPanel extends JPanel  
{  
    protected void paintComponent(Graphics g)  
    {  
        super.paintComponent(g);  
        Graphics2D g2 = (Graphics2D)g;  
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.black);
        g2.fill(new Rectangle2D.Double(0,0,getWidth(),getHeight()));
    }  
}  
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.RenderingHints;
导入java.awt.geom.Ellipse2D;
导入java.awt.geom.Line2D;
导入java.awt.geom.Rectangle2D;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类生命扩展JFrame{
专用圆原点=空;
公共生活{
//默认布局管理器是BorderLayout。
//将图形组件添加到中间部分将展开
//它可以填充可用空间,因此不需要
//现在提供此父容器的任何大小提示。
GraphicPanel GraphicPanel=新的GraphicPanel();
添加(graphicPanel);//默认的中间部分
setDefaultCloseOperation(关闭时退出);
设置大小(400400);
设定位置(200200);
setVisible(真);
}
/** 
*容器方法绘制容器并传递图形上下文
*上的组件,以便它们自己绘制。您可以绘制
*使用此方法覆盖内容窗格中的所有内容。。。
*/  
公共空间涂料(图形gr)
{  
Graphics2D g=(Graphics2D)gr;
//查看删除/移动下一行时发生的情况
超级油漆(g);
g、 setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
int w=getWidth();
inth=getHeight();
g、 setPaint(Color.white);//首先有一个。。。
原点=新圆(g,w/2,h/2,((w+h)/2)/35);
g、 setPaint(Color.white.darker());
圆圈[]circleArray=新圆圈[100];
对于(int i=0;i0?circleArray[i-1]:原点);
}
}
公共静态void main(字符串[]args)
{  
生命的新花朵();
} 
}
班级圈子{
公共int x;
公共智力;
公共INTR;
接下来是私人圈子;
私人圈子最后;
私有int种子;
公众圈(图2d g,圈c){
c、 下一个=这个;
这个最后一个=c;
this.r=c.r;//复制半径
//我们现在设置这些,并修改必要的。
这个.x=c.x;
这个.y=c.y;
这个r=c.r;
//根据种子将新圆移动到位。
this.seed=(int)(Math.random()*4);
如果(this.seed==0){//留在这里,长一点。。。
//这是r+=1;
}如果(this.seed==1){//向左移动,按r。
这.x-=c.r;
}否则如果(this.seed==2){//向上移动r。
这.y+=c.r;
}如果(this.seed==3){//向右移动,则按r。
这个.x+=c.r;
}else{//(this.seed==4)//按r向下移动。
这.y-=c.r;
}
睡眠((int)(Math.random())*9000+100);//随机(ish)生活。
//画新的圆圈
g、 绘制(新的椭圆2d.Double(x,y,r*2,r*2));
}
公共圆圈(图形2D g、int x、int y、int r){
/**
*椭圆从指定点向下和向右绘制。
*下面允许以更自然的方式声明中心
*方式。用于第一次声明。
**/
this.last=null;//父项。
这个。x=x-r;
这个。y=y-r;
这个。r=r;
这个种子=0;
g、 绘制(新的椭圆2d.Double(x,y,r*2,r*2));
}
私人无效睡眠(int-ms){
试一试{
睡眠(ms);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
类GraphicPanel扩展了JPanel
{  
受保护组件(图形g)
{  
超级组件(g);
图形2d g2=(图形2d)g;
g2.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(颜色为黑色);
g2.填充(新矩形2D.Double(0,0,getWidth(),getHeight());
}  
}  

您的圆可以存在于一个“板”上,这将是一个二维数组。然后,你可以给每个圆圈一个棋盘的视图。你也可以让董事会决定下一个圆圈的位置。

我想让圆圈相互定义,我喜欢使用这种自然下降趋势作为某种“重力”,将圆圈彼此拉开,同时它们仍然试图抓住彼此。我想我对如何制作你描述的这块板有一个很好的想法——我整个下午都在做,看看我是否能得到一块实用的。我稍后会发回,谢谢!它听起来有点像一个图形,其中的节点知道它们所连接的节点。如果你扩展Jim Barrows的答案,你可以实现一个CircleNode,它在电路板中有一个位置和一个邻居列表/集合,以及一个包含所有节点的电路板。然后,开始