Advertisement
Nexmo

moving_x_objects_task

Jul 9th, 2021
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 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 protected DispatcherTimer _animationsTimer = new DispatcherTimer();
  35.         private List<Point> Werte = new List<Point>();
  36.         private List<Thread> myThreads = new List<Thread>();
  37.         private int moveStepY = 4;
  38.  
  39.  
  40.  
  41.         public MainWindow()
  42.         {
  43.             InitializeComponent();
  44.             _animationsTimer.Interval = TimeSpan.FromMilliseconds(10);
  45.  
  46.  
  47.         }
  48.  
  49.  
  50.         private void Next_Click(object sender, RoutedEventArgs e)
  51.         {
  52.             anzahl = int.Parse(AnzahlEingabe.Text);
  53.             radius = int.Parse(RadiusEingabe.Text);
  54.             CreateCircle(anzahl);
  55.             StartStop.Visibility = Visibility.Visible;
  56.         }
  57.         /// <summary>
  58.         /// startet den Timer und ruft die Methode CircleMovestarts auf
  59.         /// </summary>
  60.         /// <param name="sender"></param>
  61.         /// <param name="e"></param>
  62.         private void StartStop_Click(object sender, RoutedEventArgs e)
  63.         {
  64.  
  65.             if (_animationsTimer.IsEnabled)
  66.                 _animationsTimer.Stop();
  67.             else
  68.                 _animationsTimer.Start();
  69.  
  70.             CircleMovestarts();
  71.  
  72.         }
  73.  
  74.         public int MoveStepY { set; get; }
  75.         public int CoordinationY { set; get; }
  76.         public int CoordinationX { set; get; }
  77.         public int Radius { set; get; }
  78.         public int Anzahl { set; get; }
  79.         /// <summary>
  80.         /// Erstellt die Objekte auf der Canvas-Oberfläche
  81.         /// und speichert die Koordinaten in einer Liste ab.
  82.         /// </summary>
  83.         /// <param name="anzahl"></param>
  84.         public void CreateCircle(int anzahl)
  85.         {
  86.             GamingArea.Children.Clear();
  87.  
  88.             for (int i = 0; i < anzahl; i++)
  89.             {
  90.                 customColor = new SolidColorBrush(Color.FromRgb((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255)));
  91.                 Ellipse Circle = new Ellipse();
  92.                 Circle.Width = radius;
  93.                 Circle.Height = radius;
  94.                 Circle.Fill = customColor;
  95.                 coordinationY = r.Next(0, 450);
  96.                 coordinationX = r.Next(0, 450);
  97.                 Canvas.SetLeft(Circle, coordinationX);
  98.                 Canvas.SetTop(Circle, coordinationY);
  99.                 GamingArea.Children.Add(Circle);
  100.                 Point p = new Point();
  101.                 p.X = coordinationX;
  102.                 p.Y = coordinationY;
  103.                 Werte.Add(p);
  104.  
  105.             }
  106.  
  107.         }
  108.  
  109.         public void RecreateCircle(int coordX, int coordY)
  110.         {
  111.             GamingArea.Children.Clear();
  112.  
  113.             Ellipse Circle = new Ellipse();
  114.             Circle.Width = radius;
  115.             Circle.Height = radius;
  116.             Circle.Fill = customColor;
  117.             Canvas.SetLeft(Circle, coordX);
  118.             Canvas.SetTop(Circle, coordY);
  119.             GamingArea.Children.Add(Circle);
  120.         }
  121.         /// <summary>
  122.         /// Die Liste wird durchlaufen und für jeden Punkt ein neuer Thread erstellt und die Methode CircleMOve aufgerufen mit übergabe des Punktes
  123.         /// </summary>
  124.  
  125.         //private Task Move() => Task.Run(async() => await );
  126.         private void CircleMovestarts()
  127.         {
  128.  
  129.             foreach (Point p in Werte)
  130.             {
  131.                
  132.                  CircleMove(p);
  133.             }
  134.  
  135.         }
  136.  
  137.         public async void CircleMove(Point p)
  138.         {
  139.  
  140.             coordinationX = Convert.ToInt32(p.X);
  141.             coordinationY = Convert.ToInt32(p.Y);
  142.            
  143.             await Task.Run(() => _animationsTimer.Tick += new EventHandler(CircleMoving));
  144.  
  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 > ActualHeight))
  158.             {
  159.                 moveStepY = -moveStepY;
  160.             }
  161.  
  162.  
  163.             RecreateCircle(coordinationX, coordinationY);
  164.         }
  165.  
  166.  
  167.     }
  168.  
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement