所以我正在编写一个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()));
}
}
答案 0 :(得分:0)
您的圈子可以存在于“棋盘”上,这将是一个二维数组。然后,您可以为每个圆圈提供电路板视图。你也可以让董事会确定下一个圈子的位置。