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 int moveStepY =4;
- private protected DispatcherTimer _animationsTimer = new DispatcherTimer();
- private List<Point> Werte = new List<Point>();
- bool starter = false;
- 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();
- if (starter) { starter = false; }
- starter = true;
- 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 a, int b)
- {
- GamingArea.Children.Clear();
- Ellipse Circle = new Ellipse();
- Circle.Width = radius;
- Circle.Height = radius;
- Circle.Fill = customColor;
- Canvas.SetLeft(Circle, a);
- Canvas.SetTop(Circle, b);
- 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 async void CircleMovestarts()
- {
- if (starter)
- {
- foreach (Point p in Werte)
- {
- ParameterizedThreadStart ts = new ParameterizedThreadStart(CircleMove);
- Thread t = new Thread(ts);
- object punkt = p;
- await Task.Run(t.Start(punkt));
- t.Start(punkt);
- }
- }
- }
- public void CircleMove(object p)
- {
- //System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Object));
- Point neu = (Point)p;
- coordinationX = Convert.ToInt32(neu.X);
- coordinationY = Convert.ToInt32(neu.Y);
- _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 > GamingArea.ActualHeight))
- {
- moveStepY = -moveStepY;
- }
- RecreateCircle(coordinationX, coordinationY);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement