help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
java-math-tools
/
src
/
main
/
java
/
com
/
rskworld
/
mathtools
/
panels
RSK World
java-math-tools
Java Math Tools - 15 Mathematical Tools + Scientific Calculator + Geometry + Statistics + Financial Calculators + Swing GUI + Educational Design
panels
  • AgeCalculatorPanel.java2.4 KB
  • BMIPanel.java2.3 KB
  • CalculatorPanel.java4.5 KB
  • ConstantsPanel.java1.6 KB
  • EquationSolverPanel.java3.3 KB
  • FinancialPanel.java3.2 KB
  • GSTPanel.java2.1 KB
  • GeometryPanel.java4 KB
  • HistoryPanel.java1.8 KB
  • MatrixPanel.java4.2 KB
  • NumberSystemPanel.java3 KB
  • PasswordPanel.java3.2 KB
  • PlotterPanel.java3 KB
  • PrimePanel.java3.5 KB
  • StatisticsPanel.java3.5 KB
  • UnitConverterPanel.java5.8 KB
MatrixPanel.java
src/main/java/com/rskworld/mathtools/panels/MatrixPanel.java
Raw Download
Find: Go to:
/**
 * Project: Java Basic Math Tools
 * Developer: Molla Samser
 * Designer & Tester: Rima Khatun
 * Website: https://rskworld.in
 * Year: 2026
 */

package com.rskworld.mathtools.panels;

import com.rskworld.mathtools.utils.ThemeManager;
import javax.swing.*;
import java.awt.*;

public class MatrixPanel extends JPanel {
    private JTextField[][] matrixA = new JTextField[3][3];
    private JTextField[][] matrixB = new JTextField[3][3];
    private JTextField[][] matrixResult = new JTextField[3][3];

    public MatrixPanel() {
        setLayout(new BorderLayout(10, 10));
        setBackground(ThemeManager.BACKGROUND);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        initComponents();
    }

    private void initComponents() {
        JPanel matricesContainer = new JPanel(new GridLayout(1, 3, 20, 0));
        matricesContainer.setOpaque(false);

        matricesContainer.add(createMatrixInputPanel("Matrix A", matrixA));
        matricesContainer.add(createMatrixInputPanel("Matrix B", matrixB));
        matricesContainer.add(createMatrixInputPanel("Result", matrixResult, false));

        add(matricesContainer, BorderLayout.CENTER);

        JPanel controlPanel = new JPanel(new FlowLayout());
        controlPanel.setOpaque(false);

        String[] ops = {"Add (A+B)", "Sub (A-B)", "Mul (A*B)", "Clear"};
        for (String op : ops) {
            JButton btn = new JButton(op);
            btn.addActionListener(e -> handleOperation(op));
            controlPanel.add(btn);
        }

        add(controlPanel, BorderLayout.SOUTH);
        ThemeManager.applyTheme(this);
    }

    private JPanel createMatrixInputPanel(String title, JTextField[][] fields) {
        return createMatrixInputPanel(title, fields, true);
    }

    private JPanel createMatrixInputPanel(String title, JTextField[][] fields, boolean editable) {
        JPanel p = new JPanel(new BorderLayout());
        p.setOpaque(false);
        JLabel lbl = new JLabel(title, JLabel.CENTER);
        lbl.setFont(new Font("SansSerif", Font.BOLD, 14));
        p.add(lbl, BorderLayout.NORTH);

        JPanel grid = new JPanel(new GridLayout(3, 3, 5, 5));
        grid.setOpaque(false);
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                fields[i][j] = new JTextField("0", 3);
                fields[i][j].setHorizontalAlignment(JTextField.CENTER);
                fields[i][j].setEditable(editable);
                grid.add(fields[i][j]);
            }
        }
        p.add(grid, BorderLayout.CENTER);
        return p;
    }

    private void handleOperation(String op) {
        try {
            double[][] a = getMatrixValues(matrixA);
            double[][] b = getMatrixValues(matrixB);
            double[][] res = new double[3][3];

            if (op.startsWith("Add")) {
                for(int i=0; i<3; i++) for(int j=0; j<3; j++) res[i][j] = a[i][j] + b[i][j];
            } else if (op.startsWith("Sub")) {
                for(int i=0; i<3; i++) for(int j=0; j<3; j++) res[i][j] = a[i][j] - b[i][j];
            } else if (op.startsWith("Mul")) {
                for(int i=0; i<3; i++) {
                    for(int j=0; j<3; j++) {
                        for(int k=0; k<3; k++) res[i][j] += a[i][k] * b[k][j];
                    }
                }
            } else {
                for(int i=0; i<3; i++) for(int j=0; j<3; j++) { matrixA[i][j].setText("0"); matrixB[i][j].setText("0"); matrixResult[i][j].setText("0"); }
                return;
            }
            setMatrixValues(matrixResult, res);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid Matrix Input");
        }
    }

    private double[][] getMatrixValues(JTextField[][] fields) {
        double[][] vals = new double[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) vals[i][j] = Double.parseDouble(fields[i][j].getText());
        }
        return vals;
    }

    private void setMatrixValues(JTextField[][] fields, double[][] vals) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) fields[i][j].setText(String.format("%.1f", vals[i][j]));
        }
    }
}
116 lines•4.2 KB
java

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer