c\#중국 장기판 과 바둑 알 그리 기

본 고 는 C\#를 이용 하여 중국 장기 의 바둑판 그리 기 를 실현 하고 구 조 를 초기 화 하 며 중국 장기 의 대국 논 리 를 실현 하지 않 는 다.학습 참고 로 만 사용 할 수 있 습 니 다.
생각:
4.567917.중국 장기 판,세로 9 개,가로 10 개 를 그립 니 다.그 중간 에'초 하','한 계'를 그립 니 다4.567917.바둑 알 을 그리고 바둑 알 을 바둑판 에 배치 하면 됩 니 다.
관련 지식 포인트:
  • 사용자 컨트롤:바둑판 의 그리 기 를 실현 하고 OnPaint(PaintEventArgs)방법 을 다시 쓰 는 데 사 용 됩 니 다
  • 매트릭스:패 키 징 은 기하학 적 변 화 를 나타 내 는 3x3 모방 행렬 이다.이 예 에 서 는 주로 회전 하여 반대 쪽 의'한 계'를 그 리 는 데 쓰 인 다Graphics Path:서로 연 결 된 일련의 직선 과 곡선 을 나타 낸다.이 예 에 서 는 주로 원형 바둑 알 을 그 리 는 데 쓰 인 다.
    효과 도 는 다음 과 같다.
    (1)

    (2)

    핵심 코드
    바둑판 의 핵심 코드 는 다음 과 같다.
    
    protected override void OnPaint(PaintEventArgs e)
      {
       base.OnPaint(e);
    
       //     
       InitArrPieceInfo();
    
       Graphics g = e.Graphics;
       int width = this.Width;
       int height = this.Height;
       int padding = this.Padding.All * 20;
       int center = height / 2;//      
       int s_width = (width - 2 * padding) / 8;//        
       int s_heigth = (height - 2 * padding) / 9;//        
       int start_x = padding;//    
       int start_y = padding;//    
       Pen pen = new Pen(Brushes.Black, 1.5f);
       Dictionary<string, string[]> dicNums = new Dictionary<string, string[]>();
       dicNums.Add("up", new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });
       dicNums.Add("down", new string[9] { " ", " ", " ", " ", " ", " ", " ", " ", " " });
       Font font = new Font("  ", 12, FontStyle.Regular);
       for (int i = 0; i < 9; i++)
       {
        //    
        Point p0 = new Point(start_x + i * s_width, start_y);
        Point p1 = new Point(start_x + i * s_width, start_y + (s_heigth * 4));
        Point p2 = new Point(start_x + i * s_width, start_y + (s_heigth * 5));
        Point p3 = new Point(start_x + i * s_width, start_y + (s_heigth * 9));
        g.DrawLine(pen, p0, p1);
        g.DrawLine(pen, p2, p3);
        //     
        Point p_up = new Point(start_x + i * s_width - 5, padding / 2);
        Point p_down = new Point(start_x + i * s_width - 5, start_y + (s_heigth * 9) + padding / 3);
        g.DrawString(dicNums["up"][i], font, Brushes.Black, p_up);
        g.DrawString(dicNums["down"][i], font, Brushes.Black, p_down);
        //    
        for (int j = 0; j < 10; j++)
        {
         Point absLocation = ArrPiece[i, j].AbsoluteLocation;
         absLocation.X = start_x + i * s_width;
         ArrPiece[i, j].AbsoluteLocation = absLocation;
        }
       }
       for (int i = 0; i < 10; i++)
       {
        //    
        Point p0 = new Point(start_x, start_y + i * s_heigth);
        Point p1 = new Point(start_x + s_width * 8, start_y + i * s_heigth);
        g.DrawLine(pen, p0, p1);
        //    
        for (int j = 0; j < 9; j++)
        {
         Point absLocation = ArrPiece[j, i].AbsoluteLocation;
         absLocation.Y = start_y + i * s_heigth;
         ArrPiece[j, i].AbsoluteLocation = absLocation;
        }
       }
       //     
       for (int i = 0; i < 2; i++)
       {
        Point p0 = new Point(start_x + (3 + i * 2) * s_width, start_y);
        Point p1 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 2));
        Point p2 = new Point(start_x + (3 + i * 2) * s_width, start_y + (s_heigth * 7));
        Point p3 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 9));
        g.DrawLine(pen, p0, p1);
        g.DrawLine(pen, p2, p3);
       }
    
       //       ,    
       for (int i = 0; i < 5; i++)
       {
        int p_x = start_x + 2 * i * s_width;
        int p_y = start_y + 3 * s_heigth;
        DrawCorner(g, pen, p_x, p_y);// 
        p_y = start_y + 6 * s_heigth;
        DrawCorner(g, pen, p_x, p_y);// 
       }
       //     ,    
       for (int i = 0; i < 2; i++)
       {
        int p_x = start_x + (1 + 6 * i) * s_width;
        int p_y = start_y + 2 * s_heigth;
        DrawCorner(g, pen, p_x, p_y);// 
        p_y = start_y + 7 * s_heigth;
        DrawCorner(g, pen, p_x, p_y);// 
       }
       //      
       Point p_0 = new Point(2 * s_width, center - 25);
       Point p_1 = new Point(7 * s_width, center + 20);
       font = new Font("      ", 30, FontStyle.Regular);
       g.DrawString("  ", font, Brushes.Black, p_0);
       Matrix mtxSave = g.Transform;
       Matrix mtxRotate = g.Transform;
       mtxRotate.RotateAt(180, p_1);
       g.Transform = mtxRotate;
       g.DrawString("  ", font, Brushes.Black, p_1);
       g.Transform = mtxSave;
       //     
       g.DrawRectangle(pen, 3, 3, width - 6, height - 6);
       g.DrawRectangle(pen, 5, 5, width - 10, height - 10);
       g.DrawRectangle(pen, 7, 7, width - 14, height - 14);
      }
    바둑돌 의 핵심 코드 는 다음 과 같다.
    
    protected override void OnPaint(PaintEventArgs e)
      {
       base.OnPaint(e);
       Graphics g = e.Graphics;
       GraphicsPath gPath = new GraphicsPath();
       // Set a new rectangle to the same size as the button's ClientRectangle property.
       Rectangle rectangle = this.ClientRectangle;
       g.DrawEllipse(new Pen(this.FlatAppearance.BorderColor), rectangle);
       gPath.AddEllipse(rectangle);
    
       // Set the button's Region property to the newly created circle region.
       this.Region = new Region(gPath);
       Rectangle inRect = new Rectangle(2, 2, this.Width - 4, this.Height - 3);
       g.FillEllipse(new SolidBrush(this.BackColor), rectangle);
       g.DrawEllipse(new Pen(Color.Black,2), inRect);
    
       Font font = new Font("  ", 25, FontStyle.Regular);
       g.DrawString(this.Text, font, new SolidBrush(this.ForeColor), 0,5);
      }
    이상 은 c\#중국 장기 바둑판 과 바둑 알 을 그 리 는 상세 한 내용 입 니 다.c\#바둑판 과 바둑 알 을 그 리 는 자 료 는 우리 의 다른 관련 글 을 주목 하 세 요!

    좋은 웹페이지 즐겨찾기