Advertisement
Nexmo

moving_x_objects

Jul 9th, 2021
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 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 moveStep = 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.  
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Die Liste wird durchlaufen und für jeden Punkt ein neuer Thread erstellt und die Methode CircleMOve aufgerufen mit übergabe des Punktes
  46.         /// </summary>
  47.         private void CircleMovestarts()
  48.         {
  49.             if (starter)
  50.             {            
  51.                 foreach (Point p in Werte)
  52.                 {
  53.                     ParameterizedThreadStart ts = new ParameterizedThreadStart(CircleMove);
  54.                     Thread t = new Thread(ts);
  55.                     object punkt = p;
  56.                     t.Start(punkt);
  57.                    
  58.                 }
  59.             }
  60.         }
  61.         /// <summary>
  62.         /// startet den Timer und ruft die Methode CircleMovestarts auf
  63.         /// </summary>
  64.         /// <param name="sender"></param>
  65.         /// <param name="e"></param>
  66.         private  void StartStop_Click(object sender, RoutedEventArgs e)
  67.         {
  68.            
  69.             if (_animationsTimer.IsEnabled)
  70.                 _animationsTimer.Stop();
  71.             else
  72.                 _animationsTimer.Start();
  73.             if (starter) { starter = false; }
  74.             starter = true;
  75.             CircleMovestarts();
  76.         }
  77.  
  78.         public int MoveStep { get; }
  79.         public int CoordinationY{set;get;}
  80.         public int CoordinationX {set; get; }
  81.         public int Radius { set; get; }
  82.         public int Anzahl { set; get; }
  83.         /// <summary>
  84.         /// Erstellt die Objekte auf der Canvas-Oberfläche
  85.         /// und speichert die Koordinaten in einer Liste ab.
  86.         /// </summary>
  87.         /// <param name="anzahl"></param>
  88.         public void CreateCircle(int anzahl)
  89.         {  
  90.            
  91.             for (int i = 0; i < anzahl; i++)
  92.             {
  93.  
  94.                 customColor = new SolidColorBrush(Color.FromRgb((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255)));
  95.                 Ellipse Circle = new Ellipse();
  96.                 Circle.Width = radius;
  97.                 Circle.Height = radius;
  98.                 Circle.Fill = customColor;
  99.                 coordinationY = r.Next(0, 450);
  100.                 coordinationX = r.Next(0, 450);
  101.                 Canvas.SetLeft(Circle, coordinationX);
  102.                 Canvas.SetTop(Circle, coordinationY);
  103.                 GamingArea.Children.Add(Circle);
  104.                 Point p = new Point();
  105.                 p.X = coordinationX;
  106.                 p.Y = coordinationY;
  107.                 Werte.Add(p);
  108.             }
  109.  
  110.         }
  111.  
  112.         public void RecreateCircle(int a, int b)
  113.         {
  114.             GamingArea.Children.Clear();
  115.  
  116.             Ellipse Circle = new Ellipse();
  117.             Circle.Width = radius;
  118.             Circle.Height = radius;
  119.             Circle.Fill = customColor;
  120.             Canvas.SetLeft(Circle, a);
  121.             Canvas.SetTop(Circle, b);
  122.             GamingArea.Children.Add(Circle);
  123.         }
  124.         public void CircleMove(object p)
  125.         {
  126.             //System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Object));
  127.             Point neu = (Point)p;
  128.             coordinationX = Convert.ToInt32(neu.X);
  129.             coordinationY = Convert.ToInt32(neu.Y);
  130.             _animationsTimer.Interval = TimeSpan.FromMilliseconds(10);
  131.             _animationsTimer.Tick += new EventHandler(CircleMoving);
  132.  
  133.         }
  134.  
  135.         /// <summary>
  136.         /// aufruf über den Eventhandler, dass die Objekte bewegt werden.
  137.         /// </summary>
  138.         /// <param name="sender"></param>
  139.         /// <param name="e"></param>
  140.         public void CircleMoving(object sender, EventArgs e)
  141.         {
  142.            
  143.            
  144.            coordinationY += moveStep;
  145.            if ((coordinationY < 0) || (coordinationY + radius > GamingArea.ActualHeight))
  146.            {
  147.                 moveStep = -moveStep;
  148.            }
  149.            
  150.            RecreateCircle(coordinationX, coordinationY);
  151.         }
  152.  
  153.         private void Next_Click(object sender, RoutedEventArgs e)
  154.         {
  155.             radius = int.Parse(RadiusEingabe.Text);
  156.             anzahl = int.Parse(AnzahlEingabe.Text);
  157.             CreateCircle(anzahl);
  158.             StartStop.Visibility = Visibility.Visible;
  159.         }
  160.     }
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement