2013-04-20 66 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

相关问题