Точки печати - PullRequest
       25

Точки печати

1 голос
/ 30 октября 2011

По какой-то причине мой код не будет отображать вторую точку на графике.Я собираю значения x, y из двух разных текстовых полей в WindowsForm.У меня есть класс, который содержит координаты х, у.Каждый раз, когда в текстовое поле вводится новое значение, я создаю новый объект, добавляю две новые координаты к объекту и добавляю объект в список.

Наконец, я перебираю список объектов и пытаюсь построить каждую координату 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 ]

2 голосов
/ 30 октября 2011

Я не понимаю, почему вы перерисовываете для каждого элемента в списке одно и то же изображение (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 голос
/ 30 октября 2011

Я думаю, вам нужно переместить некоторую логику отображения в ваш конструктор:

    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;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...