Java實(shí)現(xiàn)規(guī)則幾何圖形的繪制與周長面積計(jì)算詳解
1.背景
規(guī)則幾何圖形問題求解的程序是對(duì)根據(jù)輸入規(guī)則幾何圖形的一些特定的參數(shù)來實(shí)現(xiàn)對(duì)規(guī)則幾何圖形的面積和周長求解,以及根據(jù)輸入的參數(shù)對(duì)對(duì)他們進(jìn)行繪制相應(yīng)的圖形。
在程序中通過規(guī)則幾何的類型來輸入相應(yīng)的參數(shù)有程序得到相應(yīng)的解和圖形。這從而達(dá)到了對(duì)圖形的計(jì)算便利計(jì)算和直觀的求出圖形,從而幫助計(jì)算人員擁有更高的計(jì)算效率。
關(guān)鍵字:Java,swing,規(guī)則幾何圖形,文件操作
2.開發(fā)工具
本程序開發(fā)使用的IDE是idea!
3.數(shù)據(jù)存儲(chǔ)設(shè)計(jì)
1.程序采用文件流操作將數(shù)據(jù)存儲(chǔ)到.txt文件中,文件的路徑是d:\\xxx.txt。
2.文件中存儲(chǔ)了基本的數(shù)據(jù),包括輸入的規(guī)則幾何圖形的長寬高等數(shù)據(jù),還包括計(jì)算得到的面積周長等數(shù)據(jù)!例如:
4.項(xiàng)目功能設(shè)計(jì)
在程序中,可以實(shí)現(xiàn)已經(jīng)添加的幾何圖形的面積和周長的求解,繪制它們相應(yīng)的圖形和改變其形狀,線條的粗細(xì)和顏色。根據(jù)提示,我們可以輸入相關(guān)特征的參數(shù)去求解除它們的面積、周長以及改變它們的參數(shù),也可以根據(jù)提示去改變各邊的線條的粗細(xì)和顏色。
在規(guī)則幾何問題求解中系統(tǒng)主要有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("幾何圖形計(jì)算"); circleplay.setSize(500,300); circleplay.setLocation(500,250); } } class WindowCircle extends JFrame { Circle circle; // 數(shù)據(jù)對(duì)象 JTextField textA, textB, textC; // 數(shù)據(jù)對(duì)象的視圖 JTextArea showArea; // 數(shù)據(jù)對(duì)象的視圖 JButton controlButton1; // 控制器對(duì)象 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("計(jì)算"); 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("圖形線條粗細(xì)")); 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[]{"黑色","紅色","灰色","藍(lán)色","黃色","綠色","紫色"}; 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); // 更新數(shù)據(jù) 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("圓的圖形"); // 創(chuàng)建畫板 JPanel jpanel = new JPanel() { public void paint(Graphics graphics) { // 必須先調(diào)用父類的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("圓形線條粗細(xì)為"); dout.write(String.valueOf(line)); dout.write(" \r\n"); }catch(IOException a){} } if(line == 2){ try{ dout.write("圓形線條粗細(xì)為"); dout.write(String.valueOf(line)); dout.write(" \r\n"); }catch(IOException a){} } if(line == 3){ try{ dout.write("圓形線條粗細(xì)為"); dout.write(String.valueOf(line)); dout.write(" \r\n"); }catch(IOException a){} } if(line == 4){ try{ dout.write("圓形線條粗細(xì)為"); dout.write(String.valueOf(line)); dout.write(" \r\n"); }catch(IOException a){} } if(line == 5){ try{ dout.write("長方形線條粗細(xì)為"); dout.write(String.valueOf(line)); dout.write(" \r\n"); }catch(IOException a){} } if(line == 6){ try{ dout.write("圓形線條粗細(xì)為"); dout.write(String.valueOf(line)); dout.write(" \r\n"); }catch(IOException a){} } if(line == 7){ try{ dout.write("圓形線條粗細(xì)為"); dout.write(String.valueOf(line)); dout.write(" \r\n"); }catch(IOException a){} } if(line == 8){ try{ dout.write("圓形線條粗細(xì)為"); dout.write(String.valueOf(line)); dout.write(" \r\n"); }catch(IOException a){} } if(line == 9){ try{ dout.write("圓形線條粗細(xì)為"); 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("藍(lán)色"); 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); // 設(shè)置畫框大小(寬度,高度),默認(rèn)都為0 jFrame.setSize(300, 300); // 將畫框展示出來。true設(shè)置可見,默認(rèn)為false隱藏 jFrame.setVisible(true); jFrame.setLocation(1000,250); } }
6.項(xiàng)目結(jié)構(gòu)
以下是項(xiàng)目結(jié)構(gòu)的展示:
7.總結(jié)
規(guī)則幾何圖形求解根據(jù)圖形的某些特征設(shè)置輸入?yún)?shù),根據(jù)這些參數(shù)來計(jì)算相應(yīng)圖形的面積和周長。在繪制圖形方面,是根據(jù)所輸入的參數(shù)來確定坐標(biāo),再連接坐標(biāo)形成的圖形。在改變圖形方面,用繪圖的類去改變圖形。
這個(gè)程序適合在新手學(xué)習(xí)完Java基礎(chǔ)知識(shí)以后練習(xí),可以加深對(duì)Java編程的理解,同時(shí)對(duì)Java流的操作這一個(gè)抽象的概念有了更加深入的理解,學(xué)習(xí)完GUI技術(shù)不僅提升了編程興趣,同時(shí)為Java下一階段的學(xué)習(xí)奠定了基礎(chǔ)。
以上就是Java實(shí)現(xiàn)規(guī)則幾何圖形的繪制與周長面積計(jì)算詳解的詳細(xì)內(nèi)容,更多關(guān)于Java幾何圖形繪制 計(jì)算的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解
這篇文章主要為大家介紹了數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Java實(shí)現(xiàn)的校驗(yàn)銀行卡功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的校驗(yàn)銀行卡功能,結(jié)合完整實(shí)例形式分析了java針對(duì)銀行卡類型、歸屬地等信息的判斷、讀取相關(guān)操作技巧,需要的朋友可以參考下2018-06-06詳解Java實(shí)現(xiàn)的k-means聚類算法
這篇文章主要介紹了詳解Java實(shí)現(xiàn)的k-means聚類算法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01基于Java并發(fā)容器ConcurrentHashMap#put方法解析
下面小編就為大家?guī)硪黄贘ava并發(fā)容器ConcurrentHashMap#put方法解析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06SpringBoot?整合Mybatis-Plus并輸出SQL日志示例詳解
這篇文章主要介紹了SpringBoot整合Mybatis-Plus并輸出SQL日志,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06帶你走進(jìn)Maven的大門-最全Maven配置及集成idea工具總結(jié)
Maven項(xiàng)目對(duì)象模型(POM),是一個(gè)項(xiàng)目管理工具可以通過一小段描述信息來管理項(xiàng)目的構(gòu)建,報(bào)告和文檔的軟件.那我們想要在IDEA中使用Maven得進(jìn)行一些配置,接下來我們具體看一下是如何配置使用的,需要的朋友可以參考下2021-06-06