package Progect;
public class Data {
//设置存储的值
private static String y1="3";
private static String y2="3";
private static String x1="-1";
private static String x2="4";
Data() {
}
public String getY1() {
return y1;
}
public void setY1(String y1) {
this.y1 = y1;
}
public String getY2() {
return y2;
}
public void setY2(String y2) {
this.y2 = y2;
}
public String getX1() {
return x1;
}
public void setX1(String x1) {
this.x1 = x1;
}
public String getX2() {
return x2;
}
public void setX2(String x2) {
this.x2 = x2;
}
}
package Progect;
public class Mainclass {
public static void main(String[] args) {
new MainJFrame();
}
}
package Progect;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainJFrame extends JFrame{
private Data data=new Data();
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
//主窗体的宽高设置
private int width = 1500;
private int height =500;
//窗体出现的位置
private int x= 300;
private int y= 100;
//设置右边的绘图面板
private MyJpanel MyJpanelRight;
public MyJpanel GetMyJpanelRight() {
return MyJpanelRight;
}
//设置左边的控制面板
private MyControl MyJpanelLeft;
public MainJFrame() {
this.setTitle("动态显示正余弦函数");//设置标题
//this.setBackground(Color.WHITE);
this.setSize(width, height);
this.setLocation(x, y);
//设置主界面的布局方式为上下左右中
//this.setLayout(new GridLayout(0,2));
this.setLayout(null);
//
MyJpanelRight = new MyJpanel(this);
MyJpanelLeft = new MyControl(this);
this.add(MyJpanelLeft, BorderLayout.WEST);
this.add(MyJpanelRight, BorderLayout.EAST);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭
}
}
package Progect;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyControl extends JPanel implements ActionListener{
private MainJFrame jf;
//设置Label
private JLabel jY1;
private JLabel jY2;
// 设置两个Jlabelsin or cos;
private JLabel jSin;
private JLabel jCos;
// 设置两个可以输入的文本框
private JTextField jTexty1;
private JTextField jTexty2;
//设置按钮
private JButton jby1;
private JButton jby2;
//设置x轴的方法
private JLabel x = new JLabel("X的区间为:");
private JTextField Textx1 ;
private JTextField Textx2 ;
private JButton jb3;
public MyControl(MainJFrame jf) {
this.jf=jf;
//设置jy设置jx;
this.setLayout(null);
this.setSize(500,500);
jY1 = new JLabel("Y=");
jY2 = new JLabel("Y=");
this.setBackground(Color.blue);
jY1.setSize(100, 100);
jY2.setSize(100,100);
jY1.setLocation(20, 40);
jY2.setLocation(20, 100);
jY1.setFont(new Font("黑体", Font.BOLD,40));//设置字体
jY2.setFont(new Font("黑体", Font.BOLD,40));
//设置位置
jSin = new JLabel("SinX");
jCos = new JLabel("CosX");
jSin.setFont(new Font("宋体",Font.BOLD,40));
jCos.setFont(new Font("宋体",Font.BOLD,40));
jSin.setSize(100,100);
jCos.setSize(100,100);
jSin.setLocation(160, 40);
jCos.setLocation(160, 100);
//jY.setVisible(true);
jTexty1 = new JTextField("1");
jTexty2 = new JTextField("1");
jTexty1.setSize(60,30);
jTexty2.setSize(60,30);
jTexty1.setLocation(60,70 );
jTexty2.setLocation(60, 130);
jby1 = new JButton("ok1");
jby2 = new JButton("ok2");
jby1.setSize(60,40);
jby2.setSize(60,40);
jby1.setLocation(260,70);
jby2.setLocation(260,130);
x.setSize(250,100);
x.setFont(new Font("黑体",Font.BOLD,40));
x.setLocation(40, 200);
Textx1 = new JTextField("2");
Textx2 = new JTextField("2");
Textx1.setSize(60,30);
Textx2.setSize(60,30);
Textx1.setLocation(40, 300);
Textx2.setLocation(160, 300);
jb3 = new JButton();
jb3.setSize(100,40);
jb3.setText("play");
jb3.setLocation(220, 360);
this.add(jY1);
this.add(jY2);
this.add(jSin);
this.add(jCos);
this.add(jTexty1);
this.add(jTexty2);
this.add(jby1);
this.add(jby2);
this.add(x);
this.add(Textx1);
this.add(Textx2);
this.add(jb3);
//给按钮增加监听事件
jby1.addActionListener(this);
jby2.addActionListener(this);
jb3.addActionListener(this);
this.setVisible(true);
//
//jSin = new("");
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="ok1") {
jf.getData().setY1(jTexty1.getText());
jf.GetMyJpanelRight().paint(jf.GetMyJpanelRight().getGraphics());
}else if(e.getActionCommand()=="ok2"){
jf.getData().setY1(jTexty2.getText());
jf.GetMyJpanelRight().paint(jf.GetMyJpanelRight().getGraphics());
}else if(e.getActionCommand()== "play") {
jf.getData().setX1(Textx1.getText());
jf.getData().setX2(Textx2.getText());
jf.GetMyJpanelRight().paint(jf.GetMyJpanelRight().getGraphics());
}
}
}
package Progect;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class MyJpanel extends JPanel {
private MainJFrame jf;
private int number;
boolean isStandalone = false;
private static int SCALE_X = 40; // X轴缩放倍数
private static int SCALE_Y = 30; // Y轴缩放倍数
private static int ORIGIN_X = 10; // 原点X
private static int ORIGIN_Y = 0; // 原点Y
private static int END_ARC = 360 * 2;// 画多长
public MyJpanel(MainJFrame jf) {
this.jf = jf;
this.setLocation(500, 0);
this.setSize(1000, 500);
}
public void setBackground(Color bg) {
super.setBackground(Color.BLACK);
}
public void paint(Graphics g) {
double ox = 0, oy = 0, x = 0, y = 0, arc = 0;
super.paint(g);
g.setColor(Color.WHITE);
int x1 = Integer.valueOf(jf.getData().getX1());
int x2 = Integer.valueOf(jf.getData().getX2());
int begin = 0 + x1 * 180;
int end = 0 + x2 * 180;
ORIGIN_Y = this.getHeight() / 2;
// 画坐标轴
g.drawLine(ORIGIN_X, ORIGIN_Y, this.getWidth(), ORIGIN_Y); // 横轴
g.drawLine(ORIGIN_X, 0, ORIGIN_X, this.getHeight()); // 纵轴
// 每90度画个标尺
for (int i = begin; i <= end; i += 90) {
arc = Math.PI * i * 2 / 360;
x = ORIGIN_X + arc * SCALE_X;
g.drawLine((int) x, ORIGIN_Y - 8, (int) x, ORIGIN_Y);
}
for (int i = -20; i < 20; i += 1) {
arc = i;
y = ORIGIN_Y + arc * SCALE_Y;
g.drawLine(ORIGIN_X - 8, (int) y, ORIGIN_X, (int) y);
}
arc = Math.PI * 2 / 360;
// System.out.println(x);
// for (int i = begin; i <= end; i += 90) {
//
// x = ORIGIN_X +arc+180*i*SCALE_X;
//
// g.drawString("0",(int)x,ORIGIN_Y+10);
//
//
// }
// //画x轴刻度尺
// x= ORIGIN_X ;
// g.drawString("0",(int)x,ORIGIN_Y+10);
// x = ORIGIN_X+arc *90* SCALE_X;
// g.drawString("π/2",(int)x,ORIGIN_Y+10);
// x = ORIGIN_X+180*arc * SCALE_X;
// g.drawString("π",(int)x,ORIGIN_Y+10);
// x = ORIGIN_X+270* arc * SCALE_X;
// g.drawString("3π/2",(int)x,ORIGIN_Y+10);
// x = ORIGIN_X+360*arc * SCALE_X;;
// g.drawString("2π",(int)x,ORIGIN_Y+10);
// x = ORIGIN_X+450*arc * SCALE_X;
// g.drawString("5/2π",(int)x,ORIGIN_Y+10);
// x = ORIGIN_X+540*arc * SCALE_X;
// g.drawString("3π",(int)x,ORIGIN_Y+10);
// x = ORIGIN_X+630*arc * SCALE_X;
// g.drawString("7π/2",(int)x,ORIGIN_Y+10);
// x = ORIGIN_X+720*arc * SCALE_X;
// g.drawString("4π",(int)x,ORIGIN_Y+10);
// 画y轴刻度尺
y = ORIGIN_Y + 1 * SCALE_Y;
g.drawString("-1", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + 2 * SCALE_Y;
g.drawString("-2", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + 3 * SCALE_Y;
g.drawString("-3", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + 4 * SCALE_Y;
g.drawString("-4", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + 5 * SCALE_Y;
g.drawString("-5", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + 6 * SCALE_Y;
g.drawString("-6", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + 7 * SCALE_Y;
g.drawString("-7", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + -1 * SCALE_Y;
g.drawString("1", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + -2 * SCALE_Y;
g.drawString("2", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + -3 * SCALE_Y;
g.drawString("3", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + -4 * SCALE_Y;
g.drawString("4", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + -5 * SCALE_Y;
g.drawString("5", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + -6 * SCALE_Y;
g.drawString("6", ORIGIN_X - 8, (int) y);
y = ORIGIN_Y + -7 * SCALE_Y;
g.drawString("7", ORIGIN_X - 8, (int) y);
// 画正弦曲线
System.out.println("开始画曲线");
g.setColor(Color.RED);
double b1=100;
double a1=b1/100;
double b2=0;
double a2=b2/100;
double b3=100 * Integer.valueOf(jf.getData().getY1());
double a3=b3/100;
double b4=0;
double a4=b4/100;
arc = Math.PI * begin * 2 / 360;
ox = ORIGIN_X + arc * SCALE_X;
oy = ORIGIN_Y + (a1 * Math.sin(a3 * arc + a2) + a4) * SCALE_Y;
for (int i = begin; i <= end; i += 10) {
arc = Math.PI * i * 2 / 360;
x = ORIGIN_X + arc * SCALE_X;
y = ORIGIN_Y + (a1 * Math.sin(a3 * arc + a2) + a4) * SCALE_Y;
if (arc > 0) {
g.drawLine((int) ox, (int) oy, (int) x, (int) y);
//System.out.println("sss");
}
ox = x;
oy = y;
}
g.setColor(Color.blue);
b1=100;
a1=b1/100;
b2=290;
a2=b2/100;
b3=100 * Integer.valueOf(jf.getData().getY2());
a3=b3/100;
b4=0;
a4=b4/100;
arc = Math.PI * begin * 2 / 360;
ox = ORIGIN_X + arc * SCALE_X;
oy = ORIGIN_Y + (a1 * Math.sin(a3 * arc + a2) + a4) * SCALE_Y;
for (int i = begin; i <= end; i += 10) {
arc = Math.PI * i * 2 / 360;
x = ORIGIN_X + arc * SCALE_X;
y = ORIGIN_Y + (a1 * Math.sin(a3 * arc + a2) + a4) * SCALE_Y;
if (arc > 0) {
g.drawLine((int) ox, (int) oy, (int) x, (int) y);
//System.out.println("sss");
}
ox = x;
oy = y;
}
}
}
以上代码仅供学习者进行参考,算法设计并未给出,如需想要一步执行获取项目链接:https://pan.baidu.com/s/1aFNohw3xMVbkwvQmBP1_OA