C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 31 を表示中)
■14461 / )  Re[15]: パネル内の座標値、そして座標値の範囲を設定す
□投稿者/ Tetsu (14回)-(2008/02/19(Tue) 10:57:51)
Tetsu さんの Web サイト
2008/02/19(Tue) 10:59:08 編集(投稿者)
2008/02/19(Tue) 10:58:24 編集(投稿者)

<pre><pre>魔界の仮面弁士さん

ご回答ありがとうございます。早速、

> No14366 の私の投稿では、そうした同名変数はわかりにくいので、
> >> private float xOneInit = 50.01f;
> >> void panel2_Paint(object sender, PaintEventArgs e)
> >> {
> >>  float xOne = xOneInit;
> のように、あえて、それぞれ異なる変数名にしています。

のアドバイスをもとに、変数名を変えてみました。そうしたところ、「変数が使われていない」というメッセージはなくなりました。
初歩的なミスです。すみません。
まだC#、そしてオブジェクト指向プログラミングというものを始めて1ヶ月なのと、
周りに誰も質問できる人がいない状況で、この掲示板でご迷惑をおかけしてしまっていると思います。

>>英語のメッセージなのですが、
> 英語版の Visual Studio をお使いですか?

はい。現在アメリカに住んでおり、英語版のVisual Stuio 2005を使っています。


> ループ中で、
>  e.Graphics.DrawLines(pen, myPoints);
> を行っていますが、この myPoints の座標を、ループ中では変更していないからです。
> そのため、同じ座標に対して 3回重ね描きしていることになっています。

これについてですが、以下のようにコードを書き直したところ、ちゃんと3本の折れ線が描かれました。
こちらも単純なミスでした。
点座標を配列に格納する部分をループの中に入れました。

                //折れ線に使用する3点をPointF[]に格納する
                PointF[] myPoints = new PointF[3];
                myPoints[0] = new PointF(xOne, yOne);
                myPoints[1] = new PointF(xTwo, yTwo);
                myPoints[2] = new PointF(xThree, yThree);

あとは再描画の問題のみなのですが、画面をスクロールしてから再度ボタンを押すと、再描画されることがわかりました。
スクロール時に自動的に再描画されるようにするには、スクロールバーの部分を設定する(HScrollBar/VScrollBar コントロール)
必要があるということになるのでしょうか。
ループの後にxOne〜yThreeの値をxOneInit〜yThreeInitに戻してみましたが、変化はありませんでした。



【Form1のコード】
    public partial class Form1 : Form
    {
        DrawingMOD myDraw; //Form1で共通に使用するオブジェクト変数の宣言

        //描画に使用する3点の座標 (データベースからクエリされたものと仮定)
        private float xOneInit = 50.01f;
        private float yOneInit = 100.01f;
        private float xTwoInit = 100.01f;
        private float yTwoInit = 100.01f;
        private float xThreeInit = 120.01f;
        private float yThreeInit = 120.01f;

        public Form1()
        {
            InitializeComponent();
            this.myDraw = new DrawingMOD(this.panel2);
            myDraw.DrawPanel += new DrawOnPanel(myDraw_DrawPanel);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.myDraw.DrawMovingObjects();
        }

        //myDraw_DrawPanelイベントハンドラ
        private void myDraw_DrawPanel(object sender, PaintEventArgs e)
        {

            //パネルのインスタンスを作成
            Panel panel2 = sender as Panel;
            //this.panel2.AutoScroll = true;
            this.panel2.AutoSize = true;

            //set the background color
            e.Graphics.Clear(Color.Black);

            //Initialize the coordinates
            float xOne = xOneInit;
            float yOne = yOneInit;
            float xTwo = xTwoInit;
            float yTwo = yTwoInit;
            float xThree = xThreeInit;
            float yThree = yThreeInit;
  
            //ペンの色を定義
            int cRed = 200;
            int cGreen = 100;
            int cBlue = 100;

            Pen pen = new Pen(Color.FromArgb(cRed, cGreen, cBlue), 4); //ペンを作成
            pen.EndCap = LineCap.ArrowAnchor; //折れ線を矢印にする

            for (int i = 0; i < 3; i++)
            {
                //折れ線に使用する3点をPointF[]に格納する
                PointF[] myPoints = new PointF[3];
                myPoints[0] = new PointF(xOne, yOne);
                myPoints[1] = new PointF(xTwo, yTwo);
                myPoints[2] = new PointF(xThree, yThree);

                e.Graphics.DrawLines(pen, myPoints); //折れ線を描く

                //change the location for another line
                xOne += 200f;
                yOne += 200f;
                xTwo += 200f;
                yTwo += 200f;
                xThree += 200f;
                yThree += 200f;

                //change the color of lines
                cRed += 20;
                cGreen += 40;
                cBlue += 20;

            }

            xOne = xOneInit;
            yOne = yOneInit;
            xTwo = xTwoInit;
            yTwo = yTwoInit;
            xThree = xThreeInit;
            yThree = yThreeInit;
        }

    }

</pre></pre>

返信 編集キー/


管理者用

- Child Tree -