私はバイナリツリーの内容を視覚的に出力するプログラムに取り組んできました。このプログラムに含める最後の機能は、ツリーのポストオーダー、インオーダー、およびプリオーダーの構成のアニメーションです。Thread.Sleepを使用してバイナリツリーをアニメーション化する方法は?
これは、私が思ったよりもはるかに難しいことが分かっています。ここでは、元のドローでメソッドの:
private void DrawNode(int x, int y, BinaryTreeNode<T> node, int nodeLevel, int maxDepth, int connectX = -1, int connectY = -1,)
{
//calculate distance between the node's children
int distance = CalculateDistance(nodeLevel, maxDepth);
//draw the node at the specified coordinate
node.Draw(x, y, this.device);
if (node.Left != null)
{
DrawNode(x - distance/2, y + 50, node.Left, nodeLevel + 1, maxDepth, x, y, node);
}
if (node.Right != null)
{
DrawNode(x + distance/2, y + 50, node.Right, nodeLevel + 1, maxDepth, x, y, node);
}
//connect the node to its parent
if ((connectX != -1) && (connectY != -1))
{
node.Connect(connectX, connectY, device);
}
this.display.Image = surface;
}
私のオリジナルのアイデアは、句あれば、単純に最初の二つのそれぞれの内部でのThread.sleep(1000)を置くことだった - 私は本当に行うために必要なすべてが1のためのプログラムの実行を一時停止しましたノードの各描画の前に2番目に
スリープメソッドが描画コードの実行をブロックしていることに気がついたので、私はそのメソッドをあきらめました。次に、タイマーを使用しようとしましたが、ツリーを扱うときには難しくありませんでした。
私の目標は、単にGUIの応答性を中断することなく、かつ過度のコードを複雑にすることなく、プログラムの実行を一時停止する方法を見つけるために..です
任意の助けいただければ幸いです:)。
編集:潜在的に関連性のある情報:プログラムはWinformsで動作し、すべてのグラフィックスはGDI +で処理されます。あなたが他の情報が必要な場合は、ちょうど:)
編集を尋ねる:SLaksについて、
//draw the node's children
if (drawChildren)
{
if (node.Left != null)
{
if (this.timer2.Enabled)
{
this.timer2.Stop();
}
if (!this.timer1.Enabled)
{
this.timer1.Start();
}
this.count1++;
this.timer1.Tick += (object source, EventArgs e) =>
{
this.count1--;
DrawNode(x - distance/2, y + 50, node.Left, nodeLevel + 1, maxDepth, x, y, node);
if (this.count1 == 0)
{
this.timer1.Stop();
}
};
}
else
{
this.timer1.Stop();
this.timer2.Start();
}
if (node.Right != null)
{
this.count2++;
this.timer2.Tick += (object source, EventArgs e) =>
{
this.count2--;
DrawNode(x + distance/2, y + 50, node.Right, nodeLevel + 1, maxDepth, x, y, node);
if (this.count2 == 0)
{
this.timer2.Stop();
}
};
}
}
タイマーを使用する必要があります。何が問題になったのですか? – SLaks
C#5で 'await Task.Delay(...)'を使用することもできます – SLaks
アニメーションがポストオーダーである必要があるためタイマーが機能しませんでした。タイマーで動作させる方法が見つかりませんでした私は2つ持っていた)。あなたがそれを働かせる方法を見つけることができるなら、コードはあります: 編集:文字の制限はここにコードを置くのを防ぎます、私は元の投稿に入れます。 – Daniel