👊来学习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.项目结构
以下是项目结构的展示: