Advertisement
SquirrelInBox

Untitled

Nov 6th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. package mainPackage;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.util.Vector;
  6.  
  7. public class Main extends JComponent {
  8.     static class Canvas extends JPanel{
  9.  
  10.         int nx = 40;// цена деления  по шкалам
  11.         int ny = 40;
  12.         int oyn = 250; // начальный отступ по y
  13.         int oxn = 350 ; //начальный отступ по х
  14.         int ly = 500; // длина оси у
  15.         int lx = 700; // длина оси х
  16.  
  17.         public static double evaluate(double x) {
  18.             return x*Math.sin(x);
  19.         }
  20.  
  21.         public static Vector<Integer> getScreenCoords(Vector<Double> old_c, double a, double b) {
  22.             Vector<Integer> new_c = new Vector< Integer>();
  23.             int curr_c;
  24.             for (Double anOld_c : old_c) {
  25.                 curr_c = (int) (anOld_c * a + b);
  26.                 new_c.add(curr_c);
  27.             }
  28.             return new_c;
  29.         }
  30.  
  31.         public static void draw_graph(Vector<Integer> x, Vector<Integer> y, Graphics2D g) {
  32.             int size = x.size();
  33.             for (int i = 0; i < size - 1; i++) {
  34.                g.drawLine(x.get(i), y.get(i),
  35.                           x.get(i + 1), y.get(i + 1));
  36.             }
  37.         }
  38.  
  39.         public void draw_coord_lines(Graphics g) {
  40.             g.drawLine(oxn, (int) ly / 2 - oyn,
  41.                     oxn, (int) ly / 2 + oyn);
  42.             g.drawLine( oxn , -1 ,
  43.                     oxn - 3 , 10 );
  44.             g.drawLine(oxn, -1,
  45.                     oxn + 3, 10);
  46.  
  47.             g.drawLine( (int)(lx/2) - oxn, oyn,
  48.                     (int)(lx/2) + oxn, oyn);
  49.             g.drawLine( (int)(lx/2)+ oxn , oyn,
  50.                     (int)(lx/2) + oxn - 10 , oyn - 3 );
  51.             g.drawLine(lx, oyn,
  52.                     lx - 10, oyn + 3);
  53.         }
  54.  
  55.         public void paintComponent(Graphics g){
  56.             super.paintComponents(g);
  57.  
  58.             double a = -1;
  59.             double b = 1;
  60.  
  61.             Graphics2D g2d = (Graphics2D) g;
  62. //            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  63.  
  64.             Vector<Double> y1 = new Vector<Double>();
  65.             double max_y = 0;
  66.             double min_y = ly;
  67.             Vector<Integer> x_c = new Vector<Integer>();
  68.             Vector<Integer> y_c = new Vector<Integer>();
  69.             for (int i = 0; i < lx; ++i) {
  70.                 double x = a + i*(b - a)/lx;
  71.                 double y = evaluate(x);
  72.                 y1.add(y);
  73.                 x_c.add(i);
  74.                 max_y = Math.max(max_y, y);
  75.                 min_y = Math.min(min_y, y);
  76.             }
  77.  
  78.             int yy = (int)((evaluate(a)-min_y)*ly/(max_y-min_y));
  79.             y_c.add(0, yy);
  80.             double y;
  81.             for (int xx=1;xx<lx;++xx)
  82.             {
  83.                 y = y1.get(xx);
  84.                 yy = (int)((y-max_y)*ly/(min_y - max_y));
  85.                 y_c.add(yy);
  86.             }
  87.  
  88.             System.out.println(y_c.size());
  89.             System.out.println(x_c.size());
  90.  
  91.  
  92. /*            double maxC = 0;
  93.             double tempY;
  94.             while(l <= b) {
  95.                 x1.add(l);
  96.                 tempY = evaluate(l)*(-1);
  97.                 y1.add(tempY);
  98.                 maxC = Math.abs((evaluate(l)));
  99.                 l += interv;
  100.             }
  101.  
  102. //            nx = (int)(lx/(2*maxC));
  103.  
  104.             int n_ny = (int)(ly / ((int)(maxC)));
  105.             Vector<Integer> n_x = getScreenCoords(x1, nx, oxn);
  106.             Vector<Integer> n_y = getScreenCoords(y1, n_ny, oyn);*/
  107.  
  108. //            draw_graph(n_x, n_y, g);
  109.             draw_graph(x_c, y_c, g2d);
  110.  
  111. //            draw_coord_lines(g);
  112.  
  113. //            for (int i = 0; i < lx; i += nx){
  114. //                g.drawLine(i, oyn - 1, i, oyn + 1);
  115. //            }
  116. //
  117. //            for (int i = 0; i < ly; i += ny) {
  118. //                g.drawLine(oxn - 1, i, oxn + 1, i);
  119. //            }
  120.  
  121.             super.repaint();
  122.         }
  123.     }
  124.  
  125.     public static void main(String[] args) {
  126.         double a = 0;
  127.         double b = 5;
  128.         double intev = (b - a)/1000000;
  129.         double ind = 0;
  130.  
  131.         JFrame w=new JFrame("Function");
  132. //        w.setLayout(new BorderLayout());
  133.  
  134. //        w.setSize(715, 515);
  135.  
  136.         w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  137.  
  138.         Canvas canv=new Canvas();
  139.         w.getContentPane().add(canv);
  140. //        w.add(canv);
  141.         w.setLocationRelativeTo(null);
  142.         w.setVisible(true);
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement