冠冕堂皇

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

注意横向线条和纵向线条的规律,横向DrawLine(Pen, x1, y1 ,x2 ,y2),纵向规律是DrawLine(Pen,y1,x1,y2,x2)

代码如下:

 

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            ChessBoard cb = new ChessBoard(e.Graphics);
            cb.DrawBoard();
        }
    }

     class ChessBoard 
     { 
        private const int num = 15; 
        private const int initialPoint = 10; 
        private const int chessLength = 30; 
        private int[,] arr = new int[num, num];//0空,1黑,2白 
        private Graphics g; 
        private bool black = true; 
        private int stoneSize = 10;//棋子半径 
        public ChessBoard(Graphics g) 
        { 
           this.g = g; 
        } 
        //绘制棋盘 
        public void DrawBoard() 
        { 
           SolidBrush myBrush = new SolidBrush(Color.Orange); 
           Rectangle rect = new Rectangle(initialPoint, initialPoint, chessLength * (num - 1), chessLength * (num - 1)); 
           g.DrawRectangle(new Pen(myBrush), rect);//绘制棋盘背景 
           Pen myPen = new Pen(Color.Black, 2); 
           for (int i = 0; i < num; i++) 
           { 
              //绘制横向的线条 
              g.DrawLine(myPen, initialPoint, initialPoint + i * chessLength, chessLength * (num - 1) + initialPoint,initialPoint + i * chessLength); 
              //绘制纵向的线条 
              g.DrawLine(myPen, initialPoint + i * chessLength, initialPoint, initialPoint + i * chessLength, chessLength * (num - 1) + initialPoint);
            } 
         } 
     }
}

 

效果:

posted on 2012-05-24 14:09  冠冕堂皇  阅读(2065)  评论(0编辑  收藏  举报