[Java画图]画函数图像
2024-10-12 23:59:01
利用Graphics类画任意显式函数图像,只需修改代码中的F()函数即可,另外调整timesx和timesy参数来分方向放大或缩小图像。需要重定义坐标系。
package test; import javax.swing.*;
import java.awt.Graphics; public class DrawFunction extends JFrame {
static double timesx = 10, timesy = 10;
double F(double x) {
return Math.sin(x) / Math.pow(1.1, -x);//函数表达式
}
int x0, y0;
static int W = 800, H = 600;
static double L = -W / 2, R = W / 2;
Graphics G;
public void setOrigin(int x, int y) {
this.x0 = x;
this.y0 = y;
// show coordinate axis
drawLine(-W / 2, 0, W / 2, 0);
drawLine(0, -H / 2, 0, H / 2);
drawString("X", W / 2 - 30, -20);
drawString("Y", -20, H / 2 - 20);
for (int i = 1; i <= 10; i ++) {
draw(W / 2 - i - 6, i);
draw(W / 2 - i - 6, -i);
}
for (int i = 1; i <= 10; i ++) {
draw(-i, H / 2 - i);
draw(i, H / 2 - i);
}
}
public DrawFunction() {
add(new NewPanel());
}
public static void main(String[] args) {
DrawFunction frame = new DrawFunction();
frame.setTitle("DrawFunction");
frame.setSize(W, H);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
public class Coordinate2D {
int x, y;
public Coordinate2D(int x, int y) {
this.x = x;
this.y = y;
}
public int getPixelPointX() {
return x0 + x;
}
public int getPixelPointY() {
return y0 - y;
}
}
class NewPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
G = g;
setOrigin(W / 2, H / 2);
// in the following , draw what you want draw!
for (int i = -W / 2; i <= W / 2; i ++) {
draw(i, work(i));
}
/*
for (int i = 0; i < 1000; i ++) {
int x = (int)(Math.random() * 400 - 200);
int y = (int)(Math.random() * 400 - 200);
drawString("哈哈", x, y);
}
*/
}
}
int work(int x) {
//timesx = 0.01;
//timesy = 100;
return (int)(F(x / timesx) * timesy);
}
public void draw(int x, int y) {
int X = new Coordinate2D(x, y).getPixelPointX();
int Y = new Coordinate2D(x, y).getPixelPointY();
G.drawLine(X, Y, X, Y);
}
public void drawRec(int x1, int y1, int x2, int y2) {
int dx = x1 < x2? 1 : -1;
int dy = y1 < y2? 1 : -1;
for (int i = x1; i != x2 + dx; i += dx) {
for (int j = y1; j != y2 + dy; j += dy) {
draw(i, j);
}
}
}
public void drawLine(int x1, int y1, int x2, int y2) {
int dx = x1 < x2? 1 : -1;
if (x1 == x2) drawRec(x1, y1, x2, y2);
else {
double d = (double)(y2 - y1) / (x2 - x1);
for (int i = x1; i != x2 + dx; i += dx) {
draw(i, (int)(y1 + (i - x1) * d));
}
}
}
public void drawString(String s, int x, int y) {
int X = new Coordinate2D(x, y).getPixelPointX();
int Y = new Coordinate2D(x, y).getPixelPointY();
G.drawString(s, X, Y);
}
}
最新文章
- 《嫌疑人X的献身》书评
- BZOJ2190: [SDOI2008]仪仗队
- 斐波那契数列(Fibonacci)递归和非递归实现
- [SAP ABAP开发技术总结]以二进制、字符模式下载文件
- Xdebug的安装与使用
- ms_sql:drop and create a job
- android中对线程池的理解与使用
- powerdesigner for sqlserver的一些实用配置
- c++实用技巧
- for循环,数组
- CSS中的ul与li样式详解 list-type
- asp.net core系列 54 IS4用客户端凭据保护API
- c/c++ 多线程 层级锁
- 神经网络常用的Numpy功能笔记
- Python 爬虫一 简介
- 01——Introduction to Android介绍
- python获取命令行参数的方法(汇总)
- 新的开始——LED灯汇编机器码的点亮方式
- python pandas demo
- javascript--- document.write()和 innerHTML的区别
热门文章
- OkHttp 优雅封装 HttpUtils 之 上传下载解密
- 判断一个字符串是否是合法IP地址
- 谈谈MySQL的索引
- python 基础篇 类基础与继承
- Java 反射调用方法 - 不跳过安全检查、跳过安全检查和普通方法性能比较测试
- 使用JAVA API编程实现简易Habse操作
- Caused by: java.lang.NumberFormatException: For input string: "; 60";
- SNMP History and OID/MIB Tour
- How to change the header background color of a QTableView
- [Qt] 数字转换为 QString