注册 登录
编程论坛 C++ Builder

c++builder画正方形和圆形

KS010005 发布于 2021-04-24 17:02, 2507 次点击
要求:把畫的图形記成不同的class.按一個 redraw 會依記錄畫出不同形狀.要求要用 virtual function.
3 回复
#2
KS0100052021-04-24 21:39
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 TGroupBox *GroupBox1;
        TImage *Image1;
        int shape;
        int posX,posY,X,Y;  //初始及终点坐标
 class type
        {
        public:
            void zuotu1()
            {
            Image1->Canvas->Ellipse(posX,posY,X,Y);
            }
            virtual void zuotu2()
            {
            }
        };
         class cycle:public type
        {
        public:
            void zuotu1()
            {
            }
            void zuotu2()
            {
            Image1->Canvas->Rectangle(posX,posY,X,Y);
            }
        };
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
        shape=1;
        StatusBar1->Panels->Items[0]->Text="绘制正方形";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
        shape=2;
        StatusBar1->Panels->Items[0]->Text="绘制圆形";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, int X, int Y)
{
        posX=X;
        posY=Y;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
       if(abs(X-posX)>abs(Y-posY))
                        Y=X-posX+posY;
                else
                        X=Y-posY+posX;
        type a;
        cycle b;
        switch(shape)
        {
                case 1:
                        a.zuotu1();
                        break;
                case 2:
                        b.zuotu2();
                        break;
                default:
                        break;
}
}
//---------------------------------------------------------------------------
#3
KS0100052021-04-24 21:40
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 TGroupBox *GroupBox1;
        TImage *Image1;
        int shape;
        int posX,posY,X,Y;  //初始及终点坐标
 class type
        {
        public:
            void zuotu1()
            {
            Image1->Canvas->Ellipse(posX,posY,X,Y);
            }
            virtual void zuotu2()
            {
            }
        };
         class cycle:public type
        {
        public:
            void zuotu1()
            {
            }
            void zuotu2()
            {
            Image1->Canvas->Rectangle(posX,posY,X,Y);
            }
        };
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
        shape=1;
        StatusBar1->Panels->Items[0]->Text="绘制正方形";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
        shape=2;
        StatusBar1->Panels->Items[0]->Text="绘制圆形";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, int X, int Y)
{
        posX=X;
        posY=Y;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
       if(abs(X-posX)>abs(Y-posY))
                        Y=X-posX+posY;
                else
                        X=Y-posY+posX;
        type a;
        cycle b;
        switch(shape)
        {
                case 1:
                        a.zuotu1();
                        break;
                case 2:
                        b.zuotu2();
                        break;
                default:
                        break;
}
}
#4
KS0100052021-04-25 20:18
class CShape
{
   protected:
     TCanvas* m_pCanvas;
   public:
    virtual ~CShape(void){};
    virtual void draw( void)= 0;
};// class CShape

class CRectangle: public CShape
{
    int m_x1;
    int m_y1;
    int m_x2;
    int m_y2;
  public:
     CRectangle( TCanvas* pCanvas, int x1, int y1, int x2, int y2);
     virtual void draw( void);
};//C Rectangle


class CCircle: public CShape
{
    int m_x;
    int m_y;
    int m_r;
  public:
    CCircle( TCanvas* pCanvas, int x, int y, int r);
    virtual void draw( void);
};// class CCircle

class TForm1:...
{
   ...
  private:
   void addRect( x1, y1, x2, y2);
   void addCir( x, y, r);
   void redraw( void);
   CShape* m_apShape[100];
   int     m_szShape;
  
}

// in .cpp

CRectangle::CRectangle( TCanvas* pCanvas, int x1, int y1, int x2, int y2)
{
  m_pCanvas= pCanvas;
  m_x1= x1;
  m_y1= y1;
  m_x2= x2;
  m_y2= y2;
}// CRectangle()

void CRectangle::draw( void)
{
  m_pCanvas->Rectangle( m_x1, m_y1, m_x2, m_y2);
}// draw()

CCircle::CCircle( TCanvas* pCanvas, int x, int y, int r)
{
  int x1= m_x- m_r;
  int y1= m_y- m_r;
  int x2= m_x+ m_r;
  int y2= m_y+ m_r;
  m_pCanvas->Ellipse( x1, y1, x2, y2);
}// CCircle

TForm1::TFrom1()
{
  m_szShape= 0;
}//

void TForm1::addRect( x1, y1, x2, y2)
{
  CShape* pShape= new CRectangle( Image1->Canvas, x1, y1, x2, y2);
  m_apShape[ m_szShape]= pShape;
  m_szShape++;
}// addRect

void TForm1::addCir( x, y, r)
{
  CShape* pShape= new CCircle( Image1->Canvas, x, y, r);
  m_apShape[ m_szShape]= pShape;
  m_szShape++;
}// addCir

void TForm1::redraw( void)
{
  for( int ixShape= 0; ixShape< m_szShape; ixShape++)
  {
    m_apShape->draw();
  }// for ixShape
}// redraw()
1