2013-04-20 67 views
2

我得出使用EllipsePoints陣列限定橢圓的高度寬度和顏色的橢圓。繪製一個橢圓,然後將其移動到另一個位置

然後使用for循環使用橢圓點和一個隨機數來設置它的位置來得到橢圓的位置:

Random rand = new Random();  

Int32 randomNumber = rand.Next(0, 310); 
Int32 randomNumber2 = rand.Next(0, 500); 

for (int j = 0; j < 60; j++) 
{ 
    ellipsePoints[j] = new Ellipse() { Width = 20, Height = 20, Fill = Brushes.Red }; 

    canvas1.Children.Add(ellipsePoints[j]); 
} 

for (int i = 0; i < 60; i++) 
{ 
    Canvas.SetLeft(ellipsePoints[i], randomNumber2); 
    Canvas.SetTop(ellipsePoints[i], randomNumber); 
} 

我能做什麼做了一定量之後,橢圓消失時間,然後出現在另一個隨機的位置?

+0

我試過使用while循環,但它只是凍結。這是我嘗試過的唯一途徑。 – user1866990 2013-04-20 15:10:06

+0

在我看來,你有兩個選擇,你可以探索:'System.Timers.Timer'或'System.Threading.Thread'。一個定時器是一個非常簡單的倒計時,一旦它完成就返回一個事件。線程是一個新的獨立執行線。它可以在每次迭代之間停頓一段時間。 – LightStriker 2013-04-20 15:17:02

+0

我是c#的新手,你如何在c#中使用線程。 – user1866990 2013-04-20 17:16:35

回答

3

有這個問題2個的重要方面。

  • 定時器 - 在WPF中我們使用System.Windows.Threading.DispatcherTimer
  • 刪除元素 - 的一種方法是將它添加到畫布之前維持UI元素的副本。我已經在一個類變量保存它,這樣我可以從畫布用以下方法後刪除它

    PaintCanvas.Children.Remove(橢圓形);

創建你WPF和添加一個名爲帆布PaintCanvas

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="889" Width="1080"> 
    <Canvas Name="PaintCanvas"> 
     <Button Canvas.Left="46" Canvas.Top="274" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" /> 
    </Canvas > 
</Window> 

的代碼。我已經記錄了它。

public partial class MainWindow : Window 
{ 
    int loopCounter; 
    private System.Windows.Threading.DispatcherTimer timer; 
    Random rand = new Random(); 
    Ellipse ellipse = null; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     //Initialize the timer class 
     timer = new System.Windows.Threading.DispatcherTimer(); 
     timer.Interval = TimeSpan.FromSeconds(1); //Set the interval period here. 
     timer.Tick += timer1_Tick;    
    }  

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     loopCounter = 10; 
     timer.Start(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     //Remove the previous ellipse from the paint canvas. 
     PaintCanvas.Children.Remove(ellipse); 

     if (--loopCounter == 0) 
      timer.Stop(); 

     //Add the ellipse to the canvas 
     ellipse=CreateAnEllipse(20,20); 
     PaintCanvas.Children.Add(ellipse); 

     Canvas.SetLeft(ellipse, rand.Next(0, 310)); 
     Canvas.SetTop(ellipse, rand.Next(0, 500)); 
    } 

    // Customize your ellipse in this method 
    public Ellipse CreateAnEllipse(int height,int width) 
    { 
     SolidColorBrush fillBrush = new SolidColorBrush() { Color = Colors.Red }; 
     SolidColorBrush borderBrush = new SolidColorBrush() { Color = Colors.Black }; 

     return new Ellipse() 
     { 
      Height = height, 
      Width = width, 
      StrokeThickness = 1, 
      Stroke = borderBrush, 
      Fill = fillBrush 
     };  
    } 
} 
+1

感謝您提供的文檔代碼,這是我正在嘗試做的 – user1866990 2013-04-20 18:49:27

相關問題