2012-12-13 64 views
0

因此,我正在編寫Java的一個小練習,我創建了幾個簡單繪製黑色背景的類,然後從中心繪製圓圈半徑除90度旋轉(代碼在底部)。節點知道Java中其他節點的相對位置

現在,只是使用「隨機」種子來確定旅行方向,這總是趨向下降。每次。所以我現在想做的就是試着展示其他圈子的圈子,並讓他們傾向於向另一個圈子彎曲運動(除了父母之外沒有其他圈子)。我可以處理這部分,但我想要一個有效的方式來溝通我的「節點」或「圈子」之間的位置。我可以用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

它聽起來有點像一個圖,其中有節點了解它們的連接節點。如果你在Jim Barrows的回答中展開,你可以實現一個CircleNode,它有一個位置在一個List和一個List/Set中,一個包含所有節點的Board。然後,從任何節點開始,你都會知道你的鄰居在哪裏。如果你正在移動一塊,它可以插入當前節點,得到鄰居,並在選擇移動到鄰居時,插入新節點。 – CodeChimp

回答

0

你的圈子可以存在於「板子」上,這是一個二維數組。然後你可以給每個圈子一個董事會的看法。你也可以讓董事會決定把下一個圓圈放在哪裏。

+0

我想離開圈子定義海誓山盟,我喜歡使用這種自然下降的趨勢作爲一種「引力」的想法,從彼此畫圓圈,而他們仍然同時試圖抓住彼此。我想我對如何製作這個電路板有一個很好的想法 - 我將在整個下午工作,看看我能不能找到一個功能強大的電路板。我稍後會回覆,謝謝! – user1695505

相關問題