だから私がやろうとしているのは、さまざまな色のパネルからランダムな画像のように作成することです. ユーザーは、必要なパネル (つまりピクセル) の数と異なる色の数を選択でき、プログラムがその画像を自動的に生成します。後でこの画像が必要になり、すべてのピクセルを変更する必要があるため、これにはパネルを使用したいと思います。私はパネルに慣れているので、パネルをそのままにして、他のものは使用したくありません。
したがって、このパネルを作成するために使用しているコードは次のとおりです。
//Creates two lists of panels
//Add items to list so that these places in the list can be used later.
//nudSizeX.Value is the user-chosen number of panels in x-direction
for (int a = 0; a < nudSizeX.Value; a++)
{
horizontalRows.Add(null);
}
//nudSizeY.Value is the user-chosen number of panels in y-direction
for (int b = 0; b < nudSizeY.Value; b++)
{
allRows.Add(null);
}
for (int i = 0; i < nudSizeY.Value; i++)
{
for (int j = 0; j < nudSizeX.Value; j++)
{
// new panel is created, random values for background color are assigned, position and size is calculated
//pnlBack is a panel used as a canvas on whoch the other panels are shown
Panel pnl = new Panel();
pnl.Size = new System.Drawing.Size((Convert.ToInt32(pnlBack.Size.Width)) / Convert.ToInt32(nudSizeX.Value), (Convert.ToInt32(pnlBack.Size.Height) / Convert.ToInt32(nudSizeY.Value)));
pnl.Location = new Point(Convert.ToInt32((j * pnl.Size.Width)), (Convert.ToInt32((i * pnl.Size.Height))));
//There are different types of panels that vary in color. nudTypesNumber iis the user-chosen value for howmany types there should be.
int z = r.Next(0, Convert.ToInt32(nudTypesNumber.Value));
//A user given percentage of the panels shall be free, i.e. white.
int w = r.Next(0, 100);
if (w < nudPercentFree.Value)
{
pnl.BackColor = Color.White;
}
//If a panel is not free/white, another rendom color is assigned to it. The random number determinig the Color is storede in int z.
else
{
switch (z)
{
case 0:
pnl.BackColor = Color.Red;
break;
case 1:
pnl.BackColor = Color.Blue;
break;
case 2:
pnl.BackColor = Color.Lime;
break;
case 3:
pnl.BackColor = Color.Yellow;
break;
}
}
//Every panel has to be added to a list called horizontal rows. This list is later added to a List<List<Panel>> calles allRows.
horizontalRows[j] = (pnl);
//The panel has also to be added to the "canvas-panel" pnl back. The advantage of using the canvas panel is that it is easier to determine the coordinates on this panel then on the whole form.
pnlBack.Controls.Add(pnl);
}
allRows[i] = horizontalRows;
}
ご想像のとおり、99x99 のチェッカーボードを作成する場合、プログラムはプロセスをほぼ 10000 回ループする必要があるため、これは非常に遅くなります。
パフォーマンスを向上させるために何をしますか? 私はパネルに慣れているので、パネルでやり続けたいと言いましたが、パネルを使用するのが思ったよりも馬鹿げている場合は、他のオプションを受け入れることができます. プログラムは、作成済みのパネルが増えるほど遅くなります。それは、リストに追加することで、ますます大きくなったためだと思いますか?
現在の出力は次のようになります。
これは、後で「写真」でやりたいことです。基本的には、シェリング モデルを実行したいと考えています。このモデルは、自分のグループに属している特定のパーセンテージの人々を周囲に持ちたい場合に、さまざまなグループ (つまり、さまざまな色) がどのように分離されるかを示しています。つまり、後で各パネル/ピクセルの隣にあるものを確認し、各ピクセルの色を個別に変更できるようにする必要があります。
すぐに解決できる解決策は必要ありません。画像作成プロセスの速度を向上させるためのヒントが欲しいだけです。
どうもありがとうございました