您现在的位置是:群英 > 开发技术 > 编程语言
Java怎么计算规则几何图形的绘制与周长面积
Admin发表于 2022-10-09 18:12:08362 次浏览
今天这篇给大家分享的知识是“Java怎么计算规则几何图形的绘制与周长面积”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,因此分享发大家做个参考,下文的讲解详细,步骤过程清晰,希望这篇“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怎么计算规则几何图形的绘制与周长面积的介绍,本文内容仅供参考,有需要的朋友可以借鉴了解看看,希望对大家学习或工作,想要了解更多欢迎关注群英网络,小编每天都会为大家更新不同的知识。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-07-04 17:14:52 
摘要:需要在Go写的服务里面调用命令行或者批处理,并根据返回的结果做处理。但是windows下面用cmd返回中文会出现乱码,本文就详细的介绍一下解决方法,感兴趣的可以了解一下
2022-01-29 17:57:40 
摘要:HTML中禁止选中文字方法是什么?在HTML中我们想要禁止文字被选中,那么使用user-select属性就是实现,其作用就是用于设置用户是否能够选中文本。虽然user-select属性使用不难,但是兼容性问题大家是需要考虑的,下面我们详细的看看。
2022-05-05 15:16:27 
摘要:IE浏览器向来是前端开发者的噩梦,今天遇到一个问题就是在IE浏览器中的cookie无法读取,无论是在前端还是在后端服务器Request中都无法读取Cookie,找了好久才发现问题,原来是IE中Cookie总的大小超出了浏览器所规定的限度
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部