import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class CurveSystem extends JPanel implements Runnable {
// Runnable 是实现多线程的一种方式
private static Lock lock = new ReentrantLock();
private static final int WIDTH = 800; // 定义窗口宽度
private static final int HEIGHT = 600; // 定义窗口高度
private static final int DELAY = 50; // 定义每次更新的延迟时间(毫秒)
private static final int MAX_X = 800; // 定义x轴最大值
private static final int MIN_X = -800; // 定义x轴最小值
private static final int MAX_Y = 600; // 定义y轴最大值
private static final int MIN_Y = -600; // 定义y轴最小值
private static final int STEP = 1; // 定义x轴每个步长的距离
private static final Color[] COLORS = {Color.BLUE, Color.GREEN, Color.magenta, Color.CYAN}; // 定义每种曲线的颜色
private static final String[] CURVE_NAMES = {"sin(x)", "cos(x)", "x", "x^2"}; // 定义每种曲线的名称
private static final int[] CURVE_TYPES = {0, 1, 2, 3}; // 定义每种曲线的类型编号
private int currentCurveIndex = 0; // 当前显示的曲线编号
private List<Double> xValues = new ArrayList<>(); // 存储x轴上的值
private List<Double> yValues = new ArrayList<>(); // 存储y轴上的值
private Thread thread; // 线程对象
private boolean running = false; // 是否运行标志
private int scroll = 0; // x轴滚动条的位置
public CurveSystem() {
setPreferredSize(new java.awt.Dimension(WIDTH, HEIGHT)); // 设置窗口大小
// setBackground(Color.WHITE); // 设置背景颜色为白色
for (int i = MIN_X; i <= MAX_X; i += STEP) { // 初始化x轴上的值
xValues.add((double) i);
yValues.add(0.0);
}
JButton sinButton = new JButton("sin(x)"); // 创建sin(x)按钮
sinButton.addActionListener(e -> currentCurveIndex = 0); // 添加按钮点击事件
JButton cosButton = new JButton("cos(x)"); // 创建cos(x)按钮
cosButton.addActionListener(e -> currentCurveIndex = 1); // 添加按钮点击事件
JButton linearButton = new JButton("x"); // 创建x按钮
linearButton.addActionListener(e -> currentCurveIndex = 2); // 添加按钮点击事件
JButton quadraticButton = new JButton("x^2"); // 创建x^2按钮
quadraticButton.addActionListener(e -> currentCurveIndex = 3); // 添加按钮点击事件
JButton resetButton = new JButton("Reset x"); // 创建重置x按钮
resetButton.addActionListener(e -> scroll = 0); // 添加按钮点击事件
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
startButton.addActionListener((e) -> {
start();
stopButton.setEnabled(true);
startButton.setEnabled(false);
});
stopButton.addActionListener((e) -> {
stop();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}); // 添加按钮点击事件
startButton.setEnabled(false);
add(sinButton); // 将按钮添加到面板中
add(cosButton);
add(linearButton);
add(quadraticButton);
add(resetButton); // 将重置x按钮添加到面板中
add(stopButton);
add(startButton);
setFocusable(true); // 设置焦点
}
private void updateData() { // 更新面板中的数据
lock.lock();
System.out.println("update start");
for (int i = 0; i < xValues.size(); i++) { // 遍历x轴上的值
double x = xValues.get(i) + scroll; // 获取当前x轴上的值
double y = 0.0;
switch (CURVE_TYPES[currentCurveIndex]) { // 根据当前曲线编号计算y轴上的值
case 0:
y = - Math.sin((x+scroll) / 50.0) * 100.0;
break;
case 1:
y = - Math.cos((x+scroll) / 50.0) * 100.0;
break;
case 2:
y = -(x+scroll);
break;
case 3:
y = -(x+scroll)*(x+scroll)/ 100.0;
break;
}
yValues.set(i, y); // 将计算得到的y轴上的值存储到yValues列表中
}
scroll += 2; // 每次更新后将x轴向右滚动2个像素
System.out.println("update end");
lock.unlock();
}
// // 添加方法以移动整个图像
// public void moveImage(int x, int y) {
// scroll += x; // 改变当前x值以移动整个图像
// }
public void start() { // 启动线程的方法
System.out.println("click start");
thread = new Thread(this); // 创建线程对象
running = true; // 设置运行标志为true
thread.start(); // 启动线程
}
public void stop() { // 停止线程的方法
System.out.println("click stop");
running = false; // 设置运行标志为false
try {
thread.join(); // 等待线程结束
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() { // 线程的运行方法
System.out.println("start run");
while (running) { // 如果运行标志为true,则循环更新和重绘面板
updateData(); // 更新面板中的数据
repaint();
try {
System.out.println("run delay start");
Thread.sleep(DELAY); // 线程休眠一段时间
System.out.println("run delay end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("stop run");
}
@Override
protected void paintComponent(Graphics graphics) {
lock.lock();
System.out.println("paintComponent start");
super.paintComponent(graphics); // 父类的绘制操作得以执行
graphics.setColor(Color.BLACK); // 设置线条颜色为黑色
graphics.drawLine(0, HEIGHT / 2, WIDTH, HEIGHT / 2); // 画x轴
graphics.drawString("X", WIDTH - 20, HEIGHT/2 + 10);
graphics.setColor(Color.RED);
graphics.drawString("x value", WIDTH / 2 - 40, HEIGHT/2 + 10);
graphics.drawLine(WIDTH / 2, 0, WIDTH / 2, HEIGHT); // 画y轴
graphics.setColor(Color.BLACK);
graphics.drawString("Y", WIDTH / 2 + 10 - scroll, 50);
graphics.drawString("0", WIDTH / 2 - 10 - scroll, HEIGH
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 基于JavaSwing的简单动态曲线系统+使用说明.zip基于JavaSwing的简单动态曲线系统+使用说明.zip基于JavaSwing的简单动态曲线系统+使用说明.zip基于JavaSwing的简单动态曲线系统+使用说明.zip基于JavaSwing的简单动态曲线系统+使用说明.zip基于JavaSwing的简单动态曲线系统+使用说明.zip 基于JavaSwing的简单动态曲线系统+使用说明.zip 基于JavaSwing的简单动态曲线系统+使用说明.zip 【备注】 1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载体验! 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈!
资源推荐
资源详情
资源评论
收起资源包目录
基于JavaSwing的简单动态曲线系统+使用说明.zip (7个子文件)
CurveSystem.class 7KB
CurveSystem.java 10KB
使用说明.md 1KB
readme_img
img3.png 31KB
img4.png 27KB
img2.png 29KB
img1.png 26KB
共 7 条
- 1
资源评论
北航程序员小C
- 粉丝: 2244
- 资源: 1825
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功