zoukankan      html  css  js  c++  java
  • 用Java在Dos下,用纯字符画图

    教女朋友Java时,让她做经典的空心菱形输出的题目。忽然想到写个用字符画各种图形的程序让她练习,于是就写了这个程序让她参考。

    DrawCharShape.java
    public class DrawCharShape
    {
        
    public static void main(String[] args)
        
    {
            Screen sc
    =new Screen('.');
            DrawShape ds
    =new DrawShape(sc,0.5);
            ds.DrawCircle(
    new Point(30,18),18);
            ds.DrawCircle(
    new Point(10,10),10);
            ds.DrawCircle(
    new Point(23,15),8);
            ds.DrawLine(
    new Point(0,0),new Point(0,38));
            ds.DrawLine(
    new Point(0,0),new Point(79,0));
            ds.DrawLine(
    new Point(10,10),new Point(70,30));
            ds.DrawCircle(
    new Point(79,0),20);
            ds.DrawRect(
    new Point(10,10),new Point(65,25));
            sc.PrintScreen();
        }

    }

    class DrawShape  //画图类
    {
        
    private Screen screen;
        
    private double linewidth;
        
    public DrawShape(Screen screen)
        
    {
            
    this(screen,1);
        }

        
    public DrawShape(Screen screen,double linewidth)
        
    {
            
    this.screen=screen;
            
    this.linewidth=linewidth;
        }

        
    public void DrawLine(Point a,Point b)
        
    {
            
    if(b.x==a.x) //如果是竖线,直接画
            {            
                
    if(a.y>b.y)
                
    {
                    
    int temp;
                    temp
    =a.y;
                    a.y
    =b.y;
                    b.y
    =temp;
                }

                
    for(int i=a.y;i<b.y;i++)
                    screen.setPoint(a.x,i);
                
    return;
            }

            
    if(b.y==a.y) //如果是横线,直接画
            {
                
    if(a.x>b.x)
                
    {
                    
    int temp;
                    temp
    =a.x;
                    a.x
    =b.x;
                    b.x
    =temp;
                }

                
    for(int i=a.x;i<b.x;i++)
                    screen.setPoint(i,a.y);
                
    return;
            }

            
    //斜线,纵坐标分成小数步长。
            double ystep=((double)b.y-a.y)/((double)b.x-a.x); 
            
    if(a.x>b.x)
                
    {
                    Point temp;
                    temp
    =a;
                    a
    =b;
                    b
    =temp;
                }

            
    //横坐标以1为单位,纵坐标以小数步长为单位。
            for(double i=a.x,j=a.y;i<b.x;i++,j+=ystep)
                    screen.setPoint((
    int)i,(int)j);
        }

        
    public void DrawCircle(Point o,int r)
        
    {
            
    for(int i=0;i<80;i++)
                
    for(int j=0;j<39;j++)
                
    if(Math.abs(o.getDistanceFrom(new Point(i,j))-r)<this.linewidth) //如果某点与圆心间的距离,与半径之差小于线宽,则把该点点亮以画圆。
                    screen.setPoint(i,j);
        }

        
    public void DrawRect(Point lefttop,Point rightbottom) //画矩形
        {
            
    for(int i=lefttop.x;i<=rightbottom.x;i++//画上下两条边
            {
                screen.setPoint(i,lefttop.y);
                screen.setPoint(i,rightbottom.y);
            }

            
    for(int i=lefttop.y;i<=rightbottom.y;i++//画左右两条边
            {
                screen.setPoint(lefttop.x,i);
                screen.setPoint(rightbottom.x,i);
            }

        }

    }

    class Point  //点类
    {
        
    int x;
        
    int y;
        
    public Point()
        
    {
            
    this(0,0);
        }

        
    public Point(int x,int y)
        
    {
            
    this.x=x;
            
    this.y=y;
        }

        
    public double getDistanceFrom(Point another) //得到与另一点之间的距离,平面几何中。点与点的距离为:开根((x1-x2)平方+(y1-y2)平方)
        {
            
    return Math.sqrt(Math.pow(another.x-this.x,2)+Math.pow(another.y-this.y,2));
        }

    }

    class Screen //虚拟屏幕类
    {
        
    private char[][] screen; //屏幕上的点
        private char pointchar;
        
    public Screen(char point)
        
    {
            
    this.pointchar=point;
            screen
    =new char[39][80]; //规定,虚拟屏幕上,共有39x80个点。(和Dos屏幕点对点)左上角点为(0,0)
        }

        
    public void setPoint(int x,int y) //把屏幕的某一点点亮
        {
            screen[y][x]
    =pointchar;
        }

        
    public void setPoint(Point p)
        
    {
            
    this.setPoint(p.x,p.y);
        }

        
    public void clearPoint(int x,int y) //把屏幕的某一点熄灭
        {
            screen[y][x]
    =' ';
        }

        
    public void clearPoint(Point p)  
        
    {
            
    this.clearPoint(p.x,p.y);
        }

        
    public void PrintScreen() //将虚拟屏幕内容输出到Dos控制台屏幕。
        {
            
    for(int i=0;i<39;i++)
                
    for(int j=0;j<80;j++)
                    System.out.print(screen[i][j]);
        }

    }
  • 相关阅读:
    Extending Markov to Hidden Markov
    Logistic Regression – Geometric Intuition
    【计算机视觉】会议投稿相关推荐
    NP难问题求解综述
    PCA的数学原理
    Brief History of Machine Learning
    图像局部特征点检测算法综述【修正排版】
    thinkphp输出表格
    1503162139-ny-分数拆分
    C++中经常使用到宏
  • 原文地址:https://www.cnblogs.com/zxsoft/p/936738.html
Copyright © 2011-2022 走看看