Advertisement
Guest User

z wykresami parametrów

a guest
May 25th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.63 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import org.jfree.chart.ChartFactory;
  6. import org.jfree.chart.ChartFrame;
  7. import org.jfree.chart.JFreeChart;
  8. import org.jfree.chart.plot.PlotOrientation;
  9. import org.jfree.data.xy.XYSeries;
  10. import org.jfree.data.xy.XYSeriesCollection;
  11.  
  12.  
  13. public class mmm1_0 extends JFrame implements ActionListener {
  14.  
  15.     JButton JWykresX = new JButton("Wykres położenia x");
  16.     JButton JWykresY = new JButton("Wykres położenia y");
  17.     JButton JWykresVx = new JButton("Wykres prędkości Vx");
  18.     JButton JWykresVy = new JButton("Wykres prędkości Vy");
  19.    
  20.     JTextField JM1 = new JTextField();
  21.     JTextField JM2 = new JTextField();
  22.     JTextField JK1 = new JTextField();
  23.     JTextField JK2 = new JTextField();
  24.     JTextField JB = new JTextField();
  25.    
  26.     JRadioButton JProstokat = new JRadioButton();
  27.     JRadioButton JTrojkat = new JRadioButton();
  28.     JRadioButton JSinus = new JRadioButton();
  29.    
  30.     ButtonGroup grupa_radiowa = new ButtonGroup();
  31.    
  32.     JLabel Probny1 = new JLabel();
  33.     JLabel Probny2 = new JLabel();
  34.    
  35.     double u[];
  36.     double x[];
  37.     double y[];
  38.     double vx[];
  39.     double vy[];
  40.    
  41.     int wybranePobudzenie;
  42.    
  43.     public mmm1_0() {
  44.        
  45.         super("Model amortyzatora");
  46.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.         setVisible(true);
  48.         setSize(1000,600);
  49.         setLayout(null);
  50.        
  51.         wybranePobudzenie=0;
  52.                
  53.         JWykresX.addActionListener(this);
  54.         JWykresY.addActionListener(this);
  55.         JWykresVx.addActionListener(this);
  56.         JWykresVy.addActionListener(this);
  57.        
  58.        
  59.         add(JWykresX);
  60.         JWykresX.setBounds(20, 450, 160, 30);
  61.         JWykresX.setBackground(new Color(238, 238, 0));
  62.        
  63.         add(JWykresY);
  64.         JWykresY.setBounds(200, 450, 160, 30);
  65.         JWykresY.setBackground(new Color(238, 238, 0));
  66.        
  67.         add(JWykresVx);
  68.         JWykresVx.setBounds(380, 450, 160, 30);
  69.         JWykresVx.setBackground(new Color(238, 238, 0));
  70.        
  71.         add(JWykresVy);
  72.         JWykresVy.setBounds(560, 450, 160, 30);
  73.         JWykresVy.setBackground(new Color(238, 238, 0));
  74.        
  75.        
  76.         JLabel JNapis1 = new JLabel("Wpisz parametry:");
  77.         JNapis1.setBounds(100, 50, 150, 30);
  78.         add(JNapis1);
  79.        
  80.        
  81.         JLabel JNapisM1 = new JLabel("m1 = ");
  82.         JNapisM1.setBounds(100, 100, 33, 30);
  83.         add(JNapisM1);
  84.        
  85.         add(JM1);
  86.         JM1.setBounds(140, 100, 80, 30);
  87.        
  88.        
  89.         JLabel JNapisM2 = new JLabel("m2 = ");
  90.         JNapisM2.setBounds(100, 150, 33, 30);
  91.         add(JNapisM2);
  92.        
  93.         add(JM2);
  94.         JM2.setBounds(140, 150, 80, 30);  
  95.                
  96.        
  97.         JLabel JNapisK1 = new JLabel("k1 = ");
  98.         JNapisK1.setBounds(100, 200, 33, 30);
  99.         add(JNapisK1);
  100.        
  101.         add(JK1);
  102.         JK1.setBounds(140, 200, 80, 30);
  103.                
  104.        
  105.         JLabel JNapisK2 = new JLabel("k2 = ");
  106.         JNapisK2.setBounds(100, 250, 33, 30);
  107.         add(JNapisK2);
  108.        
  109.         add(JK2);
  110.         JK2.setBounds(140, 250, 80, 30);
  111.                
  112.        
  113.         JLabel JNapisB = new JLabel("b = ");
  114.         JNapisB.setBounds(100, 300, 30, 30);
  115.         add(JNapisB);
  116.        
  117.         add(JB);
  118.         JB.setBounds(140, 300, 80, 30);
  119.        
  120.        
  121.         JLabel JNapis2 = new JLabel("Pobudzenie sygnałem u:");
  122.         JNapis2.setBounds(300, 50, 200, 30);
  123.         add(JNapis2);
  124.        
  125.        
  126.         add(JProstokat);
  127.         JProstokat.setText("prostokątnym");
  128.         JProstokat.setBounds(300, 100, 150, 30);
  129.         grupa_radiowa.add(JProstokat);
  130.         JProstokat.addActionListener(this);
  131.        
  132.        
  133.         add(JTrojkat);
  134.         JTrojkat.setText("trójkątnym");
  135.         JTrojkat.setBounds(300, 150, 150, 30);
  136.         grupa_radiowa.add(JTrojkat);
  137.         JTrojkat.addActionListener(this);
  138.        
  139.         add(JSinus);
  140.         JSinus.setText("harmonicznym");
  141.         JSinus.setBounds(300, 200, 150, 30);
  142.         grupa_radiowa.add(JSinus);
  143.         JSinus.addActionListener(this);
  144.        
  145.        
  146.        
  147.         Probny1.setText("nic");
  148.         Probny1.setBounds(500, 100, 100, 30);
  149.         add(Probny1);
  150.        
  151.         Probny2.setText("nic");
  152.         Probny2.setBounds(500, 150, 100, 30);
  153.         add(Probny2);
  154.        
  155.        
  156.       //  JLabel obrazek1 = new JLabel();
  157.       //  add(obrazek1);
  158.       //  obrazek1.setBounds(450, 50, 148, 272);
  159.      //   obrazek1.setIcon(new ImageIcon("obrazy/schemat.jpg"));
  160.        
  161.     //    JLabel JObrazek2 = new JLabel(new ImageIcon("schemat.jpg"));
  162.     //    JObrazek2.setBounds(450, 50, 148, 272);
  163.    //     JObrazek2.setVisible(true);
  164.      
  165.        
  166.     //  
  167.        
  168.        
  169.        
  170.        
  171.        
  172.        
  173.        
  174.        
  175.        
  176.        
  177.        
  178.     }
  179.    
  180.     public void actionPerformed(ActionEvent evt){
  181.        
  182.         boolean blad = false;
  183.         double m1=1.0, m2=1.0, k1=1., k2=1., b=1.;
  184.         Object klikniete = evt.getSource();
  185.        
  186.        
  187.         try {
  188.                 m1 = Double.parseDouble(JM1.getText());        
  189.             }
  190.         catch(NumberFormatException e) {
  191.                 JM1.setText("błędne dane");
  192.                 blad=true;
  193.         }
  194.        
  195.        
  196.         try {
  197.                 m2 = Double.parseDouble(JM2.getText());        
  198.             }
  199.         catch(NumberFormatException e) {
  200.                 JM2.setText("błędne dane");
  201.                 blad=true;
  202.         }
  203.        
  204.        
  205.         try {
  206.                 k1 = Double.parseDouble(JK1.getText());        
  207.             }
  208.         catch(NumberFormatException e) {
  209.                 JK1.setText("błędne dane");
  210.                 blad=true;
  211.         }
  212.        
  213.         try {
  214.                 k2 = Double.parseDouble(JK2.getText());        
  215.             }
  216.         catch(NumberFormatException e) {
  217.                 JK2.setText("błędne dane");
  218.                 blad=true;
  219.         }
  220.        
  221.         try {
  222.                 b = Double.parseDouble(JB.getText());        
  223.             }
  224.         catch(NumberFormatException e) {
  225.                 JB.setText("błędne dane");
  226.                 blad=true;
  227.         }
  228.        
  229.         if (klikniete==JProstokat){
  230.             wybranePobudzenie=1;
  231.             Probny2.setText("Prostokat");
  232.         }
  233.        
  234.         if (klikniete==JTrojkat){
  235.             wybranePobudzenie=2;
  236.             Probny2.setText("Trojkat");
  237.         }
  238.        
  239.         if (klikniete==JSinus){
  240.             wybranePobudzenie=3;
  241.             Probny2.setText("Sinus");
  242.         }
  243.        
  244.        
  245.         if (wybranePobudzenie==0)
  246.             blad=true;
  247.        
  248.        
  249.         if (!blad){
  250.             if (klikniete == JWykresX) {
  251.                 Probny1.setText("X");
  252.                 wykres("Wykres X", wybranePobudzenie, m1, m2, k1, k2, b);
  253.             }
  254.        
  255.             if (klikniete == JWykresY) {
  256.                
  257.                Probny1.setText("Y");
  258.                wykres("Wykres Y", wybranePobudzenie, m1, m2, k1, k2, b);
  259.             }
  260.        
  261.             if (klikniete == JWykresVx) {
  262.                
  263.                 Probny1.setText("Vx");
  264.                 wykres("Wykres Vx", wybranePobudzenie, m1, m2, k1, k2, b);
  265.             }
  266.    
  267.             if (klikniete == JWykresVy) {
  268.                
  269.                 Probny1.setText("Vy");
  270.                 wykres("Wykres Vy", wybranePobudzenie, m1, m2, k1, k2, b);
  271.             }
  272.         }            
  273.        
  274.        
  275.     }
  276.        
  277.    
  278.    
  279.     public static void wykres(String tytul, int ktory, double m1, double m2, double k1, double k2, double b){
  280.         //Dane do wykresu 3d
  281.         XYSeries series = new XYSeries("XYGraph");
  282.         series.add(0, ktory);
  283.         series.add(1, m1);
  284.         series.add(2, m2);
  285.                 series.add(3, k1);
  286.                 series.add(4, k2);
  287.                 series.add(5, b);
  288.         XYSeriesCollection dataset = new XYSeriesCollection();
  289.         dataset.addSeries(series);
  290.         //Tworzymy wykres XY
  291.         JFreeChart chart = ChartFactory.createXYLineChart(
  292.             tytul,//Tytuł
  293.             "x- Lable", // x-axis Opis
  294.             "y- Lable", // y-axis Opis
  295.             dataset, // Dane
  296.             PlotOrientation.VERTICAL, // Orjentacja wykresu /HORIZONTAL
  297.             true, // pozkaż legende
  298.             true, // podpowiedzi tooltips
  299.             false
  300.         );
  301.  
  302.         //Dodanie wykresu do okna
  303.         ChartFrame frame1=new ChartFrame("XYArea Chart",chart);
  304.         frame1.setVisible(true);
  305.         frame1.setSize(500,400);
  306.     }
  307.    
  308.    
  309.    
  310.    
  311.     public static void main(String[] args) {
  312.        
  313.         new mmm1_0();
  314.        
  315.     }
  316.    
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement