Search in sources :

Example 6 with GridLayout

use of java.awt.GridLayout in project java-design-patterns by iluwatar.

the class Client method setup.

private void setup() {
    setLayout(new BorderLayout());
    JPanel panel = new JPanel();
    add(jl, BorderLayout.SOUTH);
    add(panel, BorderLayout.CENTER);
    panel.setLayout(new GridLayout(6, 2));
    panel.add(new JLabel("Name"));
    panel.add(jtFields[0]);
    panel.add(new JLabel("Contact Number"));
    panel.add(jtFields[1]);
    panel.add(new JLabel("Address"));
    panel.add(jtAreas[0]);
    panel.add(new JLabel("Deposit Number"));
    panel.add(jtFields[2]);
    panel.add(new JLabel("Order"));
    panel.add(jtAreas[1]);
    panel.add(clearButton);
    panel.add(processButton);
    clearButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            for (JTextArea i : jtAreas) {
                i.setText("");
            }
            for (JTextField i : jtFields) {
                i.setText("");
            }
        }
    });
    processButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Order order = new Order(jtFields[0].getText(), jtFields[1].getText(), jtAreas[0].getText(), jtFields[2].getText(), jtAreas[1].getText());
            jl.setText(sendRequest(order));
        }
    });
    JRootPane rootPane = SwingUtilities.getRootPane(processButton);
    rootPane.setDefaultButton(processButton);
    setVisible(true);
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) JTextArea(javax.swing.JTextArea) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JLabel(javax.swing.JLabel) JRootPane(javax.swing.JRootPane) JTextField(javax.swing.JTextField)

Example 7 with GridLayout

use of java.awt.GridLayout in project smile by haifengl.

the class SOMDemo method learn.

@Override
public JComponent learn() {
    try {
        width = Integer.parseInt(widthField.getText().trim());
        if (width < 1) {
            JOptionPane.showMessageDialog(this, "Invalid width: " + width, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid width: " + widthField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    try {
        height = Integer.parseInt(heightField.getText().trim());
        if (height < 1) {
            JOptionPane.showMessageDialog(this, "Invalid height: " + height, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid height: " + heightField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    long clock = System.currentTimeMillis();
    SOM som = new SOM(dataset[datasetIndex], width, height);
    System.out.format("SOM clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    JPanel pane = new JPanel(new GridLayout(2, 3));
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    plot.grid(som.map());
    plot.setTitle("SOM Grid");
    pane.add(plot);
    int[] membership = som.partition(clusterNumber);
    int[] clusterSize = new int[clusterNumber];
    for (int i = 0; i < membership.length; i++) {
        clusterSize[membership[i]]++;
    }
    plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    plot.setTitle("Hierarchical Clustering");
    for (int k = 0; k < clusterNumber; k++) {
        double[][] cluster = new double[clusterSize[k]][];
        for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
            if (membership[i] == k) {
                cluster[j++] = dataset[datasetIndex][i];
            }
        }
        plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
    }
    pane.add(plot);
    double[][] umatrix = som.umatrix();
    double[] umatrix1 = new double[umatrix.length * umatrix[0].length];
    for (int i = 0, k = 0; i < umatrix.length; i++) {
        for (int j = 0; j < umatrix[i].length; j++, k++) umatrix1[k] = umatrix[i][j];
    }
    plot = Histogram.plot(null, umatrix1, 20);
    plot.setTitle("U-Matrix Histogram");
    pane.add(plot);
    GaussianMixture mixture = new GaussianMixture(umatrix1);
    double w = (Math.max(umatrix1) - Math.min(umatrix1)) / 24;
    double[][] p = new double[50][2];
    for (int i = 0; i < p.length; i++) {
        p[i][0] = Math.min(umatrix1) + i * w;
        p[i][1] = mixture.p(p[i][0]) * w;
    }
    plot.line(p, Color.RED);
    plot = Hexmap.plot(umatrix, Palette.jet(256));
    plot.setTitle("U-Matrix");
    pane.add(plot);
    /*
        double[][] x = new double[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                x[i][j] = som.getMap()[i][j][0];
            }
        }
        plot = PlotCanvas.hexmap(x, Palette.jet(256));
        plot.setTitle("X");
        pane.add(plot);

        double[][] y = new double[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                y[i][j] = som.getMap()[i][j][1];
            }
        }
        plot = PlotCanvas.hexmap(y, Palette.jet(256));
        plot.setTitle("Y");
        pane.add(plot);
*/
    return pane;
}
Also used : SOM(smile.vq.SOM) JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) GaussianMixture(smile.stat.distribution.GaussianMixture) PlotCanvas(smile.plot.PlotCanvas)

Example 8 with GridLayout

use of java.awt.GridLayout in project smile by haifengl.

the class PlotPanel method organize.

/**
     * Reorganize the plots in the frame. Basically, it reset the surface layout
     * based on the number of plots in the frame.
     */
private void organize() {
    int m = (int) Math.sqrt(contentPane.getComponentCount());
    if (m <= 0)
        m = 1;
    contentPane.setLayout(new GridLayout(m, 0, 0, 0));
}
Also used : GridLayout(java.awt.GridLayout)

Example 9 with GridLayout

use of java.awt.GridLayout in project smile by haifengl.

the class PCADemo method learn.

@Override
public JComponent learn() {
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    String[] names = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
    if (names[0] == null) {
        names = null;
    }
    boolean cor = corBox.getSelectedIndex() != 0;
    long clock = System.currentTimeMillis();
    PCA pca = new PCA(data, cor);
    System.out.format("Learn PCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    JPanel pane = new JPanel(new GridLayout(1, 2));
    PlotCanvas scree = PlotCanvas.screeplot(pca);
    scree.setTitle("Variance");
    pane.add(scree);
    pca.setProjection(3);
    double[][] y = pca.project(data);
    PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("Scatter Plot");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) PCA(smile.projection.PCA) PlotCanvas(smile.plot.PlotCanvas)

Example 10 with GridLayout

use of java.awt.GridLayout in project smile by haifengl.

the class PPCADemo method learn.

@Override
public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(2, 2));
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    String[] names = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
    if (names[0] == null) {
        names = null;
    }
    long clock = System.currentTimeMillis();
    PCA pca = new PCA(data, true);
    System.out.format("Learn PCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    pca.setProjection(2);
    double[][] y = pca.project(data);
    PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("PCA");
    pane.add(plot);
    pca.setProjection(3);
    y = pca.project(data);
    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("PCA");
    pane.add(plot);
    PPCA ppca = new PPCA(data, 2);
    y = ppca.project(data);
    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("PPCA");
    pane.add(plot);
    clock = System.currentTimeMillis();
    ppca = new PPCA(data, 3);
    System.out.format("Learn PPCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    y = ppca.project(data);
    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("PPCA");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) PPCA(smile.projection.PPCA) PCA(smile.projection.PCA) PPCA(smile.projection.PPCA) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

GridLayout (java.awt.GridLayout)142 JPanel (javax.swing.JPanel)106 JLabel (javax.swing.JLabel)54 BorderLayout (java.awt.BorderLayout)50 Dimension (java.awt.Dimension)32 JButton (javax.swing.JButton)32 ActionEvent (java.awt.event.ActionEvent)29 JScrollPane (javax.swing.JScrollPane)27 ActionListener (java.awt.event.ActionListener)25 JTextField (javax.swing.JTextField)23 Insets (java.awt.Insets)21 JCheckBox (javax.swing.JCheckBox)21 GridBagConstraints (java.awt.GridBagConstraints)17 GridBagLayout (java.awt.GridBagLayout)17 TitledBorder (javax.swing.border.TitledBorder)17 PlotCanvas (smile.plot.PlotCanvas)16 BoxLayout (javax.swing.BoxLayout)15 EmptyBorder (javax.swing.border.EmptyBorder)15 FlowLayout (java.awt.FlowLayout)13 ButtonGroup (javax.swing.ButtonGroup)10