2011-10-30 27 views
1

出于某种原因,我的代码不会绘制图上的第二个点。我从WindowsForm上的两个不同文本框中获取x,y值。我有一个包含x,y坐标的类。每次在文本框中输入新值时,我都会创建一个新对象,将两个新坐标添加到对象中并将该对象添加到列表中。绘图点

最后,我遍历对象列表并尝试绘制每个x,y坐标。为什么它没有显示所有的要点?

这里是我的代码:

Form1中

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace PlotGraph 
{ 
public partial class Form1 : Form 
{ 
    private List<TheList> allValuesList = new List<TheList>(); 

    private int x = 240; // the position of the X axis 
    private int y = 0; // the position of the Y axis 
    public static Bitmap bmp = new Bitmap(360, 390); 
    public static Graphics g = Graphics.FromImage(bmp); 

    public Form1() 
    { 
     InitializeComponent(); 
     this.SetStyle(ControlStyles.ResizeRedraw, true); 
     g.DrawLine(new Pen(Color.Red, 2), 5, 5, 5, 250); 
     g.DrawLine(new Pen(Color.Red, 2), 5, 250, 300, 250); 
    } 

    private void btnPlotGraph_Click(object sender, EventArgs e) 
    { 
     TheList latestCoordinate = new TheList(); 
     if (textBoxX != null) 
     { 
      latestCoordinate.xCoordinate = Int16.Parse(textBoxX.Text); 

     } 

     if (textBoxY != null) 
     { 
      latestCoordinate.yCoordinate = Int16.Parse(textBoxY.Text); 
     } 

     allValuesList.Add(latestCoordinate); 
     plotTheValues(allValuesList); 
    } 

    public void plotTheValues(List<TheList> allValuesList) 
    { 
     Int16 x1 = 0; 
     Int16 y1 = 0; 
     foreach (TheList val in allValuesList) 
     { 
      x1 = val.xCoordinate; 
      y1 = val.yCoordinate; 
      g.DrawString("X", new Font("Calibri", 12), new SolidBrush(Color.Black), y + y1, x - x1); 

      PictureBox display = new PictureBox(); 
      display.Width = ClientRectangle.Width; 
      display.Height = ClientRectangle.Height; 
      this.Controls.Add(display); 
      display.Image = bmp; 
     } 
    } 
} 

}

类的thelist

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace PlotGraph 
{ 
    public class TheList 
    { 
     public Int16 xCoordinate = -1; 
     public Int16 yCoordinate = -1; 
    } 
} 
+0

你已经接受了一个答案,所以只是留下你的问题。如果你抹掉它的内容,答案将是无用的。 – BoltClock

回答

2

我不明白你为什么重绘每一个ITE米列表相同的图像(BMP),在相同的位置...

public void plotTheValues(List<TheList> allValuesList) 
{ 
    foreach (TheList val in allValuesList) 
    { 
     Int16 x1 = val.xCoordinate; 
     Int16 y1 = val.yCoordinate; 
     g.DrawString("X", 
        new Font("Calibri", 12), 
        new SolidBrush(Color.Black), 
        y + y1, x - x1); 
     g.DrawImage(bmp,...); // It's really faster and memory saving 
     // Are you sure you don't need to plot the image bmp 
     // at item coordinates instead of always at the same position? 
    } 
} 
+0

哇...我真笨。谢谢,你刚刚间接地解决了我的问题哈哈。 – BigBug

+0

@BlueMonster:间接吗?我直接想,告诉你你每次都在同一个位置重绘相同的图像;)无论如何,我很高兴我帮你解决了麻烦! :) – Marco

+0

哦!你是对的:o)你直接回答了我的Q. Upvoted并被选中。再次感谢.. – BigBug

1

我认为你需要一些显示逻辑转移到你的构造:

private int x = 240; // the position of the X axis 
    private int y = 0; // the position of the Y axis 
    public Bitmap bmp; 
    public Graphics g; 
    PictureBox display = new PictureBox(); 

    public Form1() 
    { 
     InitializeComponent(); 
     this.SetStyle(ControlStyles.ResizeRedraw, true); 

     bmp = new Bitmap(360, 390); 
     g = Graphics.FromImage(bmp); 

     g.DrawLine(new Pen(Color.Red, 2), 5, 5, 5, 250); 
     g.DrawLine(new Pen(Color.Red, 2), 5, 250, 300, 250); 

     display = new PictureBox(); 
     display.Width = ClientRectangle.Width; 
     display.Height = ClientRectangle.Height; 

     this.Controls.Add(display); 
    } 

然后plotTheValues样子这个:

public void plotTheValues(List<Point> allValuesList) 
    { 
     foreach (Point val in allValuesList) 
     { 
      g.DrawString("X", new Font("Calibri", 12), new SolidBrush(Color.Black), x - val.X, y - val.Y); 
     } 

     display.Image = bmp; 
    }