You are on page 1of 6

MULTIPLICACION DE MATRICES

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class AWT_TAREA2 extends JFrame{
JTextField t1,t2,t3;
JButton b1,b2,b3;
JLabel l1,l2,l3;
        public AWT_TAREA2()
        {
            super("PROYECTO");
            b1=new JButton("MATRICES");
            b2=new JButton("GRAFICOS");
            b3=new JButton("SALIR");
            add(b1);b1.setBounds(250,80,100,30);
            add(b2);b2.setBounds(250,130,100,30);
            add(b3);b3.setBounds(250,180,100,30);
            b1.addActionListener(new ActionListener()
            {  
            public void actionPerformed(ActionEvent e)
            {
                MATRICES proy=new MATRICES();
                proy.setLayout(null);
                proy.setSize(600,600);
                    proy.getContentPane().setBackground(Color.CYAN  );
    proy.setLocationRelativeTo(null);
    proy.setDefaultCloseOperation(EXIT_ON_CLOSE);
                proy.setVisible(true);
            }
            }      
            );
            b2.addActionListener(new ActionListener()
            {  
            public void actionPerformed(ActionEvent e)
            {
            }
            }      
            );
            b3.addActionListener(new ActionListener()
            {  
            public void actionPerformed(ActionEvent e)
            {
            }
            }      
            );
        }
    public static void main(String[] args) {

        AWT_TAREA2 proy=new AWT_TAREA2();


    proy.setLayout(null);
    proy.setSize(600,600);
    proy.getContentPane().setBackground(Color.yellow);
    proy.setLocationRelativeTo(null);
    proy.setDefaultCloseOperation(EXIT_ON_CLOSE);
    proy.setVisible(true);
    }
}
Clase Matrices
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class MATRICES extends JFrame{
    JButton b1,b2,b3,b4;
    JLabel l1;
    JTextField t1;
    int n=0,i,j,sw=0;
    int m[][],m1[][],m2[][];
    public MATRICES()
    {
        m=new int[10][10];
        m1=new int[10][10];
        m2=new int[10][10];
        l1=new JLabel("INGRESE DIMENSION");
        t1=new JTextField();
        b1=new JButton("ACEPTAR");
        b2=new JButton("MAGICO");
        b3=new JButton("UNITARIA");
        b4=new JButton("MULTIPLICACION DE MATRICES");
        add(l1);l1.setBounds(10,20,100,30);
        add(t1);t1.setBounds(120,20,80,30);
        add(b1);b1.setBounds(210,20,80,30);
        add(b2);b2.setBounds(410,20,80,30);
        add(b3);b3.setBounds(410,60,80,30);
        add(b4);b4.setBounds(410,100,80,30);
        b1.addActionListener(new ActionListener()
            {  
            public void actionPerformed(ActionEvent e)
            {
                n=Integer.parseInt(t1.getText());
            }
            }      
            );
        b2.addActionListener(new ActionListener()
            {  
            public void actionPerformed(ActionEvent e)
            {
                int f=0,c=0,con=0;
                c=n/2;
                do
                {
                    con++;
                    m[f][c]=con;
                    if(con%n==0)
                        f++;
                    else
                    if(f==0)
                    {
                        c++;
                        f=n-1;
                    }
                    else if(c==n-1)
                    {
                        f--;
                        c=0;
                    }
                    else
                    {
                        f--;
                        c++;
                    }
                }while(con<n*n);
                sw=1;
                repaint();
            }
            }      
            );
            b3.addActionListener(new ActionListener()
            {  
            public void actionPerformed(ActionEvent e)
            {
                for(i=0;i<n;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        if(i==j)
                        {
                            m[i][j]=1;
                        }
                        else
                        {
                            m[i][j]=0;
                        }
                    }
                }
                sw=2;
                repaint();
            }
            }      
            );
            b4.addActionListener(new ActionListener()
            {  
            public void actionPerformed(ActionEvent e)
            {
                for(i=0;i<n;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        m1[i]
[j]=Integer.parseInt(JOptionPane.showInputDialog("INGRESE VALOR DE LA
MATRIZ A EN ["+i+"] ["+j+"]"));
                    }
                }
                for(i=0;i<n;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        m2[i]
[j]=Integer.parseInt(JOptionPane.showInputDialog("INGRESE VALOR DE LA
MATRIZ A EN ["+i+"] ["+j+"]"));
                    }
                }
                for(i=0;i<n;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        for(int k=0;k<n;k++)
                        {
                            m[i][j]+=m1[i][k]*m2[k][j];
                        }
                    }
                }
                sw=3;
                repaint();
            }
            }      
            );
    }
    public void paint(Graphics g)
    {   super.paint(g);
        Font fuente=new Font("Algerian",Font.BOLD,20);
        g.setFont(fuente);
        g.setColor(Color.blue);
        if(sw==1)
        {
            for(i=0;i<n;i++)
            {
                for( j=0;j<n;j++)
                {
                    g.drawString(""+m[i][j],80+j*35,200+i*35);
                }
            }
        }
        if(sw==2)
        {
            Font fuente1=new Font("Arial",Font.ITALIC,20);
            g.setFont(fuente1);
            g.setColor(Color.green);
                for(i=0;i<n;i++)
            {
                for( j=0;j<n;j++)
                {
                    g.drawString(""+m[i][j],80+j*35,200+i*35);
                }
            }
        }
        if(sw==3)
        {
            Font fuente2=new Font("Broadway",Font.ITALIC,20);
            g.setFont(fuente2);
            g.setColor(Color.black);
                for(i=0;i<n;i++)
            {
                for( j=0;j<n;j++)
                {
                    g.drawString(""+m[i][j],80+j*35,200+i*35);
                }
            }
        }
    }
}
CORRIDO DEL PROGRAMA

You might also like