绘图点

时间:2011-10-30 08:26:34

标签: c# winforms list object plot

由于某种原因,我的代码不会在图表上绘制第二个点。我正在从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; 
    }
}

2 个答案:

答案 0 :(得分:2)

我不明白为什么你在同一个位置重新绘制同一个图像(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?
    }
}

答案 1 :(得分: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;
    }