zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    CentOS 7中搭建NFS文件共享存储服务的完整步骤
    centos 7中磁盘挂载重启后挂载失效
    smbclient 未找到命令
    Windows共享文件夹无法访问,提示“不允许一个用户使用一个以上用户名与服务器或共享资源的多重连接”
    CentOS 7下Samba服务器的安装与配置
    Systemd 指令
    centos7安装samba快速入门
    springboot2.0集成RestTemplate
    unknown directive “stream” in /usr/local/nginx
    Nginx——stream模块
  • 原文地址:https://www.cnblogs.com/jklongint/p/4843126.html
Copyright © 2011-2022 走看看