目录
一、实验目的
二、实验内容
三、程序清单
I.Abstract包
Point类(其实并没有用上)
II.FileOperate包
ReadFile接口
WriteFile接口
III.Geometry包
Objects类(图形父类)
Circle类
Triangle类
Rectangle类
IV.WindowDraw包
MainWindow类
CirWindow类
TriWindow类
RecWindow类
ReadWindow类
SaveWindow类
SaveSuccess类
V.Action类(主函数所在)
四、程序测试
一、实验目的
通过图形类实验体现继承封装和多态,类之间的关系,并应用高级特征(包括接口、抽象类、包等)、输入输出流、异常处理、窗口、事件等。
二、实验内容
编写并调试程序,实现构造圆形、三角形、矩形类。每一个类都单独用一个文件存放。
圆形类:包括圆心、半径、周长、面积。
三角形类:包括三边、周长、面积。
矩形类:包括长、宽、周长、面积。
根据输入求图形的周长、面积。
实现文件的输入输出流,将求得的图形周长与面积保存在文件中,在适当时机可以读取。
实现容错处理,检测任何不合规的输入,并给出必要提示。
优化交互界面,让非专业者(陌生人)也能顺利完成运算。
三、程序清单
I.Abstract包
-
Point类(其实并没有用上)
package Abstract;
import java.io.Serializable;
public class Point implements Serializable {
private int x;//x坐标
private int y;//y坐标
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
II.FileOperate包
-
ReadFile接口 package FileOperate;
import Geometry.Objects;
public interface ReadFile {
Objects readFile(String ads);
}
-
WriteFile接口 package FileOperate;
import Geometry.Objects;
public interface WriteFile {
void writeFile(Objects obj, String ads);
}
III.Geometry包
-
Objects类(图形父类) package Geometry;
import Abstract.Point;
import java.io.Serializable;
public abstract class Objects implements Serializable {
private String name;//图形名称
static double PI = Math.PI;
private Point O = new Point();//几何图形中心
double peri;//图形周长
double area;//图形面积
public Point getO() {
return O;
}
public void setO(Point o) {
O = o;
}
public double getPeri() {
return peri;
}
public double getArea() {
return area;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-
Circle类 package Geometry;
public class Circle extends Objects {
private int r;//圆的半径
public Circle(){
setName("圆形");
}
public Circle(int r){
setName("圆形");
this.r = r;
peri = PI*r*2;
area = PI*r*r;
}
@Override
public double getPeri() {
return peri;
}
@Override
public double getArea() {
return area;
}
public double getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
-
Triangle类 package Geometry;
import Abstract.Point;
public class Triangle extends Objects {
private Point A = new Point();
private Point B = new Point();
private Point C = new Point();
private int a;
private int b;
private int c;
public Triangle(){
setName("三角形");
}
public Triangle(int a,int b,int c){
setName("三角形");
this.a = a;
this.b = b;
this.c = c;
peri = a+b+c;
double p = peri / 2;
area = Math.sqrt(p *(p -a)*(p -b)*(p -c));
}
@Override
public double getPeri() {
return peri;
}
@Override
public double getArea() {
return area;
}
}
-
Rectangle类 package Geometry;
import Abstract.Point;
public class Rectangle extends Objects {
private Point A = new Point();
private Point B = new Point();
private Point C = new Point();
private Point D = new Point();
private int length;
private int width;
public Rectangle(){
setName("矩形");
}
public Rectangle(int length,int width){
setName("矩形");
this.length = length;
this.width = width;
peri = length*2+width*2;
area = length*width;
}
@Override
public double getPeri() {
return peri;
}
@Override
public double getArea() {
return area;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
IV.WindowDraw包
-
MainWindow类 package WindowDraw;
import FileOperate.ReadFile;
import Geometry.Objects;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class MainWindow extends JFrame implements ReadFile {
//判断文件是否存在,存在为1,不存在为0
public static int state = 1;
JLabel s,p,rem;
TextField ad;
JButton cir, tri, rec, read;
public MainWindow(){
setVisible(true);//设置窗口是否可见
setTitle("几何图形");//设置窗口标题
setLocationRelativeTo(null);//使窗口居中
setBounds(100,100,800,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口的方法(点击右上角叉号关闭程序)
setLayout(null);
init();
}
void init(){
s = new JLabel("请选择想要构造的图形:");
setStyle(s,1,1,800,70,70);
cir = new JButton("圆形");
setStyle(cir,150,120,120,50,25);
tri = new JButton("三角形");
setStyle(tri,350,120,120,50,25);
rec = new JButton("矩形");
setStyle(rec,550,120,120,50,25);
p = new JLabel("或输入想要读取的文件名:");
setStyle(p,20,250,800,70,60);
read = new JButton("确定");
setStyle(read,340,450,120,50,25);
rem = new JLabel("文件不存在,请重新输入!");
rem.setForeground(Color.white);
setStyle(rem,270,400,300,50,20);
ad = new TextField();
ad.setBounds(250,350,300,30);
ad.setFont(new Font("宋体",Font.BOLD,20));
add(ad);
buttonAction(cir);
buttonAction(tri);
buttonAction(rec);
buttonAction(read);
}
void buttonAction(JButton b){
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(java.util.Objects.equals(b.getText(), "圆形")) {
dispose();
CirWindow win = new CirWindow();
}
else if(java.util.Objects.equals(b.getText(), "三角形")) {
dispose();
TriWindow win = new TriWindow();
}
else if(java.util.Objects.equals(b.getText(),"矩形")){
dispose();
RecWindow win = new RecWindow();
}
else if(java.util.Objects.equals(b.getText(),"确定")){
String addr = ad.getText()+".txt";
Objects obj = readFile(addr);
if(state==1){
ReadWindow win = new ReadWindow(obj);
rem.setForeground(Color.white);
ad.setText(null);
}else {
ad.setText(null);
rem.setForeground(Color.red);
state = 1;
}
}
}
});
}
void setStyle(JComponent jc,int x,int y,int width,int height,int size){
jc.setPreferredSize(new Dimension(width,height));
jc.setBounds(x,y,width,height);
jc.setFont(new Font("华文行楷", Font.BOLD,size));
add(jc);
}
@Override
public Objects readFile(String ads) {
File file = new File(ads);
try{
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Objects obj = (Objects) objectIn.readObject();
objectIn.close();
return obj;
} catch (IOException | ClassNotFoundException e) {
MainWindow.state = 0;
}
return null;
}
}
-
CirWindow类 package WindowDraw;
import Geometry.Circle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CirWindow extends JFrame implements ActionListener {
JLabel rem;
TextField r,peri,area;
JButton act,rtn,save,clear;
Circle cir;
public CirWindow(){
setVisible(true);//设置窗口是否可见
setTitle("圆形");//设置窗口标题
setLocationRelativeTo(null);//使窗口居中
setBounds(100,100,800,600);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);//关闭窗口的方法
setLayout(null);
init();
}
void init(){
setStyle(new JLabel("请输入半径:"),150,100,250,30,30);
r = new TextField();
r.setBounds(450,100,200,30);
r.setFont(new Font("宋体",Font.BOLD,20));
add(r);
setStyle(new JLabel("该圆形周长为:"),150,200,250,30,30);
peri = new TextField();
setTA(peri,450,200);
setStyle(new JLabel("该圆形面积为:"),150,300,250,30,30);
area = new TextField();
setTA(area,450,300);
rem = new JLabel();
rem.setForeground(Color.red);
setStyle(rem,470,130,300,50,20);
act = new JButton("确定");
rtn = new JButton("返回");
save = new JButton("保存");
clear = new JButton("清空");
setStyle(act,120,400,120,50,25);
setStyle(rtn,270,400,120,50,25);
setStyle(save,420,400,120,50,25);
setStyle(clear,570,400,120,50,25);
act.addActionListener(this);
rtn.addActionListener(this);
save.addActionListener(this);
clear.addActionListener(this);
}
void setStyle(JComponent jc,int x,int y,int width,int height,int size){
jc.setPreferredSize(new Dimension(width,height));
jc.setBounds(x,y,width,height);
jc.setFont(new Font("华文行楷", Font.BOLD,size));
add(jc);
}
void setTA(TextField tf,int x,int y){
tf.setBounds(x,y,200,30);
tf.setFont(new Font("宋体",Font.BOLD,20));
tf.setEditable(false);
add(tf);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==act){
try{
cir = new Circle(Integer.parseInt(r.getText()));
if(cir.getR()<=0){
rem.setText("请输入正整数!");
r.setText(null);
peri.setText(null);
area.setText(null);
}
else{
rem.setText(null);
peri.setText(String.valueOf(cir.getPeri()));
area.setText(String.valueOf(cir.getArea()));
rem.setForeground(Color.white);
}
} catch (NumberFormatException numberFormatException) {
rem.setText("请输入正整数!");
r.setText(null);
peri.setText(null);
area.setText(null);
}
}
if(e.getSource()==rtn){
dispose();
MainWindow win = new MainWindow();
}
if(e.getSource()==clear){
r.setText(null);
area.setText(null);
peri.setText(null);
}
if(e.getSource()==save){
SaveWindow win = new SaveWindow(cir);
}
}
}
-
TriWindow类 package WindowDraw;
import Geometry.Circle;
import Geometry.Triangle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TriWindow extends JFrame implements ActionListener {
JLabel rem;
TextField a,b,c,peri,area;
JButton act,rtn,save,clear;
Triangle tri;
public TriWindow(){
setVisible(true);//设置窗口是否可见
setTitle("三角形");//设置窗口标题
setLocationRelativeTo(null);//使窗口居中
setBounds(100,100,800,600);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);//关闭窗口的方法
setLayout(null);
init();
}
void init(){
setStyle(new JLabel("请输入a边长:"),150,60,250,30,30);
a = new TextField();
a.setBounds(450,60,200,30);
a.setFont(new Font("宋体",Font.BOLD,20));
add(a);
setStyle(new JLabel("请输入b边长:"),150,120,250,30,30);
b = new TextField();
b.setBounds(450,120,200,30);
b.setFont(new Font("宋体",Font.BOLD,20));
add(b);
setStyle(new JLabel("请输入c边长:"),150,180,250,30,30);
c = new TextField();
c.setBounds(450,180,200,30);
c.setFont(new Font("宋体",Font.BOLD,20));
add(c);
setStyle(new JLabel("该三角形周长为:"),150,260,270,30,30);
peri = new TextField();
setTA(peri,450,260);
setStyle(new JLabel("该三角形面积为:"),150,320,270,30,30);
area = new TextField();
setTA(area,450,320);
rem = new JLabel();
rem.setForeground(Color.red);
setStyle(rem,460,210,300,50,20);
act = new JButton("确定");
rtn = new JButton("返回");
save = new JButton("保存");
clear = new JButton("清空");
setStyle(act,120,400,120,50,25);
setStyle(rtn,270,400,120,50,25);
setStyle(save,420,400,120,50,25);
setStyle(clear,570,400,120,50,25);
act.addActionListener(this);
rtn.addActionListener(this);
save.addActionListener(this);
clear.addActionListener(this);
}
void setStyle(JComponent jc,int x,int y,int width,int height,int size){
jc.setPreferredSize(new Dimension(width,height));
jc.setBounds(x,y,width,height);
jc.setFont(new Font("华文行楷", Font.BOLD,size));
add(jc);
}
void setTA(TextField tf,int x,int y){
tf.setBounds(x,y,200,30);
tf.setFont(new Font("宋体",Font.BOLD,20));
tf.setEditable(false);
add(tf);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==act){
try{
tri = new Triangle(Integer.parseInt(a.getText()),Integer.parseInt(b.getText()),Integer.parseInt(c.getText()));
if(Double.isNaN(tri.getArea())||tri.getArea()==0){
rem.setText("请输入正确的三角形!");
a.setText(null);
b.setText(null);
c.setText(null);
peri.setText(null);
area.setText(null);
}
else{
rem.setText(null);
peri.setText(String.valueOf(tri.getPeri()));
area.setText(String.valueOf(tri.getArea()));
}
} catch (NumberFormatException numberFormatException) {
rem.setText("请输入正整数!");
a.setText(null);
b.setText(null);
c.setText(null);
peri.setText(null);
area.setText(null);
}
}
else if(e.getSource()==rtn){
dispose();
MainWindow win = new MainWindow();
}
else if(e.getSource()==clear){
a.setText(null);
b.setText(null);
c.setText(null);
peri.setText(null);
area.setText(null);
}
if(e.getSource()==save){
SaveWindow win = new SaveWindow(tri);
}
}
}
-
RecWindow类 package WindowDraw;
import Geometry.Rectangle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RecWindow extends JFrame implements ActionListener {
JLabel rem;
TextField l,w,peri,area;
JButton act,rtn,save,clear;
Rectangle rec;
public RecWindow(){
setVisible(true);//设置窗口是否可见
setTitle("矩形");//设置窗口标题
setLocationRelativeTo(null);//使窗口居中
setBounds(100,100,800,600);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);//关闭窗口的方法
setLayout(null);
init();
}
void init(){
setStyle(new JLabel("请输入长:"),150,60,250,30,30);
l = new TextField();
l.setBounds(450,60,200,30);
l.setFont(new Font("宋体",Font.BOLD,20));
add(l);
setStyle(new JLabel("请输入宽:"),150,140,250,30,30);
w = new TextField();
w.setBounds(450,140,200,30);
w.setFont(new Font("宋体",Font.BOLD,20));
add(w);
setStyle(new JLabel("该矩形周长为:"),150,220,250,30,30);
peri = new TextField();
setTA(peri,450,220);
setStyle(new JLabel("该矩形面积为:"),150,300,250,30,30);
area = new TextField();
setTA(area,450,300);
rem = new JLabel();
rem.setForeground(Color.red);
setStyle(rem,460,170,300,50,20);
act = new JButton("确定");
rtn = new JButton("返回");
save = new JButton("保存");
clear = new JButton("清空");
setStyle(act,120,400,120,50,25);
setStyle(rtn,270,400,120,50,25);
setStyle(save,420,400,120,50,25);
setStyle(clear,570,400,120,50,25);
act.addActionListener(this);
rtn.addActionListener(this);
save.addActionListener(this);
clear.addActionListener(this);
}
void setStyle(JComponent jc,int x,int y,int width,int height,int size){
jc.setPreferredSize(new Dimension(width,height));
jc.setBounds(x,y,width,height);
jc.setFont(new Font("华文行楷", Font.BOLD,size));
add(jc);
}
void setTA(TextField tf,int x,int y){
tf.setBounds(x,y,200,30);
tf.setFont(new Font("宋体",Font.BOLD,20));
tf.setEditable(false);
add(tf);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==act){
try{
rec = new Rectangle(Integer.parseInt(l.getText()),Integer.parseInt(w.getText()));
if(rec.getLength()<rec.getWidth()){
rem.setText("矩形的长不能小于宽!");
l.setText(null);
w.setText(null);
peri.setText(null);
area.setText(null);
}
else if(rec.getLength()<=0||rec.getWidth()<=0){
rem.setText("请输入正整数!");
l.setText(null);
w.setText(null);
peri.setText(null);
area.setText(null);
}
else{
rem.setText(null);
peri.setText(String.valueOf(rec.getPeri()));
area.setText(String.valueOf(rec.getArea()));
}
} catch (NumberFormatException numberFormatException) {
rem.setText("请输入正整数!");
l.setText(null);
w.setText(null);
peri.setText(null);
area.setText(null);
}
}
if(e.getSource()==rtn){
dispose();
MainWindow win = new MainWindow();
}
if(e.getSource()==clear){
l.setText(null);
w.setText(null);
peri.setText(null);
area.setText(null);
}
if(e.getSource()==save&&l.getText()!=null&&w.getText()!=null){
SaveWindow win = new SaveWindow(rec);
}
}
}
-
ReadWindow类 package WindowDraw;
import Geometry.*;
import javax.swing.*;
import java.awt.*;
public class ReadWindow extends JFrame {
Objects obj;
JLabel s,pp,aa;
TextField peri,area;
public ReadWindow(Objects obj){
this.obj = obj;
setVisible(true);//设置窗口是否可见
setTitle("读取图形");//设置窗口标题
setLocationRelativeTo(null);//使窗口居中
setBounds(100,100,400,400);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);//关闭窗口的方法
setLayout(null);
init();
}
void init(){
s = new JLabel("该图形为"+obj.getName());
setStyle(s,1,1,400,30,30);
pp = new JLabel("周长为:");
setStyle(pp,1,100,150,30,30);
aa = new JLabel("面积为:");
setStyle(aa,1,200,150,30,30);
peri = new TextField();
area = new TextField();
setTA(peri,160,100,obj.getPeri());
setTA(area,160,200,obj.getArea());
}
void setStyle(JComponent jc,int x,int y,int width,int height,int size){
jc.setPreferredSize(new Dimension(width,height));
jc.setBounds(x,y,width,height);
jc.setFont(new Font("华文行楷", Font.BOLD,size));
add(jc);
}
void setTA(TextField tf,int x,int y,double info){
tf.setBounds(x,y,200,30);
tf.setFont(new Font("宋体",Font.BOLD,20));
tf.setText(String.valueOf(info));
tf.setEditable(false);
add(tf);
}
}
-
SaveWindow类 package WindowDraw;
import FileOperate.WriteFile;
import Geometry.Objects;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SaveWindow extends JFrame implements ActionListener, WriteFile {
TextField ad;
JButton save,cancel;
Objects obj;
public SaveWindow(Objects obj){
this.obj = obj;
setVisible(true);//设置窗口是否可见
setTitle("");//设置窗口标题
setLocationRelativeTo(null);//使窗口居中
setBounds(100,100,400,400);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);//关闭窗口的方法
setLayout(null);
init();
}
void init(){
setStyle(new JLabel("请输入保存地址:"),50,100,300,30,30);
ad = new TextField();
setTA(ad,100,170);
save = new JButton("保存");
cancel = new JButton("取消");
setStyle(save,70,250,120,50,25);
setStyle(cancel,200,250,120,50,25);
save.addActionListener(this);
cancel.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==save){
try{
String addr = ad.getText()+".txt";
writeFile(obj,addr);
} catch (Exception exception) {
ad.setText("请输入正确的地址");
}
SaveSuccess win = new SaveSuccess();
dispose();
}
else if(e.getSource()==cancel){
dispose();
}
}
void setStyle(JComponent jc,int x,int y,int width,int height,int size){
jc.setPreferredSize(new Dimension(width,height));
jc.setBounds(x,y,width,height);
jc.setFont(new Font("华文行楷", Font.BOLD,size));
add(jc);
}
void setTA(TextField tf,int x,int y){
tf.setBounds(x,y,200,30);
tf.setFont(new Font("宋体",Font.BOLD,20));
add(tf);
}
@Override
public void writeFile(Objects obj, String ads) {
File file = new File(ads);
try{
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(obj);
objectOut.close();
} catch (IOException e) {
System.out.println("请输入正确的地址");
}
}
}
-
SaveSuccess类 package WindowDraw;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;
public class SaveSuccess extends JFrame {
JButton goon;
public SaveSuccess(){
setVisible(true);//设置窗口是否可见
setTitle("保存图形");//设置窗口标题
setLocationRelativeTo(null);//使窗口居中
setBounds(100,100,300,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);//关闭窗口的方法
setLayout(null);
init();
}
void init(){
setStyle(new JLabel("保存成功!"),80,90,200,30,30);
goon = new JButton("确定");
setStyle(goon,90,170,120,50,25);
buttonAction(goon);
}
void buttonAction(JButton b){
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Objects.equals(b.getText(), "确定")) {
dispose();
}
}
});
}
void setStyle(JComponent jc,int x,int y,int width,int height,int size){
jc.setPreferredSize(new Dimension(width,height));
jc.setBounds(x,y,width,height);
jc.setFont(new Font("华文行楷", Font.BOLD,size));
add(jc);
}
}
V.Action类(主函数所在)
import WindowDraw.MainWindow;
public class Action {
public static void main(String[] args) {
MainWindow win = new MainWindow();
}
}
四、程序测试
程序功能实现截图如下
|