Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mainPackage;
- import javax.swing.*;
- import java.awt.*;
- import java.util.Vector;
- public class Main extends JComponent {
- static class Canvas extends JPanel{
- int nx = 40;// цена деления по шкалам
- int ny = 40;
- int oyn = 250; // начальный отступ по y
- int oxn = 350 ; //начальный отступ по х
- int ly = 500; // длина оси у
- int lx = 700; // длина оси х
- public static double evaluate(double x) {
- return x*Math.sin(x);
- }
- public static Vector<Integer> getScreenCoords(Vector<Double> old_c, double a, double b) {
- Vector<Integer> new_c = new Vector< Integer>();
- int curr_c;
- for (Double anOld_c : old_c) {
- curr_c = (int) (anOld_c * a + b);
- new_c.add(curr_c);
- }
- return new_c;
- }
- public static void draw_graph(Vector<Integer> x, Vector<Integer> y, Graphics2D g) {
- int size = x.size();
- for (int i = 0; i < size - 1; i++) {
- g.drawLine(x.get(i), y.get(i),
- x.get(i + 1), y.get(i + 1));
- }
- }
- public void draw_coord_lines(Graphics g) {
- g.drawLine(oxn, (int) ly / 2 - oyn,
- oxn, (int) ly / 2 + oyn);
- g.drawLine( oxn , -1 ,
- oxn - 3 , 10 );
- g.drawLine(oxn, -1,
- oxn + 3, 10);
- g.drawLine( (int)(lx/2) - oxn, oyn,
- (int)(lx/2) + oxn, oyn);
- g.drawLine( (int)(lx/2)+ oxn , oyn,
- (int)(lx/2) + oxn - 10 , oyn - 3 );
- g.drawLine(lx, oyn,
- lx - 10, oyn + 3);
- }
- public void paintComponent(Graphics g){
- super.paintComponents(g);
- double a = -1;
- double b = 1;
- Graphics2D g2d = (Graphics2D) g;
- // g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- Vector<Double> y1 = new Vector<Double>();
- double max_y = 0;
- double min_y = ly;
- Vector<Integer> x_c = new Vector<Integer>();
- Vector<Integer> y_c = new Vector<Integer>();
- for (int i = 0; i < lx; ++i) {
- double x = a + i*(b - a)/lx;
- double y = evaluate(x);
- y1.add(y);
- x_c.add(i);
- max_y = Math.max(max_y, y);
- min_y = Math.min(min_y, y);
- }
- int yy = (int)((evaluate(a)-min_y)*ly/(max_y-min_y));
- y_c.add(0, yy);
- double y;
- for (int xx=1;xx<lx;++xx)
- {
- y = y1.get(xx);
- yy = (int)((y-max_y)*ly/(min_y - max_y));
- y_c.add(yy);
- }
- System.out.println(y_c.size());
- System.out.println(x_c.size());
- /* double maxC = 0;
- double tempY;
- while(l <= b) {
- x1.add(l);
- tempY = evaluate(l)*(-1);
- y1.add(tempY);
- maxC = Math.abs((evaluate(l)));
- l += interv;
- }
- // nx = (int)(lx/(2*maxC));
- int n_ny = (int)(ly / ((int)(maxC)));
- Vector<Integer> n_x = getScreenCoords(x1, nx, oxn);
- Vector<Integer> n_y = getScreenCoords(y1, n_ny, oyn);*/
- // draw_graph(n_x, n_y, g);
- draw_graph(x_c, y_c, g2d);
- // draw_coord_lines(g);
- // for (int i = 0; i < lx; i += nx){
- // g.drawLine(i, oyn - 1, i, oyn + 1);
- // }
- //
- // for (int i = 0; i < ly; i += ny) {
- // g.drawLine(oxn - 1, i, oxn + 1, i);
- // }
- super.repaint();
- }
- }
- public static void main(String[] args) {
- double a = 0;
- double b = 5;
- double intev = (b - a)/1000000;
- double ind = 0;
- JFrame w=new JFrame("Function");
- // w.setLayout(new BorderLayout());
- // w.setSize(715, 515);
- w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Canvas canv=new Canvas();
- w.getContentPane().add(canv);
- // w.add(canv);
- w.setLocationRelativeTo(null);
- w.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement