그래서 내가하려는 것은 다른 색상의 패널에서 임의의 이미지를 생성하는 것입니다. 사용자는 원하는 패널 수 (픽셀 수)와 색상 수를 선택할 수 있으며, 프로그램은 자동으로 그 이미지를 생성합니다. 나중에이 그림이 필요하고 모든 단일 픽셀을 수정해야하기 때문에 패널을 사용하고 싶습니다. 패널에 익숙해지기 때문에 다른 것들은 사용하지 말고 보관하고 싶습니다.무작위로 생성 된 패널의 성능 개선
그래서 여기 내가이 패널을 만드는 데 사용하고 코드 : 당신이 상상할 수 있듯이 프로그램이 프로세스를 통해에 루프를 가지고 있기 때문에 99x99의 바둑판을 만들 때
//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;
}
, 이것은 매우 느립니다 거의 10000 타임스.
성능을 향상 시키려면 어떻게해야합니까? 나는 패널을 가지고 편안하게 지내기를 원한다고 말했다. 패널을 사용하는 것이 내가 생각했던 것보다 훨씬 바보라면, 나는 다른 옵션을 열어두고있다. 프로그램은 이미 생성 된 패널이 더 느려지고 느려집니다. 나는 그것이 커지면 커지는 목록에 추가하기 때문에 그런 것 같아?
이 출력 지금 모습입니다 :
이것은 내가 나중에 내 "사진"을 수행 할 것입니다 : 나는 기본적으로 Schellings 모델을하고 싶어. 이 모델은 자신의 그룹에 속한 사람들의 특정 비율을 원할 때 다른 그룹의 사람들 (즉, 서로 다른 색상)이 어떻게 분리되는지를 보여줍니다. 즉, 나중에 각 패널/픽셀에 대해 이웃이 무엇인지 확인하고 각 픽셀의 색상을 개별적으로 변경할 수 있어야합니다.
나는 준비된 해결책을 원하지 않는다. 나는 그림 생성 과정의 속도를 향상시키는 방법에 대한 조언만을 원하고있다.
당신 대신 당신이 필요로하는 당신의 색상과 다른 정보를 저장하는 데 사용하는 매트릭스를Panels
를 사용
GDI +를 사용하여 캔버스에 픽셀을 그립니다. –
GDI + (초보자)는 잘 모릅니다 만 튜토리얼을 보았는데 좋았습니다. 그러나 그려진 사각형을리스트에 어떻게 추가 할 수 있습니까? 아니면 (내가 말했듯이, 나는 GDI +를 모른다) 가능하지 않습니까? – tomet
당신은 정말로 * 정말로 * 그런 식으로하고 싶지 않습니다. 각 패널은 자체 핸들이있는 별도의 Windows 컨트롤이며 엄청난 오버 헤드가 있습니다! 당신의 직사각형을위한 작은 클래스 래퍼를 사용하십시오. –