利用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);
}
}

最新文章

  1. 《嫌疑人X的献身》书评
  2. BZOJ2190: [SDOI2008]仪仗队
  3. 斐波那契数列(Fibonacci)递归和非递归实现
  4. [SAP ABAP开发技术总结]以二进制、字符模式下载文件
  5. Xdebug的安装与使用
  6. ms_sql:drop and create a job
  7. android中对线程池的理解与使用
  8. powerdesigner for sqlserver的一些实用配置
  9. c++实用技巧
  10. for循环,数组
  11. CSS中的ul与li样式详解 list-type
  12. asp.net core系列 54 IS4用客户端凭据保护API
  13. c/c++ 多线程 层级锁
  14. 神经网络常用的Numpy功能笔记
  15. Python 爬虫一 简介
  16. 01——Introduction to Android介绍
  17. python获取命令行参数的方法(汇总)
  18. 新的开始——LED灯汇编机器码的点亮方式
  19. python pandas demo
  20. javascript--- document.write()和 innerHTML的区别

热门文章

  1. OkHttp 优雅封装 HttpUtils 之 上传下载解密
  2. 判断一个字符串是否是合法IP地址
  3. 谈谈MySQL的索引
  4. python 基础篇 类基础与继承
  5. Java 反射调用方法 - 不跳过安全检查、跳过安全检查和普通方法性能比较测试
  6. 使用JAVA API编程实现简易Habse操作
  7. Caused by: java.lang.NumberFormatException: For input string: &quot; 60&quot;
  8. SNMP History and OID/MIB Tour
  9. How to change the header background color of a QTableView
  10. [Qt] 数字转换为 QString