I am trying to build a program in which the user can manually input the required matrix size of a grid of nxn panels. The panels then cycle through different shades of grey in such a manner where the first panel cycles through all the shades (ex: 5 different shades) for the next panel to increment one shade, eventually displaying all the possible combinations of shades within the grid. My issue is that I can't seem to get that function right.
I've started with a simpler problem of just one row of panels. further, I've developed a much simpler code in python that creates the required behavior for a list instead of panels:
import time
length = int(input("Enter length of the list: "))
my_list = \[0\]\*length
j=1
while True:
time.sleep(0.5)
if my_list[0] == 4:
my_list[0] = 0
for j in range(1,len(my_list)):
my_list[j]+=1
if my_list[j] % 4 != 0:
break
else:
my_list[j] = 0
print(my_list)
my_list[0] += 1
if j == len(my_list):
j=1
This will provide a list of 0's (number of zeros dictated by user) and will cycle through all the combinations of 0-3 for each item in the list. I essentially want to do the same thing with the different shades of grey panels but I can't quite figure it out and hoping someone can help. The issue lies in the **Timer1_Tick **segment:
using System;
using System.Drawing;
using System.Windows.Forms;
using WinFormsApp12;
namespace WinFormsApp12
{
public partial class Form1 : Form
{
private Panel\[\] panels; // Array to store panels
private int\[\] panelCurrentShades; // Array to store current shades for each panel
private int\[\] panelShades = new int\[\] { 0, 36, 73, 109, 146, 182, 219 }; // Shades of gray
private System.Windows.Forms.Timer timer1;
private int numPanels; // Number of panels input by user
public Form1(int numPanels)
{
this.numPanels = numPanels;
InitializeComponent();
InitializePanels();
InitializeTimer();
}
private void InitializePanels()
{
panels = new Panel[numPanels]; // Create panels array with size based on numPanels input
panelCurrentShades = new int[numPanels]; // Create panelCurrentShades array with size based on numPanels input
for (int i = 0; i < numPanels; i++)
{
Panel panel = new Panel
{
Size = new Size(100, 100),
Location = new Point(50 + i * 150, 50), // Position panels horizontally with a gap of 150 pixels
BackColor = GetColor(panelShades[0])
};
panels[i] = panel; // Add panel to panels array
Controls.Add(panel); // Add panel to form's controls
}
}
private void InitializeTimer()
{
timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 250; // Change the interval as desired
timer1.Tick += Timer1_Tick;
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < panels.Length; i++)
{
panelCurrentShades[i]++;
if (panelCurrentShades[i] == panelShades.Length)
{
panelCurrentShades[i] = 0;
}
panels[i].BackColor = GetColor(panelShades[panelCurrentShades[i]]);
if (panelCurrentShades[i] == 0)
{
// If the panel has cycled through all shades, update the next panel
int nextPanelIndex = (i + 1) % panels.Length; // Get index of next panel (wrap around)
panelCurrentShades[nextPanelIndex]++;
if (panelCurrentShades[nextPanelIndex] == panelShades.Length)
{
panelCurrentShades[nextPanelIndex] = 0;
}
panels[nextPanelIndex].BackColor = GetColor(panelShades[panelCurrentShades[nextPanelIndex]]);
}
}
}
private Color GetColor(int grayValue)
{
return Color.FromArgb(grayValue, grayValue, grayValue);
}
}
}