いろいろな形の描画

以下のプログラムは水色に塗りつぶした長方形と赤い線で描いた長方形を表示する。

プログラム1
    
        Dim g As System.Drawing.Graphics ' g を定義
        g = Me.CreateGraphics ' g を準備
        Dim pen1, pen2 As Pen   ' 描画用のペン p を定義
        Dim brush1 As Brush   ' 描画用のペン p を定義
        pen1 = Pens.Red ' 描画用のペン pの色を赤にする
        brush1 = Brushes.SkyBlue '塗りつぶしブラシを水色にする
        pen2 = Pens.Red ' 描画用のペン pの色を黒にする
        g.FillRectangle(brush1, 10, 20, 200, 40)
        g.DrawRectangle(pen1, 40, 10, 60, 100)

  

線の色は Pen で, 塗りつぶし色は Brush で指定する。

これ以外にいろいろな形が描画できる。

g.DrawRectangle (pen1, x, y, w, h)
	    
長方形
g.DrawEllipse(pen1, x, y, w, h)
長方形に内接する楕円
g.DrawArc(pen1, x, y, w, h, start, sweep)   

楕円の周囲 start度からsweep度の円弧

g.DrawPie(pen1, x, y, w, h, start, sweep)
楕円を start度からsweep度まで切り取った扇型
g.FillRectangle (brush1, x, y, w, h)
長方形を塗りつぶす
g.FillEllipse(brush1, x, y, w, h)     
楕円を塗りつぶす
g.FillPie(brush1,  x, y, w, h, start, sweep)
30度から270度の扇型を塗りつぶす

実際のプログラム例は下記の通り。以下のコードを「プログラム1」中に追記すれば描画する。

g.DrawRectangle (pen1,10, 10, 100,200)
	    
長方形
g.DrawEllipse(pen1, 10, 20, 100, 120)
        
長方形に内接する楕円
g.DrawArc(pen1, 10,10,200,250, 30,270)   

楕円の周囲 30度から270度の円弧

g.DrawPie(pen1, 10, 10, 200, 250, 30, 270)
楕円を 30度から270度まで切り取った扇型
g.FillRectangle (brush1, 10, 10, 20, 100)
長方形を塗りつぶす
g.FillEllipse(brush1, 10, 10, 100,30)        
楕円を塗りつぶす
g.FillPie(brush1,  10,10,200,250,30, 270)
30度から270度の扇型を塗りつぶす

Rectangle, PointF, Font

Rectangle, PointF, Pen, Brush, Fontなどの構造体の使い方をマスターすればより自由に描画することができる。

位置と大きさは Rectangleで指定できる。

PontF の配列を使えば、多数の点を指定できる。

以下に示すプログラムは 4つの点を指定し, その4つの点の座標を用いて閉曲線を描画するプログラムである。4つの点の座標はDim p(3) As PointF で定義した配列 p(3) に一旦代入してから描画に用いている。

  
    Dim g As System.Drawing.Graphics ' g を定義
    g = Me.CreateGraphics ' g を準備
    Dim pen1 As System.Drawing.Pen
    pen1 = New System.Drawing.Pen( _
        Color.FromArgb(&HFFFF0000))
    Dim p(3) As PointF
    Dim x
    For x = 10 To 200 Step 20
        p(0).X = x + 0 : p(0).Y = 100
        p(1).X = x + 20 : p(1).Y = 10
        p(2).X = x + 40 : p(2).Y = 40
        p(3).X = x + 60 : p(3).Y = 30
        g.DrawClosedCurve(pen1, p)
    Next
        

塗りつぶさない図形

点の配列を指定して、塗りつぶさない図形を各種描画することができる。

g.DrawPolygon(pen1, p)
多角形(輪郭)
g.DrawClosedCurve(pen1, p)
スプライン曲線
g.DrawCurve(pen1, p)
複数点を通る曲線
g.DrawLine(pen1, p(0), p(1))
p(0)とp(1)を結ぶ直線
g.DrawBezier(Pen1,p(0), p(1),p(2),p(3))
ベジエ曲線
g.DrawString("文字", font1, brush1, p(0))
文字を描画する

塗りつぶした図形

塗りつぶした図形を描画するメソッドは以下の通り

g.FillPolygon(brush1, p)
多角形
g.FillClosedCurve(pen1, p)
スプライン曲線

以下のプログラムは、塗りつぶした長方形を描画する。

現在コメントになっている部分を1つづつコメント解除するば、他の図形や文字を描画することができる。

 
    Dim g As System.Drawing.Graphics ' g を定義
    g = Me.CreateGraphics ' g を準備
    Dim brush1 As Brush
    brush1 = New SolidBrush( _
        Color.FromArgb(&HFFFF0000))
    Dim font1 As Font
    font1 = New Font("MS明朝", 11)
    Dim r1 As Rectangle
    Dim p(3) As PointF

    Dim x
    For x = 10 To 200 Step 10
        r1.X = x
        r1.Y = 20
        r1.Width = 8
        r1.Height = 20
        'g.DrawRectangle(brush1, r1)
        'g.DrawwEllipse(brush1, r1)
        'g.DrawPie(brush1, r1, 30.0, 300.0)
        'g.FillRectangle(brush1, r1)
        'g.FillEllipse(brush1, r1)
        'g.FillPie(brush1, r1, 30.0, 300.0)

        p(0).X = x + 0 : p(0).Y = 100
        p(1).X = x + 20 : p(1).Y = 0
        p(2).X = x + 40 : p(2).Y = 30
        p(3).X = x + 60 : p(3).Y = 20
        'g.FillPolygon(brush1, p)
        'g.FillClosedCurve(brush1, p)
        'g.DrawString("文字", font1, brush1, p(0))
    Next
  

三角形の描画

三角形は3点を指定して、DrawPolygonまたはFillPolygonで描画すればよい。

  
    Dim g As System.Drawing.Graphics ' g を定義
    g = Me.CreateGraphics ' g を準備
    Dim pen1 As System.Drawing.Pen
    pen1 = New System.Drawing.Pen( _
        Color.FromArgb(&HFFFF0000))
    Dim brush1 As Brush
    brush1 = New SolidBrush( _
        Color.FromArgb(&HFFFFFF00))
    Dim p(2) As PointF
    Dim x
    For x = 10 To 200 Step 20
        p(0).X = x + 0 : p(0).Y = 100
        p(1).X = x + 20 : p(1).Y = 10
        p(2).X = x + 40 : p(2).Y = 40
        If x < 100 Then
            g.DrawPolygon(pen1, p)
        Else
            g.FillPolygon(brush1, p)

        End If
    Next
        

Pen

        ' 線を描画する際の色などを指定する
        ' rr=赤, gg=緑, bb=青という配色の場合
        ' &HFFrrggbb という8桁の16進数で色を指定する
        Dim pen1 As System.Drawing.Pen
        pen1 = New System.Drawing.Pen( _
            Color.FromArgb(&HFFFF0000))
        g.DrawRectangle(pen1, 10, 10, 100, 100)

Brush

        ' 図形を塗りつぶす際の色を指定する
        ' rr=赤, gg=緑, bb=青という配色の場合
        ' &HFFrrggbb という8桁の16進数で色を指定する
        Dim brush1 As Brush
        brush1 = New SolidBrush( _
            Color.FromArgb(&HFFFF0080))
        Dim r1 As Rectangle
        r1.X = x
        r1.Y = 100
        r1.Width = 30
        r1.Height = 15
        g.FillRectangle(brush1, r1)

PointF

        ' x座標, y座標で座標を指定する
        ' x=20, y=30であれば、左から20ドット, 上から30ドット
        Dim p(2) As PointF
        p(0).X = x
        p(0).Y = 50
        p(1).X = 10 + x
        p(1).Y = 0
        p(2).X = 20 + x
        p(2).Y = 50
        g.DrawCurve(pen1, p)

Rectangle

        ' 長方形
        ' x,yで左上の点を指定する
        ' width, heightで幅と高さを指定する
        Dim r1 As Rectangle
        r1.X = x
        r1.Y = 100
        r1.Height = 10
        r1.Width = 20
        g.DrawRectangle(pen1, r1)

Font

        ' Font("フォント名", ポイント数)
        Dim font1 As Font
        font1=New Font("Ariel",16)
        g.DrawString("文字", font1, brush1, p(1))