Java实现规则几何图形的绘制与周长面积计算详解

2022-07-13,,,,

1.背景

规则几何图形问题求解的程序是对根据输入规则几何图形的一些特定的参数来实现对规则几何图形的面积和周长求解,以及根据输入的参数对对他们进行绘制相应的图形。

在程序中通过规则几何的类型来输入相应的参数有程序得到相应的解和图形。这从而达到了对图形的计算便利计算和直观的求出图形,从而帮助计算人员拥有更高的计算效率。

关键字:java,swing,规则几何图形,文件操作

2.开发工具

本程序开发使用的ide是idea!

3.数据存储设计

1.程序采用文件流操作将数据存储到.txt文件中,文件的路径是d:\\xxx.txt。

2.文件中存储了基本的数据,包括输入的规则几何图形的长宽高等数据,还包括计算得到的面积周长等数据!例如:

4.项目功能设计

在程序中,可以实现已经添加的几何图形的面积和周长的求解,绘制它们相应的图形和改变其形状,线条的粗细和颜色。根据提示,我们可以输入相关特征的参数去求解除它们的面积、周长以及改变它们的参数,也可以根据提示去改变各边的线条的粗细和颜色。

在规则几何问题求解中系统主要有main程序、triangleplay程序、 rectangleplay程序、squareplay程序、circleplay程序、rhombusplay程序、trapezoidplay程序、trapezoidequilateral程序和trapezoidright程序。

5.部分代码展示

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;


public class circleplay {
    public static void main(string args[]){
        windowcircle circleplay = new windowcircle();
        circleplay.settitle("几何图形计算");
        circleplay.setsize(500,300);
        circleplay.setlocation(500,250);
    }
}

class windowcircle extends jframe {
    circle circle; // 数据对象
    jtextfield texta, textb, textc; // 数据对象的视图
    jtextarea showarea; // 数据对象的视图
    jbutton controlbutton1; // 控制器对象
    jbutton controlbutton2;


    windowcircle() {
        init();
        setvisible(true);
        setdefaultcloseoperation(jframe.dispose_on_close);

    }

    void init() {
        circle = new circle();
        texta = new jtextfield(5);
        textb = new jtextfield(5);
        showarea = new jtextarea();
        controlbutton1 = new jbutton("计算");
        controlbutton2 = new jbutton("退出");
        jpanel pnorth = new jpanel();
        jpanel pnorth1 = new jpanel();

        pnorth.add(new jlabel("半径"));
        pnorth.add(texta);
        pnorth.add(controlbutton1);
        pnorth.add(controlbutton2);
        pnorth.setlocation(250,250);

        pnorth1.add(new jlabel("图形线条粗细"));
        string[] s1 = new string[]{"1","2","3","4","5","6","7","8","9"};
        final jcombobox<string> combobox1 = new jcombobox<string>(s1);
        pnorth1.add(combobox1);


        pnorth1.add(new jlabel("图形线条颜色"));
        string[] s2 = new string[]{"黑色","红色","灰色","蓝色","黄色","绿色","紫色"};
        final jcombobox<string> combobox2 = new jcombobox<string>(s2);
        pnorth1.add(combobox2);

        add(pnorth, borderlayout.north);
        pnorth1.setpreferredsize(new dimension(90,150));
        add(pnorth1, borderlayout.west);
        add(new jscrollpane(showarea), borderlayout.center);

        controlbutton1.addactionlistener(new actionlistener(){
            public void actionperformed(actionevent e){
                try {
                    double a = double.parsedouble(texta.gettext().trim()); //
                    circle.seta(a); // 更新数据
                    circle.paint();
                    string area1 = circle.getlength();
                    string area2 = circle.getarea();
                    showarea.append("半径为"+a+"的圆"+"	 "+"周长为:" +area1+"	"+"面积为:"+area2+"\n");
                } catch (exception ex) {
                    showarea.append("\n" + ex + "\n");
                }
            }});

        controlbutton2.addactionlistener(new actionlistener(){
            public void actionperformed(actionevent e){
                dispose();
            }});

        combobox1.additemlistener(new itemlistener() {
            public void itemstatechanged(itemevent e) {
                if (e.getstatechange() == itemevent.selected) {
                    circle.line(combobox1.getselectedindex()+1);
                    circle.paint();
                }
            }});


        combobox2.additemlistener(new itemlistener() {
            public void itemstatechanged(itemevent e) {
                if (e.getstatechange() == itemevent.selected) {
                    circle.colour(combobox2.getselectedindex()+1);
                    circle.paint();
                }
            }});

    }}

class circle {

    filewriter dout;
    double sidea, sideb, area,p;
    int line = 1,colournumber = 1;

    public void seta(double a) {
        sidea = a;

    }



    public string getarea() {

        area = 3.14*sidea*sidea;
        return string.valueof(area); // double.tostring(area)

    }
    public string getlength() {

        p = 3.14 * sidea;
        return string.valueof(p);

    }


    public void line(int line)
    {
        this.line = line;

    }


    public void colour(int colournumber)
    {
        this.colournumber = colournumber;

    }


    public void paint(){

        try{
            dout = new filewriter("d:\\circle.txt");

        }catch(ioexception e){}

        jframe jframe = new jframe("圆的图形");
        // 创建画板
        jpanel jpanel = new jpanel() {

            public void paint(graphics graphics) {
                // 必须先调用父类的paint方法
                super.paint(graphics);

                graphics2d g=(graphics2d)  graphics.create();
                g.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);
                if(colournumber == 1)
                    g.setcolor(color.black);
                else if(colournumber == 2)
                    g.setcolor(color.red);
                else if(colournumber == 3)
                    g.setcolor(color.gray);
                else if(colournumber == 4)
                    g.setcolor(color.blue);
                else if(colournumber == 5)
                    g.setcolor(color.yellow);
                else if(colournumber == 6)
                    g.setcolor(color.green);
                else if(colournumber == 7)
                    g.setcolor(color.magenta);


                if(line == 1){
                    try{
                        dout.write("圆形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }


                if(line == 2){
                    try{

                        dout.write("圆形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }


                if(line == 3){
                    try{

                        dout.write("圆形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }


                if(line == 4){
                    try{

                        dout.write("圆形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }

                if(line == 5){
                    try{

                        dout.write("长方形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }


                if(line == 6){
                    try{

                        dout.write("圆形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }

                if(line == 7){
                    try{

                        dout.write("圆形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }


                if(line == 8){
                    try{

                        dout.write("圆形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }

                if(line == 9){
                    try{

                        dout.write("圆形线条粗细为");
                        dout.write(string.valueof(line));
                        dout.write(" \r\n");
                    }catch(ioexception a){}

                }


                if(colournumber == 1)
                {
                    g.setcolor(color.black);

                    try{

                        dout.write("圆形颜色为");
                        dout.write("黑色");
                        dout.write(" \r\n");
                    }catch(ioexception e){}

                }


                else if(colournumber == 2)
                {
                    g.setcolor(color.red);

                    try{

                        dout.write("圆形颜色为");
                        dout.write("红色");
                        dout.write(" \r\n");
                    }catch(ioexception e){}

                }

                else if(colournumber == 3)
                {

                    g.setcolor(color.gray);

                    try{

                        dout.write("圆形颜色为");
                        dout.write("灰色");
                        dout.write(" \r\n");
                    }catch(ioexception e){}

                }

                else if(colournumber == 4)

                {
                    g.setcolor(color.blue);

                    try{

                        dout.write("圆形颜色为");
                        dout.write("蓝色");
                        dout.write(" \r\n");
                    }catch(ioexception e){}

                }

                else if(colournumber == 5)
                {
                    g.setcolor(color.yellow);

                    try{

                        dout.write("圆形颜色为");
                        dout.write("黄色");
                        dout.write(" \r\n");
                    }catch(ioexception e){}

                }

                else if(colournumber == 6)
                {
                    g.setcolor(color.green);
                    try{

                        dout.write("圆形颜色为");
                        dout.write("绿色");
                        dout.write(" \r\n");
                    }catch(ioexception e){}

                }

                else if(colournumber == 7)
                {
                    g.setcolor(color.magenta);

                    try{

                        dout.write("圆形颜色为");
                        dout.write("紫色");
                        dout.write(" \r\n");
                    }catch(ioexception e){}

                }




                stroke stroke=new basicstroke(line);
                g.setstroke(stroke);


                decimalformat df = new decimalformat("######0");
                string str1 = df.format(sidea);
                int a = integer.parseint(str1);

                g.drawoval(100, 50, a*10,a*10);


                try{
                    dout.write("圆形半径为");
                    dout.write(string.valueof(a));
                    dout.write(" \r\n");
                    dout.write("圆形周长为");
                    dout.write(string.valueof(p));
                    dout.write(" \r\n");
                    dout.write("圆形面积为");
                    dout.write(string.valueof(area));
                    dout.write(" \r\n");
                    dout.close();
                }catch(ioexception exa){}


            }

        };

        jframe.add(jpanel);
        // 设置画框大小(宽度,高度),默认都为0
        jframe.setsize(300, 300);
        // 将画框展示出来。true设置可见,默认为false隐藏
        jframe.setvisible(true);
        jframe.setlocation(1000,250);
    }
}

6.项目结构

以下是项目结构的展示:

7.总结

规则几何图形求解根据图形的某些特征设置输入参数,根据这些参数来计算相应图形的面积和周长。在绘制图形方面,是根据所输入的参数来确定坐标,再连接坐标形成的图形。在改变图形方面,用绘图的类去改变图形。

这个程序适合在新手学习完java基础知识以后练习,可以加深对java编程的理解,同时对java流的操作这一个抽象的概念有了更加深入的理解,学习完gui技术不仅提升了编程兴趣,同时为java下一阶段的学习奠定了基础。

以上就是java实现规则几何图形的绘制与周长面积计算详解的详细内容,更多关于java几何图形绘制 计算的资料请关注其它相关文章!

《Java实现规则几何图形的绘制与周长面积计算详解.doc》

下载本文的Word格式文档,以方便收藏与打印。