1

私は Windows フォーム アプリケーションを使用しており、1 つのボタンを使用して乱数を生成し、フォームに描画しています。ボタンがクリックされると、 Graphics.Drawing メソッドを使用して乱数を追加しています。問題は、最初にボタンを押して正常に動作し、乱数、つまり 11111 を追加したときです。もう一度ボタンを押すと、(次の位置に) 新しい乱数が追加されますが、前の数字も新しく生成された乱数に変更されます。

更新: (完全なコードを追加)

編集:ランダムをスクープの外に移動したため、同じ番号は生成されませんが、古い乱数を他の乱数に変更しています。

メインクラス:

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

namespace DrawingText
{
    public partial class Form1 : Form
    {

        private Point mouseDownPosition = new Point(0, 0);
        private Point mouseMovePosition = new Point(0, 0);
        private int mousePressdDown;
        private ArrayList drawnItemsList;
        Random rnd;

        public Form1()
        {
            InitializeComponent();
            drawnItemsList = new ArrayList();
            this.rnd = new Random();

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseMovePosition = e.Location;
            if (e.Button == MouseButtons.Left)
                mousePressdDown = 1;

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                mouseDownPosition = e.Location;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mousePressdDown == 1)
            {
                label1.Text = "X: " + mouseMovePosition.X.ToString();
                label2.Text = "Y: " + mouseMovePosition.Y.ToString();
                this.Invalidate();
            }
            DrawingData a = new DrawingData(mouseMovePosition, mouseDownPosition);
            drawnItemsList.Add(a);
            mousePressdDown = 0;

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (DrawingData a in drawnItemsList)
            {
                draw(e.Graphics, a.old, a.cur);

            }
         draw(e.Graphics, mouseDownPosition, mouseMovePosition);


        }
        private void draw(Graphics e, Point mold, Point mcur)
        {
            Pen p = new Pen(Color.Black, 2);

                    using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
                    {
                            string header2 = rnd.Next().ToString();
                            RectangleF header2Rect = new RectangleF();
                            int moldX = mold.X - 5;
                            int moldY = mold.Y;

                            header2Rect.Location = new Point(moldX, moldY);
                            header2Rect.Size = new Size(600, ((int)e.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
                            e.DrawString(header2, useFont, Brushes.Black, header2Rect);
                        }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

図面データ クラス:

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

namespace DrawingText
{
    [Serializable]
    class DrawingData
    {
        private Point mold; // mouseDown position
        private Point mcur; // mouseUp poslition

        public DrawingData()
        {
            mold = new Point(0, 0);
            mcur = new Point(0, 0);
        }
        public DrawingData(Point old, Point cur)
        {
            mold = old;
            mcur = cur;
        }

        public Point old
        {
            get
            {
                return mold;
            }
            set
            {
                mold = value;
            }
        }

        public Point cur
        {
            get
            {
                return mcur;
            }
            set
            {
                mcur = value;
            }
        }


    }
}

ボタンを 3 回クリックすると、古い値が新しい値に置き換えられます。 ここに画像の説明を入力

4

3 に答える 3

1

DrawingData次のように、ランダム値をポイント値とともにクラスに保存する必要があります。

メインクラス:

namespace DrawingText
{
    public partial class Form1 : Form
    {
        private Point mouseDownPosition = new Point(0, 0);
        private Point mouseMovePosition = new Point(0, 0);
        private int mousePressdDown;
        private ArrayList drawnItemsList;
        Random rnd;

        public Form1()
        {
            InitializeComponent();
            drawnItemsList = new ArrayList();
            this.rnd = new Random();
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mousePressdDown == 1)
            {
                label1.Text = "X: " + mouseMovePosition.X.ToString();
                label2.Text = "Y: " + mouseMovePosition.Y.ToString();
                this.Invalidate();
            }
            DrawingData a = new DrawingData(mouseMovePosition, mouseDownPosition, rnd.Next().ToString());
            drawnItemsList.Add(a);
            mousePressdDown = 0;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (DrawingData a in drawnItemsList)
            {
                draw(e.Graphics, a);
            }
            draw(e.Graphics, mouseDownPosition, mouseMovePosition);
        }

        private void draw(Graphics e, DrawingData a)
        {
            Pen p = new Pen(Color.Black, 2);

            using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
            {
                RectangleF header2Rect = new RectangleF();
                int moldX = a.old.X - 5;
                int moldY = a.old.Y;

                header2Rect.Location = new Point(moldX, moldY);
                header2Rect.Size = new Size(600, ((int)e.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
                e.DrawString(a.Rand, useFont, Brushes.Black, header2Rect);
            }
        }
    }
}

図面データ クラス:

namespace DrawingText
{
    [Serializable]
    public class DrawingData
    {
        private Point mold; // mouseDown position
        private Point mcur; // mouseUp poslition
        private string randValue; // random data value

        public DrawingData()
        {
            mold = new Point(0, 0);
            mcur = new Point(0, 0);
            randValue = String.Empty;
        }

        public DrawingData(Point old, Point cur, string rand)
        {
            mold = old;
            mcur = cur;
            randValue = rand;
        }

        public Point old
        {
            get
            {
                return mold;
            }
            set
            {
                mold = value;
            }
        }

        public Point cur
        {
            get
            {
                return mcur;
            }
            set
            {
                mcur = value;
            }
        }

        public sting Rand
        {
            get
            {
                return randValue;
            }
            set
            {
                randValue = value;
            }
     }
}
于 2013-08-26T01:04:15.770 に答える
0

これは、グラフィックパスを使用してオンザフライで作成されます。

    GraphicsPath gp;
    int moldX = 10;
    int moldY = 10;

    public Form1()
    {
        InitializeComponent();
        gp = new GraphicsPath();  
    }


    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.FillPath(Brushes.Black, gp);
        // if you want the numbers outlined do e.Graphics.DrawPath
    }

    private void button1_Click(object sender, EventArgs e)
    {
        AddToPath();
        Invalidate();   
    }

    private void AddToPath()
    {
        using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
        {
            Random rnd = new Random();
            string header2 = rnd.Next().ToString();
            int strsize = TextRenderer.MeasureText(header2, useFont).Height;
            StringFormat format = StringFormat.GenericDefault;
            gp.AddString(header2, useFont.FontFamily, 1, 28, new Point(moldX, moldY), format);

            moldX += 5;
            moldY += strsize;
        }
    }
于 2013-08-26T00:17:55.503 に答える
0

ループ内で毎回ランダムを再作成しているため、同じシードと最初の番号が同じになります。そのため、すべての数値が同じです。あなたがすべき。

  1. ランダムをメソッドとループの外に移動し、代わりに使用します。行 Random rnd = new Random() を rnd = new Random() に変更します。クラスには、ランダムを保持するための変数が既にあります。

  2. 以前の乱数を前回と同じままにしたい場合は、それらをどこかにリストに保存し、ペイントで描画する必要があります。現在、毎回新しい乱数のセットを作成しています。

于 2013-08-26T00:00:32.457 に答える