Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace moving_objects
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- /// <summary>
- /// Initialiserung der Parameter
- /// </summary>
- Brush customColor;
- Random r = new Random();
- private int coordinationY;
- private int coordinationX;
- private int anzahl;
- private int radius;
- private protected DispatcherTimer _animationsTimer = new DispatcherTimer();
- private List<Point> Werte = new List<Point>();
- private List<Thread> myThreads = new List<Thread>();
- private int moveStepY = 4;
- public MainWindow()
- {
- InitializeComponent();
- _animationsTimer.Interval = TimeSpan.FromMilliseconds(10);
- }
- private void Next_Click(object sender, RoutedEventArgs e)
- {
- anzahl = int.Parse(AnzahlEingabe.Text);
- radius = int.Parse(RadiusEingabe.Text);
- CreateCircle(anzahl);
- StartStop.Visibility = Visibility.Visible;
- }
- /// <summary>
- /// startet den Timer und ruft die Methode CircleMovestarts auf
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void StartStop_Click(object sender, RoutedEventArgs e)
- {
- if (_animationsTimer.IsEnabled)
- _animationsTimer.Stop();
- else
- _animationsTimer.Start();
- CircleMovestarts();
- }
- public int MoveStepY { set; get; }
- public int CoordinationY { set; get; }
- public int CoordinationX { set; get; }
- public int Radius { set; get; }
- public int Anzahl { set; get; }
- /// <summary>
- /// Erstellt die Objekte auf der Canvas-Oberfläche
- /// und speichert die Koordinaten in einer Liste ab.
- /// </summary>
- /// <param name="anzahl"></param>
- public void CreateCircle(int anzahl)
- {
- GamingArea.Children.Clear();
- for (int i = 0; i < anzahl; i++)
- {
- customColor = new SolidColorBrush(Color.FromRgb((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255)));
- Ellipse Circle = new Ellipse();
- Circle.Width = radius;
- Circle.Height = radius;
- Circle.Fill = customColor;
- coordinationY = r.Next(0, 450);
- coordinationX = r.Next(0, 450);
- Canvas.SetLeft(Circle, coordinationX);
- Canvas.SetTop(Circle, coordinationY);
- GamingArea.Children.Add(Circle);
- Point p = new Point();
- p.X = coordinationX;
- p.Y = coordinationY;
- Werte.Add(p);
- }
- }
- public void RecreateCircle(int coordX, int coordY)
- {
- GamingArea.Children.Clear();
- Ellipse Circle = new Ellipse();
- Circle.Width = radius;
- Circle.Height = radius;
- Circle.Fill = customColor;
- Canvas.SetLeft(Circle, coordX);
- Canvas.SetTop(Circle, coordY);
- GamingArea.Children.Add(Circle);
- }
- /// <summary>
- /// Die Liste wird durchlaufen und für jeden Punkt ein neuer Thread erstellt und die Methode CircleMOve aufgerufen mit übergabe des Punktes
- /// </summary>
- //private Task Move() => Task.Run(async() => await );
- private void CircleMovestarts()
- {
- foreach (Point p in Werte)
- {
- CircleMove(p);
- }
- }
- public async void CircleMove(Point p)
- {
- coordinationX = Convert.ToInt32(p.X);
- coordinationY = Convert.ToInt32(p.Y);
- await Task.Run(() => _animationsTimer.Tick += new EventHandler(CircleMoving));
- }
- /// <summary>
- /// aufruf über den Eventhandler, dass die Objekte bewegt werden.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void CircleMoving(object sender, EventArgs e)
- {
- coordinationY += moveStepY;
- if ((coordinationY < 0) || (coordinationY + radius > ActualHeight))
- {
- moveStepY = -moveStepY;
- }
- RecreateCircle(coordinationX, coordinationY);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement