Java實現(xiàn)規(guī)則幾何圖形的繪制與周長面積計算詳解
1.背景
規(guī)則幾何圖形問題求解的程序是對根據(jù)輸入規(guī)則幾何圖形的一些特定的參數(shù)來實現(xiàn)對規(guī)則幾何圖形的面積和周長求解,以及根據(jù)輸入的參數(shù)對對他們進行繪制相應的圖形。
在程序中通過規(guī)則幾何的類型來輸入相應的參數(shù)有程序得到相應的解和圖形。這從而達到了對圖形的計算便利計算和直觀的求出圖形,從而幫助計算人員擁有更高的計算效率。
關(guān)鍵字:Java,swing,規(guī)則幾何圖形,文件操作
2.開發(fā)工具
本程序開發(fā)使用的IDE是idea!

3.數(shù)據(jù)存儲設計
1.程序采用文件流操作將數(shù)據(jù)存儲到.txt文件中,文件的路徑是d:\\xxx.txt。

2.文件中存儲了基本的數(shù)據(jù),包括輸入的規(guī)則幾何圖形的長寬高等數(shù)據(jù),還包括計算得到的面積周長等數(shù)據(jù)!例如:

4.項目功能設計
在程序中,可以實現(xiàn)已經(jīng)添加的幾何圖形的面積和周長的求解,繪制它們相應的圖形和改變其形狀,線條的粗細和顏色。根據(jù)提示,我們可以輸入相關(guān)特征的參數(shù)去求解除它們的面積、周長以及改變它們的參數(shù),也可以根據(jù)提示去改變各邊的線條的粗細和顏色。
在規(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("幾何圖形計算");
circleplay.setSize(500,300);
circleplay.setLocation(500,250);
}
}
class WindowCircle extends JFrame {
Circle circle; // 數(shù)據(jù)對象
JTextField textA, textB, textC; // 數(shù)據(jù)對象的視圖
JTextArea showArea; // 數(shù)據(jù)對象的視圖
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); // 更新數(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("圓形線條粗細為");
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.項目結(jié)構(gòu)
以下是項目結(jié)構(gòu)的展示:

7.總結(jié)
規(guī)則幾何圖形求解根據(jù)圖形的某些特征設置輸入?yún)?shù),根據(jù)這些參數(shù)來計算相應圖形的面積和周長。在繪制圖形方面,是根據(jù)所輸入的參數(shù)來確定坐標,再連接坐標形成的圖形。在改變圖形方面,用繪圖的類去改變圖形。
這個程序適合在新手學習完Java基礎知識以后練習,可以加深對Java編程的理解,同時對Java流的操作這一個抽象的概念有了更加深入的理解,學習完GUI技術(shù)不僅提升了編程興趣,同時為Java下一階段的學習奠定了基礎。

以上就是Java實現(xiàn)規(guī)則幾何圖形的繪制與周長面積計算詳解的詳細內(nèi)容,更多關(guān)于Java幾何圖形繪制 計算的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Mybatis-Plus自動填充更新操作相關(guān)字段的實現(xiàn)
這篇文章主要介紹了Mybatis-Plus自動填充更新操作相關(guān)字段的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
JAVA HashSet和TreeSet 保證存入元素不會重復的操作
這篇文章主要介紹了JAVA HashSet和TreeSet 保證存入元素不會重復的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
spring(java,js,html) 截圖上傳圖片實例詳解
這篇文章主要介紹了spring(java,js,html) 截圖上傳圖片實例詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
Java基于正則表達式實現(xiàn)的替換匹配文本功能【經(jīng)典實例】
這篇文章主要介紹了Java基于正則表達式實現(xiàn)的替換匹配文本功能,結(jié)合完整實例形式分析了java字符串正則替換操作技巧,需要的朋友可以參考下2017-04-04

