All pastes #896093 Raw Edit

Anonymous

public java v1 · immutable
·published 2008-02-08 10:22 UTC
rendered paste body
package projekt;import java.awt.Dimension;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JTextField;public class Projekt extends JFrame {    private JMenuBar menuBar = new JMenuBar();    private JMenu menuFile = new JMenu();    private JMenuItem menuFileExit = new JMenuItem();    private JMenu menuHelp = new JMenu();    private JButton jButton1 = new JButton();    private JLabel jLabel1 = new JLabel();    private JButton jButton2 = new JButton();    private JTextField jTextField1 = new JTextField();    private JLabel jLabel2 = new JLabel();    private JMenuItem jMenuItem1 = new JMenuItem();    private int[][] matrix;    private JButton jButton3 = new JButton();        public Projekt() {        try {            jbInit();        } catch (Exception e) {            e.printStackTrace();        }    }    private void jbInit() throws Exception {        this.setJMenuBar( menuBar );        this.getContentPane().setLayout( null );        this.setSize( new Dimension(400, 300) );        this.setTitle( "CSV Reader" );        menuFile.setText( "File" );        menuFileExit.setText( "Exit" );        menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );        menuHelp.setText( "Help" );        jButton1.setText("Pobierz");        jButton1.setBounds(new Rectangle(305, 10, 80, 25));        jButton1.addActionListener(new ActionListener() {                    public void actionPerformed(ActionEvent e) {                        jButton1_actionPerformed(e);                    }                });        jLabel1.setText("Wyniki");        jLabel1.setBounds(new Rectangle(15, 135, 365, 65));        jButton2.setText("Średnia Arytmetyczna");        jButton2.setBounds(new Rectangle(10, 65, 175, 30));        jButton2.addActionListener(new ActionListener() {                    public void actionPerformed(ActionEvent e) {                        jButton2_actionPerformed(e);                    }                });                jTextField1.setBounds(new Rectangle(55, 10, 250, 25));        jLabel2.setText("URL:");        jLabel2.setBounds(new Rectangle(15, 10, 75, 25));        jMenuItem1.setText("About");        jButton3.setText("Dominanta");        jButton3.setBounds(new Rectangle(210, 65, 175, 30));        jButton3.addActionListener(new ActionListener() {                    public void actionPerformed(ActionEvent e) {                        jButton3_actionPerformed(e);                    }                });        menuFile.add( menuFileExit );        menuBar.add( menuFile );        menuHelp.add(jMenuItem1);        menuBar.add(menuHelp);        this.getContentPane().add(jButton4, null);        this.getContentPane().add(jButton3, null);        this.getContentPane().add(jLabel2, null);        this.getContentPane().add(jTextField1, null);        this.getContentPane().add(jButton2, null);        this.getContentPane().add(jLabel1, null);        this.getContentPane().add(jButton1, null);            }            public void ToMatrix() throws Exception{            URL address = new URL(jTextField1.getText()); // stwórz adres            URLConnection aconnection = address.openConnection(); // otwórz połaczenie                       BufferedReader in = new BufferedReader(new InputStreamReader(aconnection.getInputStream())); // przygotuj do czytania                                     ArrayList<String> rows = new ArrayList<String>(); // tu będą wiersze            String inputString; // wczytywany wiersz            while ((inputString = in.readLine()) != null) rows.add(inputString); //wczytaj wiersze do rows                       int[][] matrix = new int[rows.size()][]; // stwórz tablicę o ilości wierszy równą ilości wczytanych wierszy                                     for (int i = 0; i < rows.size(); i++) { //dla każdego wczytanego wiersza                                             String row = rows.get(i); // wiersz wczytany z URL-a            int[] numbers = new int[row.split(",").length]; //tworzymy nową tablicę liczb, wielkości liczb w wierszu            String[] sNumbers = row.split(","); // tworzymy tablicę Stringów-liczb                                             for (int j = 0; j < sNumbers.length; j++) {                numbers[j] = Integer.parseInt(sNumbers[j]); // zamieniaj Stringi w inty            }            matrix[i] = numbers; // odpowiedniemu wierszowi w macierzy przyporządkuj daną tablicę	    this.matrix = matrix;        }    }              void fileExit_ActionPerformed(ActionEvent e) {        System.exit(0);    }    void widowClosing(WindowEvent e) {        System.exit(0);    }    void helpAbout_ActionPerformed(ActionEvent e) {        //JOptionPane.showMessageDialog(this, new Projekt_AboutBoxPanel1(), "About", JOptionPane.PLAIN_MESSAGE);    }    private void jButton1_actionPerformed(ActionEvent e) {        if(e.getSource() == jButton1) {            try{                ToMatrix();            }            catch(Exception ex) {                JOptionPane.showMessageDialog(null,"Niepoprawne dane, podaj adres URL!","Error",JOptionPane.ERROR_MESSAGE);            }         }    }        private void jButton2_actionPerformed(ActionEvent e) {        if(e.getSource() == jButton2) {            try {                //liczenie średniej arytmetycznej, powinno działać;)                int suma = 0;                for (int i = 0; i < this.matrix[0].length; i++) suma += this.matrix[0][i];                double srednia = suma / this.matrix[0].length;                jLabel1.setText(Double.toString(srednia)); //wypisanie do labela            }            catch(Exception ex) {                jLabel1.setText(""+ex.getMessage());            }        }                }    private void jButton3_actionPerformed(ActionEvent e) {        try {            double m, modeVal = 0.0;            int count, oldcount = 0;            for (int j=0; j<this.matrix[0].length; j++) {                m= this.matrix[0][j];                count = 0;                //zliczenie występowania poszczególnych wartości                for (int k=j+1; k<this.matrix[0].length; k++)                    if (m == this.matrix[0][k]) count++;                //jeśli wartość występuje częściej niż poprzednie zapamiętaj ją                if (count > oldcount) {                    modeVal = m;                    oldcount = count;                }            }            if (oldcount == 0)                 jLabel1.setText("Dominanta nie występuje");            else                jLabel1.setText(Double.toString(modeVal));        }        catch (Exception de){            jLabel1.setText(""+de.getMessage());          }    }    public static void main(String args[])    {        JFrame frame = new Projekt();        frame.setVisible(true);    }}//http://mimuw.wikispaces.com/space/graph/edits?year=2008&month=2&csv