来学习Java基础练习小项目啦!

随着计算机的发展,人们对图形的计算要求会越来越高。在各行各业中的计算人员会对图形的计算要有便利的要求,规则几何图形问题求解程序应运而生!


文章目录

  • 1.背景
  • 2.开发工具
  • 3.数据存储设计
  • 4.项目功能设计
  • 5.部分代码展示
  • 6.项目结构
  • 7.总结

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);// 设置画框大小(宽度,高度),默认都为0jFrame.setSize(300, 300);// 将画框展示出来。true设置可见,默认为false隐藏jFrame.setVisible(true);jFrame.setLocation(1000,250);}}

6.项目结构

以下是项目结构的展示:

7.总结

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

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


追风赶月莫停留,平芜尽处是春山