示例#1
0
        private void BtnBuild_Click(object sender, RoutedEventArgs e)
        {
            // создаем набор случайных примитивов
            RandomFactory drankDirector = new RandomFactory();

            for (int i = 0; i < 5; i++)
            {
                AddToCanvas(
                    drankDirector.BuildRandomShape(), // директор
                    random.NextDouble() * (canvas.ActualWidth - 50),
                    random.NextDouble() * (canvas.ActualHeight - 50));
            }

            // клонирование дерева
            Shape tree = new ShapeBuilder(ShapeBuilder.Type.TREE).Build();

            tree.Stroke = Brushes.Blue;
            double left = random.NextDouble() * (canvas.ActualWidth - 50);
            double top  = random.NextDouble() * (canvas.ActualHeight - 50);

            AddToCanvas(tree, left, top);

            for (int j = 0; j < 2; j++)
            {
                left += tree.Width * 0.5;

                Tree copy = (tree as Tree).Clone() as Tree; // копия
                copy.Stroke = Brushes.Green;
                AddToCanvas(copy, left, top);
            }
        }
示例#2
0
        private void BtnBuild_Click(object sender, RoutedEventArgs e)
        {
            RandomFactory drankDirector = new RandomFactory();

            for (int i = 0; i < 5; i++)
            {
                Shape  s    = drankDirector.BuildRandomShape(); // директор
                double left = random.NextDouble() * (canvas.ActualWidth - 50);
                double top  = random.NextDouble() * (canvas.ActualHeight - 50);
                s.SetValue(Canvas.LeftProperty, left);
                s.SetValue(Canvas.TopProperty, top);
                canvas.Children.Add(s);

                // Добавление копий
                if (s is TreeShape)
                {
                    TreeShape tree = s as TreeShape;
                    tree.Stroke = Brushes.Blue;
                    for (int j = 0; j < 2; j++)
                    {
                        left += tree.Width / 2;
                        top  += tree.Height * 0.2;

                        TreeShape copy = tree.Clone() as TreeShape; // копия
                        copy.SetValue(Canvas.LeftProperty, left);
                        copy.SetValue(Canvas.TopProperty, top);
                        copy.Height = copy.Height * 0.8;
                        copy.Width  = copy.Width * 0.8;
                        copy.Stroke = Brushes.Green;
                        canvas.Children.Add(copy);

                        tree = copy;
                    }
                }
            }
        }