Новичок
Профиль
Группа: Участник
Сообщений: 12
Регистрация: 12.11.2008
Где: АР Крым
Репутация: нет Всего: нет
|
Partizan, я проверил пошагово, вроде нормально, и цвета изменяет, а вот рисует все равно так.... одним цветом Шлю весь проект извините, из-за глюков интернета, шлю только код, загрузить не могу Код | using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Threading; using System.Windows.Forms; using ZedGraph; namespace NearestNeighbor { public partial class Form1 : Form { private bool n = true; private bool ColAdded; private const string Z = ""; private const string AddFeature = ""; private const int marging = 12; private const int i = 1;
private readonly Color[] colors = new Color[] {Color.Red, Color.Blue, Color.Green, Color.Yellow, Color.Cyan, Color.Black, Color.Gold}; private int[,] points; private Dictionary<int, Color> сolorClass; private Dictionary<int, PointPair> pointClass; private Dictionary<int, PointPairList> listPreparedItems; #region FormLoad public Form1() { MainMenu MyMenu = new MainMenu(); // Добавляем в это меню элемент верхнего уровня. MenuItem ml = new MenuItem("File"); MyMenu.MenuItems.Add(ml); MenuItem subml = new MenuItem("Open Ctrl+O"); ml.MenuItems.Add(subml); MenuItem subm7 = new MenuItem("Save Ctrl+S"); ml.MenuItems.Add(subm7); MenuItem subm3 = new MenuItem("Exit Alt+X"); ml.MenuItems.Add(subm3);
MenuItem m2 = new MenuItem("Edit"); MyMenu.MenuItems.Add(m2); MenuItem subm6 = new MenuItem("Add"); m2.MenuItems.Add(subm6); MenuItem subm4 = new MenuItem("Clear Ctrl+R"); m2.MenuItems.Add(subm4); MenuItem subm5 = new MenuItem("Delete Ctrl+D"); MenuItem subm9 = new MenuItem("Add Item Ctrl+Shift+I"); m2.MenuItems.Add(subm9); MenuItem subm10 = new MenuItem("Delete Item Ctrl+Shift+D"); m2.MenuItems.Add(subm10);
MenuItem m3 = new MenuItem("Help"); MyMenu.MenuItems.Add(m3); MenuItem subm11 = new MenuItem("Hot Keys"); m3.MenuItems.Add(subm11); MenuItem subm8 = new MenuItem("About"); m3.MenuItems.Add(subm8);
m2.MenuItems.Add(subm5); subm3.Click += MMExitClick; // subm6.Click += MMAddClick; subm4.Click += MMClearClick; subm5.Click += MMDeleteClick; subml.Click += MMOpenClick; subm7.Click += MMSaveClick; subm8.Click += MMAboutClick; subm11.Click += MMHotKeysClick; Menu = MyMenu; InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MakeWindow(zg1); SetSize(); zg1.Left = zg1.Top = marging; zg1.Size = new Size(tbpNNeighbor1.Size.Width - 2 * marging, tbpNNeighbor1.Size.Height - 2 * marging); }
private static void MakeWindow(ZedGraphControl zgc) { GraphPane myPane = zgc.GraphPane; myPane.Title.Text = "Graphic"; myPane.XAxis.Title.Text = "X1"; myPane.YAxis.Title.Text = "X2"; } #endregion
#region Method of K-Neares Neighbors
/* private void Swap(int FElem, int SecElem) { int tmp = points[FElem, 1]; points[FElem, 1] = points[SecElem, 1]; points[SecElem, 1] = tmp; }
public void BubbleSort() { int i, j;
for (i = 1; i < N; ++i) { for (j = 0; j < N - i; ++j) { if (points[j, 1] > points[j + 1, 1]) { Swap(j, j + 1); } } } }*/
private void KnearestNeughbors() { int RowCount = dgvTraining.Rows.Count - 2; double K = 0; K = double.Parse(this.txtK.Text);
for (int j = 0; j < points.Length; j++) { double x1, y1, x2, y2; double[] R = new double[RowCount];
for (int i = 0; i < RowCount; i++) { if ((string)dgvTraining.Rows[RowCount].Cells[0].Value == null) { x1 = Convert.ToInt32(dgvTraining.Rows[RowCount].Cells[1].Value); y1 = Convert.ToInt32(dgvTraining.Rows[RowCount].Cells[2].Value);
x2 = Convert.ToInt32(dgvTraining.Rows[i].Cells[1].Value); y2 = Convert.ToInt32(dgvTraining.Rows[i].Cells[2].Value);
R[i] = Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));
Array.Sort(R); } } } } #endregion
#region Make Graphics
private void CreateGraph(ZedGraphControl zgc) { int currentRow = dgvTraining.Rows.Count-2; GraphPane myPane = zgc.GraphPane; int numberOfColor = 0; points = new int[currentRow + 1, 3];
PointPairList listPointsForAllClasses = new PointPairList(); PointPairList listPointsForCurrentClass = new PointPairList(); pointClass = new Dictionary<int, PointPair>(); listPreparedItems = new Dictionary<int, PointPairList>();
//-------Внесение в массив ------------------------- сolorClass = new Dictionary<int, Color>(); if ((string)dgvTraining.Rows[currentRow + 1].Cells[0].Value != "") { for (int j = 0; j < currentRow + 1; ++j) { for (int q = 0; q <= 2; q++) { points[j, q] = Convert.ToInt32(dgvTraining.Rows[j].Cells[q].Value.ToString()); } if (!сolorClass.ContainsKey(points[j, 0]) && numberOfColor < colors.Length) { сolorClass.Add(points[j, 0], colors[numberOfColor]); numberOfColor++; } else if (numberOfColor > colors.Length) { throw new ApplicationException("Count of classes exceeded the limit!"); } } listPointsForAllClasses.Add(points[currentRow, 1], points[currentRow, 2]); pointClass.Add(points[currentRow, 0], new PointPair(points[currentRow, 1], points[currentRow, 2]));
/*foreach (int klass1 in сolorClass.Keys) { foreach(KeyValuePair<int, PointPair> kvp in pointClass) { if(klass1 == kvp.Key) { listPointsForCurrentClass.Add(kvp.Value.X, kvp.Value.Y); } } listPreparedItems.Add(klass1, listPointsForCurrentClass); // listPointsForCurrentClass.Clear(); }*/ foreach (int klass1 in сolorClass.Keys) { foreach (KeyValuePair<int, PointPair> kvp in pointClass) { if (klass1 == kvp.Key) { listPointsForCurrentClass.Add(kvp.Value.X, kvp.Value.Y); } } listPreparedItems.Add(klass1, listPointsForCurrentClass); listPointsForCurrentClass = new PointPairList(); } } /*
foreach (KeyValuePair<string, object> MyDictionarys in MyDictionary) { foreach (KeyValuePair<int, Color> color in сolorClass) { myPane.AddCurve("", listPreparedItem.Value, color.Value, SymbolType.Circle); myPane.AddCurve("", (IPointList)MyDictionarys.Value, color.Value, SymbolType.Circle); } }
*/
foreach (KeyValuePair<int, PointPairList> listPreparedItem in listPreparedItems) { foreach (KeyValuePair<int, Color> color in сolorClass) { myPane.AddCurve("", listPreparedItem.Value, color.Value, SymbolType.Circle); } } zgc.AxisChange(); zgc.Refresh(); сolorClass.Clear(); }
/* public object CloneObjectInMemory(object MyObject) { using (MemoryStream MemoryStream = new MemoryStream()) { { new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(MemoryStream, MyObject); MemoryStream.Position = 0; return new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Deserialize(MemoryStream); } } } */
//------------------- /*class Point_ { private double x; private double y; //private double listPointForCurentClass;
public Point_ (Point_ p) { this.x = p.listPointForCurentClass; this.y = p.y; } }*/ //------------------------
private void Form1_Resize(object sender, EventArgs e) { SetSize(); } private void SetSize() { zg1.Location = new Point(10, 10); }
protected void dgvTraining_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0) { return; } if (dgvTraining.Rows[e.RowIndex].Cells[1].Value != null && dgvTraining.Rows[e.RowIndex].Cells[2].Value != null) { CreateGraph(zg1); SetSize(); //zg1.Left = zg1.Top = marging; //zg1.Size = new Size(tbpNNeighbor1.Size.Width - 2 * marging, tbpNNeighbor1.Size.Height - 2 * marging); } } #endregion
#region MainMenuEvents
protected void MMDeleteClick(object sender, EventArgs e) { if (n == false) { dgvTraining.Rows.Remove(dgvTraining.Rows[0]); } else { dgvTraining.Rows.Remove(dgvTraining.Rows[1]); } }
protected void MMClearClick(object sender, EventArgs e) { dgvTraining.Rows.Clear(); }
protected void MMOpenClick(object sender, EventArgs e) { OpenFileDialog oDialog = new OpenFileDialog(); if (oDialog.ShowDialog() == DialogResult.OK) { oDialog.Filter = string.Format("{0}", "TXT File Format(*.txt)|*.txt"); StreamReader sr = new StreamReader(oDialog.FileName); dgvTraining.Rows[i].Cells[0].Value = sr.ReadToEnd(); sr.Close(); } }
protected void MMSaveClick(object sender, EventArgs e) { SaveFileDialog sDialog = new SaveFileDialog(); if (sDialog.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(sDialog.FileName); sDialog.Filter = string.Format("{0}", "TXT File Format(*.txt)|*.txt"); sw.Write(dgvTraining.Rows[i].Cells[0].Value); sw.Close(); } }
protected void MMExitClick(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Are you sure want to exit?", "Exit question", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) Application.Exit(); }
protected void MMAboutClick(object sender, EventArgs e) { MessageBox.Show("This application was created by \n Servin Osmanov, \n the student of Tavrida National University.\n\n All rights reserved, 2008", "About author"); }
protected void MMAddItemClick(object sender, EventArgs e) { if (ColAdded == false) {
dgvTraining.Columns.Add(Z, "Z"); dgvTraining.Columns[Z].Width = 65; ColAdded = true; } }
protected void MMDeleteItemClick(object sender, EventArgs e) { dgvTraining.Columns.Remove(Z); ColAdded = false; }
private void MMHotKeysClick(object sender, EventArgs e) { MessageBox.Show(" Ctrl+O - Open File \n Ctrl+S - Save into File \n Ctrl+D - Delete Row \n Ctrl+R - Clear Rows \n Ctrl+Shift+I - Incert Column \n Ctrl+Shift+D - Delete Column", "Hot Keys"); } #endregion
#region ButtonMethods private void btnExit_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Are you sure you want to exit?", "Exit question", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) Application.Exit(); }
private void btnClear_Click(object sender, EventArgs e) { dgvTraining.Rows.Clear(); zg1.Refresh(); //pict.Refresh(); }
private void btnDelete_Click(object sender, EventArgs e) { if (dgvTraining.Rows.Count == 0) { return; } else { if (n) { dgvTraining.Rows.Remove(dgvTraining.Rows[0]); n = false; } else { dgvTraining.Rows.Remove(dgvTraining.Rows[1]); n = true; } }
}
private void btnAddItem_Click(object sender, EventArgs e) {
int i = dgvTraining.Columns.Count; if (i == 0) { dgvTraining.Columns.Add(AddFeature, "Class"); dgvTraining.Columns[i].Width = 60; } else { dgvTraining.Columns.Add(AddFeature, "X" + i); dgvTraining.Columns[i].Width = 60; } if (i >= 3) { tbcNNeighbors.SelectedIndex = 1; } }
private void btnDeleteItem_Click(object sender, EventArgs e) { int i = dgvTraining.Columns.Count; if (i > 3) { int CellCount = dgvTraining.Columns.Count - 1; dgvTraining.Columns.Remove(dgvTraining.Columns[CellCount]); } } #endregion
#region HotKeys /*
private void HotKeyOpen(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode.Equals(Keys.O)) { OpenFileDialog oDialog = new OpenFileDialog(); if (oDialog.ShowDialog() == DialogResult.OK) { oDialog.Filter = string.Format("{0}", "TXT File Format(*.txt)|*.txt"); StreamReader sr = new StreamReader(oDialog.FileName); dgvTraining.Rows[i].Cells[0].Value = sr.ReadToEnd();
sr.Close(); } }
}
private void HotKeySave(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode.Equals(Keys.S)) { SaveFileDialog sDialog = new SaveFileDialog(); if (sDialog.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(sDialog.FileName); sDialog.Filter = string.Format("{0}", "TXT File Format(*.txt)|*.txt"); sw.Write(dgvTraining.Rows[i].Cells[0].Value); sw.Close(); } }
}
private void HotKeyInsertColumn(object sender, KeyEventArgs e) { int i = dgvTraining.Columns.Count; if (i == 0) { dgvTraining.Columns.Add(AddFeature, "Class"); n = false; } else { dgvTraining.Columns.Add(AddFeature, "X" + i); } }
private void HotKeyDeleteColumn(object sender, KeyEventArgs e) { int CellCount = dgvTraining.Columns.Count - 1; dgvTraining.Columns.Remove(dgvTraining.Columns[CellCount]); }
private void HotKeyDelete(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode.Equals(Keys.D)) { if (!n) { dgvTraining.Rows.Remove(dgvTraining.Rows[0]); n = false; } else { dgvTraining.Rows.Remove(dgvTraining.Rows[1]); n = true; } } }
private void HotKeyClear(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode.Equals(Keys.R)) { dgvTraining.Rows.Clear(); } } */
#endregion } }
|
|