Plotting Points

本文关键字:Points Plotting | 更新日期: 2023-09-27 18:12:41

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

最后,i循环遍历对象列表并尝试绘制每个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; 
    }
}

Plotting Points

我不明白为什么你在相同的位置为列表中的每个项目重新绘制相同的图像(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?
    }
}

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

    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;
    }