一、题目要求
给出数字序列A、B的值和逻辑函数F的表达式(与、或、非、 异或、同或等),生成A、B和F的波形图。注:A、B序列长 度最大值不小于16。
- 实现运算过程
- 输入长度显示及错误提示
- 输出波形界面及其他交互设计
二、设计流程
2.1 设计流程
2.2 实现软件结果图
2.2.1 初始化界面
2.2.2 长度不同报错
2.2.3 输入波形数据报错
2.2.4 正确输入波形数据及显示波形(单击按钮与)
三、关键源码及注释
3.1 主函数解释
运行此主函数,可以运行波形生成器,进入程序。
package wave;
public class main {
public static void main(String[] args) {
new window("波形生成器");}}
3.2 窗口界面解释
此文件可以绘制窗口并且进行相应的错误提示。
package wave;
import 导入相关的包
public class window extends JFrame implements ActionListener {
public window(String title) {
//对窗口构造函数进行相关属性设计
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(title);
this.setLayout(new BorderLayout());
this.setBounds(70, 10, width, height);
//对窗口的不同部分进行分别设计(以pA为例)
JPanel pA = new JPanel();
pA.setLayout(flow);
JLabel a = new JLabel("A:");
a.setFont(字体2);
pA.add(a);
pA.setBackground(Color.cyan);
pA.setBounds(0, 0, width, height / 4);
add(pA); }
//对按钮点击进行监听,以此来进行不同的结果反应
public void actionPerformed(ActionEvent e) {
JButton curr = (JButton) e.getSource();
//将输入的文本转换成int数组,q用来监听是否出现规定以外的波形数据(0,1以外的字符)
for (i = 0; i < a1.length; i++)
switch (a1[i]) {
case '0': a2[i] = 0; break;
case '1': a2[i] = 1; break;
default: q = true;}
//把B的波形数据进行量化
for (i = 0; i < b1.length; i++)
switch (b1[i]) {
case '0': b2[i] = 0; break;
case '1':b2[i] = 1; break;
default: q = true;}
//按钮反映举例(以“与”按钮为例)
if (curr == 与) {
if (q) tff.setText("A和B输入数据不对,应为0或1的波形数据");
else if (a1.length == b1.length) {
//与 相当于A、B两个波形对应位置的两个数相乘
//或 相当于A、B两个波形对应位置的两个数相加(如果大于等于1,结果为1)
//同或 相当于两个波形对应位置的两个数相加判断是否为1,如果不是则为0
//异或 相当于两个波形对应位置的两个数相加判断是否为1,如果不是则为1
for (i = 0; i < a1.length; i++) {
f[i] = a2[i] * b2[i];
a = a + f[i];}
//绘制相应的波形数据
new Draw("输出波形", a2,b2,f);
//界面交互,对输出结果进行相应的提示
tff.setText("波形数据为: "+a);
ta.setText("您输入的信号A的长度为:" + a1.length);
tb.setText("您输入的信号B的长度为:" + b1.length);
} else if (a1.length != b1.length) {
tff.setText("A和B输入长度不同");
ta.setText("您输入的信号A的长度为:" + a1.length);
tb.setText("您输入的信号B的长度为:" + b1.length);}}
3.3 绘制波形函数
此文件可以把输入的波形数据进行相应的转换,并且在窗口上绘制出来。
import 导入相应的包
public class Draw extends JFrame {
public Draw(String name, int[] a1, int[] b1, int[] c1) {
//绘制波形,并将波形添加到窗口上
ShapePanel h = new ShapePanel();
this.getContentPane().add(h);
//设置界面属性
setVisible(true);
setTitle(name);
setSize(width, height);
setLocation(150, 100);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}
public static class ShapePanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
//设置标签,对绘制的三个波形进行名字提示
JLabel labela = new JLabel("输出波形A:");
JLabel labelb = new JLabel("输出波形B:");
JLabel labelf = new JLabel("输出波形F:");
for (j = 0; j < 3; j++) {
//根据传入进来的坐标进行相应的位置转换,并在窗口上进行相应位置的绘制
for (i = 0; i < w.length; i++) {
if (i == 0) {
if (w[i] == 0)
g2.drawLine(0, height / 6 * 2 * (j + 1) - height / 10, width / 16, height / 6 * 2 * (j + 1) - height / 10);
else
g2.drawLine(0, height / 6 * (2 * j + 1) - height / 10, width / 16, height / 6 * (2 * j + 1) - height / 10);
} else {
if (w[i] == 0) {
if (w[i - 1] == 0)
g2.drawLine(width / 16 * i, height / 6 * 2 * (j + 1) - height / 10, width / 16 * (i + 1), height / 6 * 2 * (j + 1) - height / 10);
else {
g2.drawLine(width / 16 * i, height / 6 * 2 * (j + 1) - height / 10, width / 16 * i, height / 6 * (2 * j + 1) - height / 10);
g2.drawLine(width / 16 * i, height / 6 * 2 * (j + 1) - height / 10, width / 16 * (i + 1), height / 6 * 2 * (j + 1) - height / 10);
}
} else {
if (w[i - 1] == 1)
g2.drawLine(width / 16 * i, height / 6 * (2 * j + 1) - height / 10, width / 16 * (i + 1), height / 6 * (2 * j + 1) - height / 10);
else {
g2.drawLine(width / 16 * i, height / 6 * 2 * (j + 1) - height / 10, width / 16 * i, height / 6 * (2 * j + 1) - height / 10);
g2.drawLine(width / 16 * i, height / 6 * (2 * j + 1) - height / 10, width / 16 * (i + 1), height / 6 * (2 * j + 1) - height / 10);
}
}
}
}
}
}
}
}
四、遇到的问题及相应的解决方法
4.1 窗口设计总是不按照你所设计的位置来
- 1)使用setBounds()方法来进行强制定位,设置好所要添加的位置,及大小;
- 2)添加的时候要注意添加的顺序,前后添加的顺序不同,最终显示结果不同,会产生覆盖的效果;
- 3)在Jframe添加Jpanel的时候,最后一个添加上去可能会找不到在哪里显示,解决方法是在此panel之下再添加一个,原来的最后一个的位置给限制住,这样就会在规定位置进行显示;
4.2 不同Java文件之间传输数据的时候可能无法传输,或者数据丢失
可以使用static关键字,由此可以一次初始化之后以后的也可以使用。
五、设计总结
在对数字波形进行逻辑操作的时候需要把问题数字化,抽象出其中的数学原理,以此来进行相应的数学建模,简化问题难度。
在对整个的流程设计的时候,要对问题考虑全面,列出所有可能出现的问题,并且有相对应的解决方案。
解决项目的时候可以把问题进行模块化,逐步减小缩小面对的问题,然后再把处理的模块集成起来完成整个项目操作。
完整源码
层次结构
Draw.java
package wave;
import javax.swing.*;
import java.awt.*;
public class Draw extends JFrame {
static int[] a;
static int[] b;
static int[] c;
static String name;
static int width = 1200;
static int height = 600;
public Draw(String name, int[] a1, int[] b1, int[] c1) {
a = a1;
b = b1;
c = c1;
this.name = name;
ShapePanel h = new ShapePanel();
this.getContentPane().add(h);
setVisible(true);
setTitle(name);
setSize(width, height);
setLocation(150, 100);
//设置是否可以更改大小
setResizable(false);
//设置关闭方式
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static class ShapePanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.white);
JLabel labela = new JLabel("输出波形A:");
JLabel labelb = new JLabel("输出波形B:");
JLabel labelf = new JLabel("输出波形F:");
labela.setBounds(width/2,0,100,100);
labelb.setBounds(width/2,height/6*2,100,100);
labelf.setBounds(width/2,height/6*4,100,100);
add(labela);
add(labelb);
add(labelf);
Graphics2D g2 = (Graphics2D) g; //g是Graphics对象
Stroke stroke = new BasicStroke(10.0f);
g2.setStroke(stroke);
int j;
int[] w = new int[a.length];
for (j = 0; j < 3; j++) {
if (j == 0) {w = a;g2.setColor(Color.cyan);}
else if (j == 1) {w = b;g2.setColor(Color.orange);}
else if (j == 2) {w = c;g2.setColor(Color.green);}
int i;
for (i = 0; i < w.length; i++) {
if (i == 0) {
if (w[i] == 0)
g2.drawLine(0, height / 6 * 2 * (j + 1) - height / 10, width / 16, height / 6 * 2 * (j + 1) - height / 10);
else
g2.drawLine(0, height / 6 * (2 * j + 1) - height / 10, width / 16, height / 6 * (2 * j + 1) - height / 10);
} else {
if (w[i] == 0) {
if (w[i - 1] == 0)
g2.drawLine(width / 16 * i, height / 6 * 2 * (j + 1) - height / 10, width / 16 * (i + 1), height / 6 * 2 * (j + 1) - height / 10);
else {
g2.drawLine(width / 16 * i, height / 6 * 2 * (j + 1) - height / 10, width / 16 * i, height / 6 * (2 * j + 1) - height / 10);
g2.drawLine(width / 16 * i, height / 6 * 2 * (j + 1) - height / 10, width / 16 * (i + 1), height / 6 * 2 * (j + 1) - height / 10);
}
} else {
if (w[i - 1] == 1)
g2.drawLine(width / 16 * i, height / 6 * (2 * j + 1) - height / 10, width / 16 * (i + 1), height / 6 * (2 * j + 1) - height / 10);
else {
g2.drawLine(width / 16 * i, height / 6 * 2 * (j + 1) - height / 10, width / 16 * i, height / 6 * (2 * j + 1) - height / 10);
g2.drawLine(width / 16 * i, height / 6 * (2 * j + 1) - height / 10, width / 16 * (i + 1), height / 6 * (2 * j + 1) - height / 10);
}
}
}
}
}
}
}
}
main.java
package wave;
public class main {
public static void main(String[] args) {
new window("波形生成器");
}
}
package wave;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class window extends JFrame implements ActionListener {
int width = 1400;
int height = 800;
String inputa;
Font 字体 = new Font("楷体", 5, 50);
Font 字体2 = new Font("楷体", 5, 100);
Font 字体3 = new Font("楷体", 1, 40);
JButton 与 = new JButton("与");
JButton 或 = new JButton("或");
JButton 同或 = new JButton("同或");
JButton 异或 = new JButton("异或");
TextComponent tfa = new TextField(13);
TextComponent tfb = new TextField(13);
JLabel tff = new JLabel("请在A、B输入框内输入A和B波形数据,然后选择逻辑运算");
JLabel ta = new JLabel();
JLabel tb = new JLabel();
int[] a2;
int[] b2;
int[] f;
public window(String title) {
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(title);
this.setLayout(new BorderLayout());
this.setBounds(70, 10, width, height);
FlowLayout flow = new FlowLayout();
//对齐值
flow.setAlignment(FlowLayout.LEADING);
JPanel pA = new JPanel();
pA.setLayout(flow);
JLabel a = new JLabel("A:");
a.setFont(字体2);
pA.add(a);
pA.setBackground(Color.cyan);
pA.setBounds(0, 0, width, height / 4);
JPanel dA = new JPanel();
dA.setBounds(width / 2, 0, width / 2, height / 4);
dA.setBackground(Color.MAGENTA);
ta.setFont(字体);
dA.add(ta);
tfa.setFont(字体3);
pA.add(tfa);
inputa = tfa.getText();
JPanel pB = new JPanel();
pB.setBackground(Color.BLUE);
pB.setBounds(0, height / 4, width, height / 4);
pB.setLayout(flow);
JLabel b = new JLabel("B:");
JPanel dB = new JPanel();
dB.setBounds(width / 2, height / 4, width / 2, height / 4);
dB.setBackground(Color.ORANGE);
tb.setFont(字体);
dB.add(tb);
pB.add(b);
b.setFont(字体2);
tfb.setFont(字体3);
pB.add(tfb);
JPanel butt = new JPanel();
butt.setBackground(Color.green);
butt.setBounds(0, height / 2, width, height / 4);
butt.setLayout(new GridLayout(1, 4));
与.setFont(字体);
与.addActionListener(this);
异或.setFont(字体);
异或.addActionListener(this);
同或.setFont(字体);
同或.addActionListener(this);
或.setFont(字体);
或.addActionListener(this);
butt.add(与);
butt.add(或);
butt.add(异或);
butt.add(同或);
JPanel pF = new JPanel();
pF.setBackground(Color.green);
pF.setBounds(0, height / 4 * 3, width, height / 4);
pF.setLayout(flow);
JLabel ff = new JLabel("F:");
ff.setFont(字体2);
pF.add(ff);
JPanel dF = new JPanel();
dF.setBounds(width / 2, height / 4 * 3, width / 2, height / 4);
dF.setBackground(Color.YELLOW);
tff.setFont(字体3);
pF.add(tff);
JPanel p = new JPanel();
add(dA);
add(pA);
add(dB);
add(pB);
add(butt);
//add(dF);
add(pF);
add(p);
}
public void actionPerformed(ActionEvent e) {
JButton curr = (JButton) e.getSource();
String a = tfa.getText();
char[] a1 = a.toCharArray();
String b = tfb.getText();
char[] b1 = b.toCharArray();
a2 = new int[a1.length];
b2 = new int[b1.length];
f=new int[a1.length];
boolean q = false;
//将输入的文本转换成int数组
int i;
for (i = 0; i < a1.length; i++)
switch (a1[i]) {
case '0':
a2[i] = 0;
break;
case '1':
a2[i] = 1;
break;
default:
q = true;
}
for (i = 0; i < b1.length; i++)
switch (b1[i]) {
case '0':
b2[i] = 0;
break;
case '1':
b2[i] = 1;
break;
default:
q = true;
}
if (curr == 与) {
a = "";
if (q) tff.setText("A和B输入数据不对,应为0或1的波形数据");
else if (a1.length == b1.length) {
for (i = 0; i < a1.length; i++) {
f[i] = a2[i] * b2[i];
a = a + f[i];
}
new Draw("输出波形", a2,b2,f);
tff.setText("波形数据为: "+a);
ta.setText("您输入的信号A的长度为:" + a1.length);
tb.setText("您输入的信号B的长度为:" + b1.length);
} else if (a1.length != b1.length) {
tff.setText("A和B输入长度不同");
ta.setText("您输入的信号A的长度为:" + a1.length);
tb.setText("您输入的信号B的长度为:" + b1.length);
}
}
if (curr == 或) {
a = "";
if (q) tff.setText("A和B输入数据不对,应为0或1的波形数据");
else if (a1.length == b1.length) {
for (i = 0; i < a1.length; i++) {
if (a2[i] + b2[i] == 0) f[i] = 0;
else f[i] = 1;
a = a + f[i];
}
new Draw("输出波形", a2,b2,f);
tff.setText("波形数据为: "+a);
ta.setText("您输入的信号A的长度为:" + a1.length);
tb.setText("您输入的信号B的长度为:" + b1.length);
} else if (a1.length != b1.length) tff.setText("A和B输入长度不同");
}
if (curr == 异或) {
a = "";
if (q) tff.setText("AA和B输入数据不对,应为0或1的波形数据");
else if (a1.length == b1.length) {
for (i = 0; i < a1.length; i++) {
if (a2[i] + b2[i] == 1) f[i] = 1;
else f[i] = 0;
a = a + f[i];
}
new Draw("输出波形", a2,b2,f);
tff.setText("波形数据为: "+a);
ta.setText("您输入的信号A的长度为:" + a1.length);
tb.setText("您输入的信号B的长度为:" + b1.length);
} else if (a1.length != b1.length) tff.setText("A和B输入长度不同");
}
if (curr == 同或) {
a = "";
if (q) tff.setText("A和B输入数据不对,应为0或1的波形数据");
else if (a1.length == b1.length) {
for (i = 0; i < a1.length; i++) {
if (a2[i] + b2[i] == 1) f[i] = 0;
else f[i] = 1;
a = a + f[i];
}
new Draw("输出波形", a2,b2,f);
tff.setText("波形数据为: "+a);
ta.setText("您输入的信号A的长度为:" + a1.length);
tb.setText("您输入的信号B的长度为:" + b1.length);
} else if (a1.length != b1.length) tff.setText("A和B输入长度不同");
}
}
}