Last active
May 17, 2019 17:00
-
-
Save Mati365/99dae53fea1759503b6a1765e38a19ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://joanpaon.wordpress.com/2013/07/12/how-to-install-a-custom-swing-component-in-netbeans-palette/ | |
package com.korporejszyn.matrix; | |
import java.awt.BorderLayout; | |
import java.awt.Dimension; | |
import java.awt.GridBagConstraints; | |
import java.awt.GridBagLayout; | |
import java.awt.GridLayout; | |
import javax.swing.Box; | |
import javax.swing.BoxLayout; | |
import javax.swing.JButton; | |
import javax.swing.JComponent; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JTextField; | |
class MatrixData { | |
private int w = 0, h = 0; | |
private float[][] values = null; | |
public MatrixData(int w, int h, float[][] values) { | |
this.values = values; | |
this.w = w; | |
this.h = h; | |
} | |
public static MatrixData add(MatrixData a, MatrixData b, int sign) { | |
float[][] values = new float[a.h][a.w]; | |
for (int i = 0; i < a.h; ++i) { | |
for (int j = 0; j < a.w; ++j) { | |
values[i][j] += a.values[i][j] + (sign * b.values[i][j]); | |
} | |
} | |
return new MatrixData(a.w, a.h, values); | |
} | |
public static MatrixData mul(MatrixData a, MatrixData b) { | |
int w = a.h, h = b.w; | |
float[][] values = new float[w][h]; | |
for(int i = 0; i < w; i++) { | |
for (int j = 0; j < h; j++) { | |
for (int k = 0; k < w; k++) { | |
values[i][j] += a.values[i][k] * b.values[k][j]; | |
} | |
} | |
} | |
return new MatrixData(w, h, values); | |
} | |
public float[][] getValues() { return values; } | |
public int getWidth() { return w; } | |
public int getHeight() { return h; } | |
} | |
class MatrixSheet extends JPanel { | |
private int w = 0, h = 0; | |
private JTextField[][] texts = null; | |
public MatrixSheet(int w, int h) { | |
this.w = w; | |
this.h = h; | |
this.texts = new JTextField[h][w]; | |
setLayout(new GridLayout(w, h)); | |
for (int i = 0; i < h; ++i) { | |
for (int j = 0; j < w; ++j) { | |
add(texts[i][j] = new JTextField()); | |
} | |
} | |
} | |
public MatrixData getValues() { | |
float[][] values = new float[h][w]; | |
for (int i = 0; i < h; ++i) { | |
for (int j = 0; j < w; ++j) { | |
String text = texts[i][j].getText(); | |
values[i][j] = Float.parseFloat( | |
text.length() > 0 ? text : "0.0" | |
); | |
} | |
} | |
return new MatrixData(w, h, values); | |
} | |
public void setValues(MatrixData matrix) { | |
float[][] values = matrix.getValues(); | |
for (int i = 0; i < h; ++i) { | |
for (int j = 0; j < w; ++j) { | |
texts[i][j].setText(String.valueOf(values[i][j])); | |
} | |
} | |
} | |
public void reset() { | |
for (int i = 0; i < h; ++i) { | |
for (int j = 0; j < w; ++j) { | |
texts[i][j].setText(" "); | |
} | |
} | |
} | |
} | |
class ToolbarMatrixButton extends JButton { | |
public ToolbarMatrixButton(String text) { | |
super(text); | |
setContentAreaFilled(false); | |
setPreferredSize(new Dimension(64, 64)); | |
setMaximumSize(new Dimension(64, 64)); | |
} | |
} | |
class CenteredBox extends Box { | |
public JPanel inner = new JPanel(); | |
public CenteredBox(JComponent item) { | |
super(BoxLayout.Y_AXIS); | |
setAlignmentX(JComponent.CENTER_ALIGNMENT); | |
add(Box.createVerticalGlue()); | |
inner.setAlignmentX(JComponent.CENTER_ALIGNMENT); | |
inner.setLayout(new BoxLayout(inner, BoxLayout.Y_AXIS)); | |
if (item != null) | |
inner.add(item); | |
add(inner); | |
add(Box.createVerticalGlue()); | |
} | |
} | |
class MatrixSheetsRow extends JPanel { | |
private MatrixSheet a = null, b = null, output = null; | |
public MatrixSheetsRow() { | |
this(4, 4); | |
} | |
public MatrixSheetsRow(int w, int h) { | |
this.a = new MatrixSheet(w, h); | |
this.b = new MatrixSheet(w, h); | |
this.output = new MatrixSheet(w, h); | |
setLayout(new GridBagLayout()); | |
GridBagConstraints c = new GridBagConstraints(); | |
c.fill = GridBagConstraints.BOTH; | |
c.gridx = 0; | |
c.gridy = 0; | |
c.weightx = 0.5; | |
c.weighty = 1.0; | |
add(a, c); | |
CenteredBox toolbar = this.createToolbarList(); | |
c.gridx = 1; | |
c.gridy = 0; | |
c.weightx = 0.1; | |
c.weighty = 1.0; | |
add(toolbar, c); | |
c.gridx = 2; | |
c.gridy = 0; | |
c.weightx = 0.5; | |
c.weighty = 1.0; | |
add(b, c); | |
CenteredBox operation = new CenteredBox(new CenteredBox(new JLabel("="))); | |
c.gridx = 3; | |
c.gridy = 0; | |
c.weightx = 0.1; | |
c.weighty = 1.0; | |
add(operation, c); | |
c.gridx = 4; | |
c.gridy = 0; | |
c.weightx = 0.5; | |
c.weighty = 1.0; | |
add(output, c); | |
} | |
private Boolean checkValues() { | |
try { | |
a.setValues(a.getValues()); | |
b.setValues(b.getValues()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
JOptionPane.showMessageDialog(this, "Nieprawidłowe wprowadzone dane!", "Błąd", JOptionPane.ERROR_MESSAGE); | |
} | |
return true; | |
} | |
private CenteredBox createToolbarList() { | |
CenteredBox toolbar = new CenteredBox(null); | |
JButton add = new ToolbarMatrixButton("+"); | |
add.addActionListener(e -> { | |
if (checkValues()) { | |
output.setValues( | |
MatrixData.add(a.getValues(), b.getValues(), 1) | |
); | |
} | |
}); | |
toolbar.inner.add(add); | |
toolbar.inner.add(Box.createRigidArea(new Dimension(0, 10))); | |
JButton sub = new ToolbarMatrixButton("-"); | |
toolbar.inner.add(sub); | |
sub.addActionListener(e -> { | |
if (checkValues()) { | |
output.setValues( | |
MatrixData.add(a.getValues(), b.getValues(), -1) | |
); | |
} | |
}); | |
toolbar.inner.add(Box.createRigidArea(new Dimension(0, 10))); | |
JButton mul = new ToolbarMatrixButton("*"); | |
mul.addActionListener(e -> { | |
if (checkValues()) { | |
output.setValues( | |
MatrixData.mul(a.getValues(), b.getValues()) | |
); | |
} | |
}); | |
toolbar.inner.add(mul); | |
return toolbar; | |
} | |
} | |
public class MatrixOperationPanel extends JPanel { | |
private int w = 4, h = 4; | |
private MatrixSheetsRow sheet = null; | |
public MatrixOperationPanel() { | |
setLayout(new BorderLayout()); | |
rescaleUI(); | |
} | |
private void rescaleUI () { | |
if (sheet != null) | |
remove(sheet); | |
add(sheet = new MatrixSheetsRow(w, h), BorderLayout.CENTER); | |
} | |
public void setHorizontalCells(int w) { | |
this.w = w; | |
rescaleUI(); | |
} | |
public void setVerticalCells(int h) { | |
this.h = h; | |
rescaleUI(); | |
} | |
public int getHorizontalCells() { return w; } | |
public int getVeritcalCells() { return h; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment