そこで、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()));
}
}