版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangyisen0713/article/details/18216803
WPF模拟太阳。月球、地球三者运动轨迹的模拟,现在还没有加上太阳自传的动画,有兴趣的可以加上。
主要是利用EllipseGeometry实现路径的绘制
xaml代码如下:
<Window x:Class="WpfApp11.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="地球、月球、太阳运动模拟" Width="1366" Height="768" WindowStartupLocation="CenterScreen"> <Grid> <Grid.Background> <ImageBrush ImageSource="earth.jpg" /> </Grid.Background> <Ellipse Name="ellipse3" Width="150" Height="150" Margin="619,321,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" ToolTip="太阳"> <Ellipse.Fill> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <GradientStop Offset="0.246" Color="#FFFFCA00" /> <GradientStop Offset="0.967" Color="#FFFF0034" /> </LinearGradientBrush> </Ellipse.Fill> </Ellipse> <!-- 通过使用EllipseGeometry实现椭圆路径的绘制 --> <Path Margin="308,136,120,81" RenderTransformOrigin="0.415,0.498" Stroke="#FFFF7900" StrokeThickness="5"> <Path.Data> <EllipseGeometry x:Name="e1" Center="400 250" RadiusX="400" RadiusY="250" /> </Path.Data> </Path> <Grid Name="grid1" Width="484" Height="352" Margin="30,-12,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Grid.RenderTransform> <MatrixTransform x:Name="grid" /> </Grid.RenderTransform> <!-- Grid触发器 --> <Grid.Triggers> <EventTrigger RoutedEvent="Page.Loaded"> <BeginStoryboard> <Storyboard x:Name="sb1" RepeatBehavior="Forever"> <MatrixAnimationUsingPath x:Name="ma1" Storyboard.TargetName="grid" Storyboard.TargetProperty="Matrix" Duration="0:1:0" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Grid.Triggers> <Ellipse Name="ellipse1" Width="100" Height="100" Margin="227,94,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" ToolTip="地球"> <Ellipse.Fill> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <GradientStop Offset="0" Color="#FFE0DFDD" /> <GradientStop Offset="0.975" Color="#FF0035FF" /> </LinearGradientBrush> </Ellipse.Fill> <Ellipse.RenderTransform> <TransformGroup> <MatrixTransform x:Name="earth" /> </TransformGroup> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTrigger RoutedEvent="Page.Loaded" /> </Ellipse.Triggers> </Ellipse> <Ellipse Name="ellipse2" Width="50" Height="50" Margin="228,40,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" ToolTip="月球"> <Ellipse.Fill> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <GradientStop Offset="0" Color="White" /> <GradientStop Offset="0.943" Color="#FFDDD2BE" /> </LinearGradientBrush> </Ellipse.Fill> <Ellipse.RenderTransform> <TransformGroup> <MatrixTransform x:Name="moon" /> </TransformGroup> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTrigger RoutedEvent="Page.Loaded"> <BeginStoryboard> <Storyboard x:Name="sb2" RepeatBehavior="Forever"> <MatrixAnimationUsingPath x:Name="ma2" Storyboard.TargetName="moon" Storyboard.TargetProperty="Matrix" Duration="0:0:30" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Ellipse.Triggers> </Ellipse> <Path Width="122" Height="200" Margin="262.522,59,0,93" HorizontalAlignment="Left" Stroke="#FF00FF40" StrokeThickness="5"> <Path.Data> <EllipseGeometry x:Name="ellipseGeometry1" Center="50 100" RadiusX="50" RadiusY="100"> <EllipseGeometry.Transform> <SkewTransform AngleY="-20" /> </EllipseGeometry.Transform> </EllipseGeometry> </Path.Data> </Path> </Grid> <TextBox Width="448" Height="110" Margin="891,7,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Background="{x:Null}" BorderBrush="#FF0012FF" BorderThickness="0" Foreground="White" Text="地球饶太阳公转,月球饶地球公转。太阳、地球、月球都在自转太阳是太阳系的主宰,是恒星。地球是太阳系的一颗行星,月球是地球的一颗天然卫星。地球绕着太阳公转,月球绕着地球公转。太阳不可能位于地球和月球之间。" TextWrapping="Wrap" /> </Grid> </Window>
最后效果如图:
public MainWindow() { InitializeComponent(); ma2.PathGeometry = ellipseGeometry1.GetFlattenedPathGeometry(); ma1.PathGeometry = e1.GetFlattenedPathGeometry(); }