如何以不同的颜色绘制每个点?

时间:2012-03-29 18:29:39

标签: c#

这是我点击点的鼠标代码:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      label1.Text = e.X.ToString();
      label2.Text = e.Y.ToString();
      label1.Visible = true;
      label2.Visible = true;
      label3.Visible = true;
      label4.Visible = true;
      // find the index that is closest to the current mouse location
      MinDist = float.MaxValue;

      for (idx = 0; idx < Point_X.Count; ++idx)
      {
         float dx = Point_X[idx] - e.X;
         float dy = Point_Y[idx] - e.Y;
         float dist = (float)Math.Sqrt(dx * dx + dy * dy);

         if (dist < MinDist)
         {
            MinDist = dist;
            selectedIndex = idx;
         }
      }

      if (MinDist < 5)
      {
         mouseMove = true;
         OriginalX = Point_X[(int)selectedIndex];
         OriginalY = Point_Y[(int)selectedIndex];

         if (cyclicSelectedIndex.Count() == 2)
         {
            cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
            currentCyclicIndex++;
            if (currentCyclicIndex > 1)
            {
               currentCyclicIndex = 0;
            }
            if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) ||
                (cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))
            {
               button2.Enabled = false;
            }
            else
            {
               button2.Enabled = true;
            }

            label13.Text = selectedIndex.ToString();
            label13.Visible = true;
            label14.Visible = true;

            listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
         }
      }
   }
}

这是第一个FOR LOOP中的绘制事件我在第二个FOR LOOP中绘制点我画点之间的线。

我希望如果我点击一个点它将是蓝色,如果我点击第二个点它将是黄色。现在,当我创建积分时,它们是红色的。

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
   Point connectionPointStart;
   Point connectionPointEnd;
   Graphics g = e.Graphics;
   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
   SolidBrush brush = new SolidBrush(Color.Red);
   Pen p = new Pen(brush);
   for (int idx = 0; idx < Point_X.Count; ++idx)
   {
      Point dPoint = new Point((int)Point_X[idx], (int)Point_Y[idx]);
      dPoint.X = dPoint.X - 5; // was - 2
      dPoint.Y = dPoint.Y - 5; // was - 2
      Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
      g.FillEllipse(brush, rect);
   }

   for (int i = 0; i < connectionStart.Count; i++)
   {
      int startIndex = connectionStart[i];
      int endIndex = connectionEnd[i];

      connectionPointStart = new Point(
         (int)Point_X[startIndex], (int)Point_Y[startIndex]);
      connectionPointEnd = new Point(
         (int)Point_X[endIndex], (int)Point_Y[endIndex]);
      p.Width = 4;
      g.DrawLine(p, connectionPointStart, connectionPointEnd);
   }
}

1 个答案:

答案 0 :(得分:1)

您需要修改用于绘制每个点的画笔。这是一个简单的解决方案。

首先,在类中添加另一个名为int的previousSelectedIndex的变量,并用-1初始化它。然后,在Mouse Down事件中,添加如下所示的行:

     ...
     if (dist < MinDist)
     {
        MinDist = dist;
        previouslySelectedIndex = selectedIndex  // <--- Add this line
        selectedIndex = idx;
     }
     ...

现在,将下面提到的部分添加到Paint事件处理程序中的正确位置:

   ...
   Brush brush = Brushes.Red;  // <-Change the type from SolidBrush to Brush, which is more general
   ...
   for (int idx = 0; idx < Point_X.Count; ++idx)
   {
      ...

      // START OF NEW CODE

      brush = Brushes.Red;  // <-- This line was not in my original solution.

      if (idx==selectedIndex)  // <-- Add all this code
          brush = Brushes.Blue; 
      else if(idx==previouslySelectedIndex) 
          brush = Brushes.Yellow;

      // END OF NEW CODE

      g.FillEllipse(brush, rect);
   }
   ...
相关问题