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

 更新时间:2022年07月11日 14:17:40   作者:橙子!  
随着计算机的发展,人们对图形的计算要求会越来越高。在各行各业中的计算人员会对图形的计算要有便利的要求,规则几何图形问题求解程序应运而生!本文将用Java编写一个程序,可以实现规则几何图形的绘制与周长面积计算,感兴趣的可以了解一下

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几何图形绘制 计算的资料请关注脚本之家其它相关文章!

相关文章

  • Mybatis-Plus自动填充更新操作相关字段的实现

    Mybatis-Plus自动填充更新操作相关字段的实现

    这篇文章主要介绍了Mybatis-Plus自动填充更新操作相关字段的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Spring 加载多个xml配置文件的原理分析

    Spring 加载多个xml配置文件的原理分析

    我们知道Spring一次可以加载多个Bean定义的Xml配置文件,我们可以设想下如果让我们来做我们会怎么做?我估计会根据配置文件的顺序依次读取并加载,那再来看看Spring是如何做的?
    2021-06-06
  • SpringCloud的那些中间件问题

    SpringCloud的那些中间件问题

    这篇文章主要介绍了SpringCloud的那些中间件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • JAVA HashSet和TreeSet 保证存入元素不会重复的操作

    JAVA HashSet和TreeSet 保证存入元素不会重复的操作

    这篇文章主要介绍了JAVA HashSet和TreeSet 保证存入元素不会重复的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 新手初学Java继承、封装与多态

    新手初学Java继承、封装与多态

    封装、继承、多态三大特征是java中比较常用的,务必要掌握,下面给大家介绍Java封装、继承、多态三大特征的理解,有不清楚的朋友可以一起学习下
    2021-07-07
  • Spring AOP对嵌套方法不起作用的解决

    Spring AOP对嵌套方法不起作用的解决

    这篇文章主要介绍了Spring AOP对嵌套方法不起作用的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • intellij idea tomcat热部署配置教程

    intellij idea tomcat热部署配置教程

    这篇文章主要介绍了intellij idea tomcat热部署配置教程图解,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
    2018-07-07
  • spring(java,js,html) 截图上传图片实例详解

    spring(java,js,html) 截图上传图片实例详解

    这篇文章主要介绍了spring(java,js,html) 截图上传图片实例详解的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • Java基于正则表达式实现的替换匹配文本功能【经典实例】

    Java基于正则表达式实现的替换匹配文本功能【经典实例】

    这篇文章主要介绍了Java基于正则表达式实现的替换匹配文本功能,结合完整实例形式分析了java字符串正则替换操作技巧,需要的朋友可以参考下
    2017-04-04
  • 详解java如何集成swagger组件

    详解java如何集成swagger组件

    今天给大家带来的是关于Java的相关知识,文章围绕着java如何集成swagger组件展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06

最新评论