2013-04-20 4 views
2

타원의 높이 너비와 색을 정의하는 EllipsePoints 배열을 사용하여 타원을 그렸습니다. 나는 타원의 일정 금액 후 사라 만들기 위해 무엇을 할 수 있는지타원 그리기 및 다른 위치로 이동

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

+0

제 생각에는'System.Timers.Timer' 또는'System.Threading.Thread'라는 두 가지 선택을 할 수 있습니다. 타이머는 완료되면 이벤트를 반환하는 매우 간단한 카운트 다운입니다. 스레드는 새로운 독립적 인 실행 행입니다. 각 반복 사이에 일시 중지와 함께 자체적으로 반복 될 수 있습니다. – LightStriker

+0

C#을 처음 사용하는 이유는 무엇입니까? – user1866990

답변

3

이 질문에는 두 가지 중요한 측면이 있습니다.

  • 타이머은 - WPF에서 우리는 System.Windows.Threading.DispatcherTimer 사용합니다.
  • 요소 제거 - 한 가지 방법은 UI 요소를 Canvas에 추가하기 전에 유지 관리하는 것입니다. 클래스 변수에 저장하여 나중에 다음 방법을 사용하여 캔버스에서 제거 할 수 있습니다.

    PaintCanvas.Children.Remove (ellipse);

는 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