[Java画图]画函数图像

利用Graphics类画任意显式函数图像,只需修改代码中的F()函数即可,另外调整timesx和timesy参数来分方向放大或缩小图像。需要重定义坐标系。

  1 package test;
  2
  3 import javax.swing.*;
  4 import java.awt.Graphics;
  5
  6 public class DrawFunction extends JFrame {
  7     static double timesx = 10, timesy = 10;
  8     double F(double x) {
  9         return Math.sin(x) / Math.pow(1.1, -x);//函数表达式
 10     }
 11     int x0, y0;
 12     static int W = 800, H = 600;
 13     static double L = -W / 2, R = W / 2;
 14     Graphics G;
 15     public void setOrigin(int x, int y) {
 16         this.x0 = x;
 17         this.y0 = y;
 18         // show coordinate axis
 19         drawLine(-W / 2, 0, W / 2, 0);
 20         drawLine(0, -H / 2, 0, H / 2);
 21         drawString("X", W / 2 - 30, -20);
 22         drawString("Y", -20, H / 2 - 20);
 23         for (int i = 1; i <= 10; i ++) {
 24             draw(W / 2 - i - 6, i);
 25             draw(W / 2 - i - 6, -i);
 26         }
 27         for (int i = 1; i <= 10; i ++) {
 28             draw(-i, H / 2 - i);
 29             draw(i, H / 2 - i);
 30         }
 31     }
 32     public DrawFunction() {
 33         add(new NewPanel());
 34     }
 35     public static void main(String[] args) {
 36         DrawFunction frame = new DrawFunction();
 37         frame.setTitle("DrawFunction");
 38         frame.setSize(W, H);
 39         frame.setLocationRelativeTo(null);
 40         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 41         frame.setVisible(true);
 42         frame.setResizable(false);
 43     }
 44     public class Coordinate2D {
 45         int x, y;
 46         public Coordinate2D(int x, int y) {
 47             this.x = x;
 48             this.y = y;
 49         }
 50         public int getPixelPointX() {
 51             return x0 + x;
 52         }
 53         public int getPixelPointY() {
 54             return y0 - y;
 55         }
 56     }
 57     class NewPanel extends JPanel {
 58         protected void paintComponent(Graphics g) {
 59             super.paintComponent(g);
 60             G = g;
 61             setOrigin(W / 2, H / 2);
 62             // in the following , draw what you want draw!
 63             for (int i = -W / 2; i <= W / 2; i ++) {
 64                 draw(i, work(i));
 65             }
 66             /*
 67             for (int i = 0; i < 1000; i ++) {
 68                 int x = (int)(Math.random() * 400 - 200);
 69                 int y = (int)(Math.random() * 400 - 200);
 70                 drawString("哈哈", x, y);
 71             }
 72             */
 73         }
 74     }
 75     int work(int x) {
 76         //timesx = 0.01;
 77         //timesy = 100;
 78         return (int)(F(x / timesx) * timesy);
 79     }
 80     public void draw(int x, int y) {
 81         int X = new Coordinate2D(x, y).getPixelPointX();
 82         int Y = new Coordinate2D(x, y).getPixelPointY();
 83         G.drawLine(X, Y, X, Y);
 84     }
 85     public void drawRec(int x1, int y1, int x2, int y2) {
 86         int dx = x1 < x2? 1 : -1;
 87         int dy = y1 < y2? 1 : -1;
 88         for (int i = x1; i != x2 + dx; i += dx) {
 89             for (int j = y1; j != y2 + dy; j += dy) {
 90                 draw(i, j);
 91             }
 92         }
 93     }
 94     public void drawLine(int x1, int y1, int x2, int y2) {
 95         int dx = x1 < x2? 1 : -1;
 96         if (x1 == x2) drawRec(x1, y1, x2, y2);
 97         else {
 98             double d = (double)(y2 - y1) / (x2 - x1);
 99             for (int i = x1; i != x2 + dx; i += dx) {
100                 draw(i, (int)(y1 + (i - x1) * d));
101             }
102         }
103     }
104     public void drawString(String s, int x, int y) {
105         int X = new Coordinate2D(x, y).getPixelPointX();
106         int Y = new Coordinate2D(x, y).getPixelPointY();
107         G.drawString(s, X, Y);
108     }
109 }
时间: 2024-08-09 21:58:48

[Java画图]画函数图像的相关文章

R语言自学小计,从零到画函数图像

自从入了菊花厂,空余时间就得越来越少了.加之毕业前的几个月放纵,留下了很多未出毕业的游戏,荒废了一些时间,人也颓废了许多.工作压力变得越来越大,对工作环境越来越不满,让我变得想逃离这里.既然想逃离菊花厂,自然要准备点技能,收拾收拾心情准备开始做离职了. 首先向探一探数据挖掘的深浅,了解下R语言.从入门的这些知识学习看来,R和matlab确实很相似. 基础知识准备: https://www.w3cschool.cn/r/ 花了一点时间,找到了W3C上的一点资料,看完基本语法就可以动手开始玩一玩了

用python画函数图像

import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 1, 50) # 从0到1,等分50分 y = 210*(x**6)*((1-x)**4) # 这里是函数的表达式 plt.figure() # 定义一个图像窗口 plt.plot(x, y) # 绘制曲线 y plt.show() 原文地址:https://www.cnblogs.com/hare/p/11964562.html

java画图程序_图片用字母画出来_源码发布_版本二

在上一个版本:java画图程序_图片用字母画出来_源码发布 基础上,增加了图片同比例缩放,使得大像素图片可以很好地显示画在Notepad++中. 项目结构: 运行效果1: 原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_result1.png 运行效果2: 原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_res

java画图程序_图片用字母画出来_源码发布

在之前写了一篇blog:java画图程序_图片用字母画出来 主要是把一些调试的截图发布出来,现在程序调试我认为可以了(当然,你如果还想调试的话,也可以下载源码自己调试). 就把源码发布出来. 项目结构: 资源文件: 原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_resource.png 运行效果: 原图:http://images.cnblogs.com/cnblogs_com/hongten/356

java画图程序_图片用字母画出来

最近在研究怎样将图片用字母在文本编辑工具中“画”出来. 你看了这个可能还不知道我想说什么? 我想直接上图,大家一定就知道了 第一张:小猫 原图:http://www.cnblogs.com/hongten/gallery/image/143365.html 第二张:林允儿 原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_star.png 第三张:郭静 原图:http://www.cnblogs.com/hongten/galle

几何画板中作函数图像的几种方法

随着社会的发展,现代教学很多的地方都有了多媒体教学,这就需要一些教学软件的辅助了,几何画板就是其中之一.一些老师在使用几何画板的过程中,常常涉及到函数图象的绘制.因此,很多用户对这方面教程是非常的感兴趣的.下面就给大家分享一下几何画板中作函数图像的几种方法? 一.直接法 例1  画函数y=sinx在R上的图象. 操作步骤:单击“图表”菜单下“绘制新函数”f(x)=sinx. 二.轨迹法 例2  画函数y=(1/4)x^2在区间[-2,3]上的图象. 操作步骤: (1)单击“绘图”菜单下“绘制点”

极坐标系 隐函数 数值求解 并 绘制 函数图像

我写了一个 极坐标系 隐函数 数值求解 并 绘制 函数图像 的 程序   DrawPolarFunc  . 项目地址 :            https://github.com/kelin-xycs/DrawPolarFunc            . 进入 项目页面 后 点击 右边绿色 的 “Clone or download” 按钮 就可以下载 项目文件 了 .  项目中 只有一个 程序文件   DrawPolarFunc.html  , 用 Html5 + javascript  写的

用这款数学课件制作工具画函数切线,很省事!

作为新世纪的数学老师,要适应多媒体教学,那就需要掌握许多教学辅助工具,比如几何画板数学课件制作工具,可以用来画几何图形和函数图像,它有别于其它的几何绘图工具.在研究函数图像时少不了函数的切线,那么怎样用几何画板画函数切线呢? 几何画板双十一钜惠活动正在进行,可以访问http://www.jihehuaban.com.cn/index.php?page=goumai&ref=11查看活动详情. 该软件下载地址如下: 几何画板-Windows:http://wm.makeding.com/iclk/

python如何画三维图像?

python三维图像输出的代码如下所示:#画3D函数图像输出from mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmimport matplotlib.pyplot as pltimport numpy as npimport mpl_toolkits.mplot3dfigure=plt.figure()#ax = Axes3D(figure)ax=figure.gca(projection="3d")x1=np.