Advertisement
Nexmo

Moving_Objects

Jul 7th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using System.Windows.Threading;
  17.  
  18. namespace moving_objects
  19. {
  20.     /// <summary>
  21.     /// Interaction logic for MainWindow.xaml
  22.     /// </summary>
  23.     public partial class MainWindow : Window
  24.     {
  25.         /// <summary>
  26.         /// Initialiserung der Parameter
  27.         /// </summary>
  28.         Brush customColor;
  29.         Random r = new Random();
  30.         private int coordinationY;
  31.         private int coordinationX;
  32.         private int anzahl;
  33.         private int radius;
  34.         private int moveStepY =4;
  35.         private protected DispatcherTimer _animationsTimer = new DispatcherTimer();
  36.         private List<Point> Werte = new List<Point>();
  37.         bool starter = false;
  38.         public MainWindow()
  39.         {
  40.             InitializeComponent();
  41.             _animationsTimer.Interval = TimeSpan.FromMilliseconds(10);
  42.            
  43.                  
  44.         }
  45.  
  46.        
  47.         private void Next_Click(object sender, RoutedEventArgs e)
  48.         {
  49.             anzahl = int.Parse(AnzahlEingabe.Text);
  50.             radius = int.Parse(RadiusEingabe.Text);
  51.             CreateCircle(anzahl);
  52.             StartStop.Visibility = Visibility.Visible;
  53.         }
  54.         /// <summary>
  55.         /// startet den Timer und ruft die Methode CircleMovestarts auf
  56.         /// </summary>
  57.         /// <param name="sender"></param>
  58.         /// <param name="e"></param>
  59.         private void StartStop_Click(object sender, RoutedEventArgs e)
  60.         {
  61.  
  62.             if (_animationsTimer.IsEnabled)
  63.                 _animationsTimer.Stop();
  64.             else
  65.                 _animationsTimer.Start();
  66.             if (starter) { starter = false; }
  67.             starter = true;
  68.             CircleMovestarts();
  69.         }
  70.  
  71.         public int MoveStepY { set; get; }
  72.         public int CoordinationY { set; get; }
  73.         public int CoordinationX { set; get; }
  74.         public int Radius { set; get; }
  75.         public int Anzahl { set; get; }
  76.         /// <summary>
  77.         /// Erstellt die Objekte auf der Canvas-Oberfläche
  78.         /// und speichert die Koordinaten in einer Liste ab.
  79.         /// </summary>
  80.         /// <param name="anzahl"></param>
  81.         public void CreateCircle(int anzahl)
  82.         {
  83.             GamingArea.Children.Clear();
  84.  
  85.             for (int i = 0; i < anzahl; i++)
  86.             {
  87.                 customColor = new SolidColorBrush(Color.FromRgb((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255)));
  88.                 Ellipse Circle = new Ellipse();
  89.                 Circle.Width = radius;
  90.                 Circle.Height = radius;
  91.                 Circle.Fill = customColor;
  92.                 coordinationY = r.Next(0, 450);
  93.                 coordinationX = r.Next(0, 450);
  94.                 Canvas.SetLeft(Circle, coordinationX);
  95.                 Canvas.SetTop(Circle, coordinationY);
  96.                 GamingArea.Children.Add(Circle);
  97.                 Point p = new Point();
  98.                 p.X = coordinationX;
  99.                 p.Y = coordinationY;
  100.                 Werte.Add(p);
  101.             }
  102.            
  103.         }
  104.  
  105.         public void RecreateCircle(int a, int b)
  106.         {
  107.             GamingArea.Children.Clear();
  108.      
  109.             Ellipse Circle = new Ellipse();
  110.             Circle.Width = radius;
  111.             Circle.Height = radius;
  112.             Circle.Fill = customColor;
  113.             Canvas.SetLeft(Circle, a);
  114.             Canvas.SetTop(Circle, b);
  115.             GamingArea.Children.Add(Circle);
  116.         }
  117.         /// <summary>
  118.         /// Die Liste wird durchlaufen und für jeden Punkt ein neuer Thread erstellt und die Methode CircleMOve aufgerufen mit übergabe des Punktes
  119.         /// </summary>
  120.         private async void CircleMovestarts()
  121.         {
  122.             if (starter)
  123.             {
  124.                
  125.                
  126.                     foreach (Point p in Werte)
  127.                     {
  128.                    
  129.                          ParameterizedThreadStart ts = new ParameterizedThreadStart(CircleMove);
  130.                         Thread t = new Thread(ts);
  131.                         object punkt = p;
  132.                         await Task.Run(t.Start(punkt));
  133.                         t.Start(punkt);
  134.  
  135.                     }
  136.             }
  137.         }
  138.         public void CircleMove(object p)
  139.         {
  140.             //System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Object));
  141.             Point neu = (Point)p;
  142.             coordinationX = Convert.ToInt32(neu.X);
  143.             coordinationY = Convert.ToInt32(neu.Y);
  144.             _animationsTimer.Tick += new EventHandler(CircleMoving);
  145.         }
  146.  
  147.         /// <summary>
  148.         /// aufruf über den Eventhandler, dass die Objekte bewegt werden.
  149.         /// </summary>
  150.         /// <param name="sender"></param>
  151.         /// <param name="e"></param>
  152.         public void CircleMoving(object sender, EventArgs e)
  153.         {
  154.  
  155.            
  156.             coordinationY += moveStepY;
  157.             if ((coordinationY < 0) || (coordinationY + radius > GamingArea.ActualHeight))
  158.             {
  159.                 moveStepY = -moveStepY;
  160.             }
  161.            
  162.             RecreateCircle(coordinationX, coordinationY);
  163.         }
  164.  
  165.        
  166.     }
  167.    
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement