中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
面向对象开发范例之多态化图形操作
作者:未知 时间:2005-07-27 23:32 出处:CSDN 责编:chinaitpower
              摘要:面向对象开发范例之多态化图形操作


/*
作者:meteor135
Email:meteor@mail.biti.edu.cn
      smith_135@163.com
时间:2003.5.22
*/
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <bios.h>
#include <dos.h>
#define  path "C:\\TC\\BGI"     //改为你自己的BGI路径
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include <math.h>
#define Pi      3.1415926
#define RIGHT   77
#define LEFT    75
#define UP      72
#define DOWN    80
#define HIDE    'h'
#define SHOW    's'
#define ENLARGE '='
#define REDUCE  'b'
#define Alt_X   '-'
enum bool { false, true };
int  GetKey();
int  prompt();
void window(int, int, int, int, int, int, int);
//////////////////////////////////////////////////
class Location
{
protected:
    int x0, y0;
public:
    Location(int x, int y){x0=x;y0=y;}
    int GetX0(){return x0;}
    int GetY0(){return y0;}
};

class Point:public Location
{
protected:
    bool isVisible;
    void Draw();
public:
    void Show(){putpixel(x0,y0,getcolor());}
    Point(int x, int y):Location(x,y){};
    bool GetVisible(){return isVisible;}
};
//////////////////////////////////////////////////////////////
class Figure : public Point
{
protected:
    int changeStep;
    virtual void  Draw   ()=0;
public:
    Figure(int x, int y):Point(x, y){changeStep=10;}
public:
    virtual void  Show   ();
    virtual void  Left   ();
    virtual void  Up     ();
    virtual void  Down   ();
    virtual void  Hide   ();
    virtual void  Right  ();
public:
    virtual void  Enlarge()=0;
    virtual void  Reduce ()=0;
};
void Figure::Hide()
{
    unsigned color = getcolor();
    isVisible = false;
    setcolor(getbkcolor());
    Draw();
    setcolor(color);
}
void Figure::Show()
{
    setcolor(getcolor());
    Draw();
    isVisible=true;
}
void Figure::Up()
{
    Hide();
    (y0 -= changeStep) ? 0 : (y0 += 480);
    Draw();
}
void Figure::Down()
{
    Hide();
    y0 = (y0 + changeStep) % 480;
    Draw();
}
void Figure::Right()
{
    Hide();
    x0 = (x0 + changeStep) % 640;
    Draw();
}
void Figure::Left()
{
    Hide();
    (x0 -= changeStep) ? 0 : (x0 += 640);
    Draw();
}
//////////////////////////////////////////////////////////////
class Rectangle : public Figure
{
private:
    int width,height;
    void Draw()
    {
        int left,top,right,bottom;
        left   = x0-width /2;
        top    = y0-height/2;
        right  = x0+width /2;
        bottom = y0+height/2;
        rectangle(left,top,right,bottom);
    }
public:
    Rectangle(int x, int y, int w, int h):Figure(x,y)
    {
        width  = w;
        height = h;
    }
public:
    int   GetWidth (){return width ;}
    int   GetHeight(){return height;}
    void  Enlarge();
    void  Reduce ();
};
void Rectangle::Enlarge()
{
    Hide();
    width  += changeStep;
    height += changeStep;
    Draw();
}
void Rectangle::Reduce()
{
    Hide();
    width  -= width >0&&height>0 ? changeStep : 0;
    height -= height>0&&height>0 ? changeStep : 0;
    Draw();
}
//////////////////////////////////////////////////////////////
class Circle: public Figure
{
private:
    int radius;
    void Draw(){circle(x0, y0, radius);}
public:
    Circle(int x, int y, int radius);
    int GetRadius(){return radius;}
    void  Enlarge();
    void  Reduce ();
};

Circle::Circle(int x, int y, int radius):Figure(x, y)
{
    this->radius = radius;
}
void Circle::Enlarge()
{
    Hide();
    radius += changeStep;
    Draw();
}
void Circle::Reduce()
{
    Hide();
    radius -= radius>0 ? changeStep : 0;
    Draw();
}
/////////////////////////////////////////////////////////////////////////
void main()
{
    Figure *pFigure;
    int choice;
    while((choice=prompt())>0)
    {
        switch(choice)
        {
        case 1:
            pFigure = new Circle(320, 240, 100);
            break;
        case 2:
            pFigure = new Rectangle(200,200,300,200);
            break;
        default:
            pFigure = new Circle(320, 240, 100);
        }

        char s1[10], s2[10];
        //char s3[15];

        int key;
        int gdriver = DETECT, gmode;

        initgraph(&gdriver, &gmode, path);
        setbkcolor(LIGHTCYAN);
        cleardevice();

        window(0, 0, getmaxx(), getmaxy(), 20, BLUE, LIGHTCYAN);
        settextstyle(1, 0, 1);
        setcolor(RED);
        outtextxy(20, 15, "Now you will see a figure and you can do something with it");
        outtextxy(20, 33, "Press any key to continue!");
        outtextxy(20, 57, "h:hide  s:show  =:enlarge  b:reduce");
        outtextxy(20, 75, "   :right   :left  :up   :down  alt_x:quit");
        setlinestyle(SOLID_LINE, 1, 3);
        line(26, 88, 40, 88);
        line(40, 88, 36, 84);
        line(40, 88, 36, 92);
        line(100, 88, 114, 88);
        line(100, 88, 104, 84);
        line(100, 88, 104, 92);
        line(214, 82, 214, 95);
        line(214, 95, 210, 91);
        line(214, 95, 218, 91);
        line(166, 82, 166, 95);
        line(166, 82, 162, 86);
        line(166, 82, 170, 86);
        setcolor(RED);
        rectangle(390,62,441,81);
        rectangle(454,62,505,81);
        //rectangle(518,62,601,81);
        while((key = GetKey())!= Alt_X)
        {
            setfillstyle(SOLID_FILL,CYAN);
            bar(391,63,440,80);
            bar(455,63,504,80);
            //bar(519,63,600,80);
            setviewport(20, 100, 618, 458 , 1);
            switch(key)
            {
                case RIGHT   :    pFigure->Right  ();    break;
                case LEFT    :    pFigure->Left   ();    break;
                case UP      :    pFigure->Up     ();    break;
                case DOWN    :    pFigure->Down   ();    break;
                case ENLARGE :    pFigure->Enlarge();    break;
                case REDUCE  :    pFigure->Reduce ();    break;
                case HIDE    :    pFigure->Hide   ();    break;
                case SHOW    :    pFigure->Show   ();    break;
                default      :                           break;
            }
            setviewport(0, 0, 639, 479, 1);
            settextstyle(2,0,5);
            setcolor(RED);
            sprintf(s1,"X0=%d",pFigure->GetX0());
            outtextxy(392,62,s1);
            sprintf(s2,"Y0=%d",pFigure->GetY0());
            outtextxy(456,62,s2);
            //sprintf(s3,"RADIUS=%d",pFigure->GetRadius());
            //outtextxy(520,62,s3);
        }
        delete pFigure;
        closegraph();
    }
}

///////////////////////////////////////////////////////////////////////////////////////
void window(int left, int top, int right, int bottom, int width, int color1, int color2)
{
    setfillstyle(SOLID_FILL, color1);
    bar(left, top, right, bottom);
    setcolor(color2);
    rectangle(left+width, top+width*5, right-width, bottom-width);
    setfillstyle(SOLID_FILL, color2);
    floodfill(left+width+1, top+width*5+1, color2);
    rectangle(left+2, top+2, right-2, bottom-2);
    rectangle(left+width-2, top+width-2, right-width+2, bottom-width+2);
    line(left+width-1, top+width*3-2, right-width+1, top+width*3-2);
    line(left+width-1, top+width*5-2, right-width+1, top+width*5-2);
    line(left+2, top+2, left+width-2, top+width-2);
    line(right-2, top+2, right-width+2, top+width-2);
    line(left+2, bottom-2, left+width-2, bottom-width+2);
    line(right-2, bottom-2, right-width+2, bottom-width+2);
}
///////////////////////////////////////////////////////////
int prompt()
{
    printf("This program shows a figure, it can move there and here,\n");
    printf("hide and then show.\n");
    printf("\nPlease input your choice to show what figure you want to see:");
    printf("\n1:Circle\n2:Rectangle\n0:quit\n");
    int choice = 0;
    cin >> choice;
    return choice;
}
//////////////////////////////////////////////////////////
int GetKey()
{
    int key;
    while(bioskey(1) == 0);
    key = bioskey(0);
    key = key & 0xff ? key & 0xff : key >> 8;
    return(key);
}
/////////////////////////////////////////////////////////
//The program is over here.




关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有