Java - como redibujar...

 
Vista:

como redibujar...

Publicado por Ferran (1 intervención) el 13/05/2007 23:55:31
En un applet k hice utilizand repaint(); podia redibujar el applet sin tener k maximizar todo el rato, no obstante con este pograma no puedo conseguirlo... haber si alguien puede echarme una mano, y gracias de antemano.

Aqui les dejo el código del programa.

//package myprojects.check01;

import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;


class MatriusG extends Frame {

public MatriusG() { //constructor

CalculMatriu panel = new CalculMatriu();
add(panel,BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) {
System.out.println("Starting CalculMatriu");
MatriusG mainFrame = new MatriusG();
mainFrame.setBackground(Color.green);
mainFrame.setSize(700,300);
mainFrame.setTitle("Matrius");
mainFrame.setVisible(true);
}
}

class CalculMatriu extends Panel implements ActionListener {

Label matriu1 = new Label ("Introdueix l'ordre de la matriu 1:", Label.LEFT);
TextField fila1 = new TextField(2);
TextField columna1 = new TextField(2);

Label matriu2 = new Label ("Introdueix l'ordre de la matriu 2:", Label.LEFT);
TextField fila2 = new TextField(2);

TextField mA1 = new TextField();
TextField mA2 = new TextField();
TextField mA3 = new TextField();
TextField mA4 = new TextField();

TextField mB1 = new TextField();
TextField mB2 = new TextField();
TextField mB3 = new TextField();
TextField mB4 = new TextField();

TextField columna2 = new TextField(2);


public void actionPerformed(ActionEvent e) {

Panel resultant = new Panel();
resultant.setLayout(new GridLayout(2,2));

TextField resultant1 = new TextField(2);
resultant.add(resultant1);
TextField resultant2 = new TextField(2);
resultant.add(resultant2);
TextField resultant3 = new TextField(2);
resultant.add(resultant3);
TextField resultant4 = new TextField(2);
resultant.add(resultant4);


if(e.getActionCommand().equals("sortir")) { //salida

System.exit(0);
}

if(e.getActionCommand().equals("s")) { //suma

int a1 = Integer.parseInt(mA1.getText());
int a2 = Integer.parseInt(mA2.getText());
int a3 = Integer.parseInt(mA3.getText());
int a4 = Integer.parseInt(mA4.getText());

int b1 = Integer.parseInt(mB1.getText());
int b2 = Integer.parseInt(mB2.getText());
int b3 = Integer.parseInt(mB3.getText());
int b4 = Integer.parseInt(mB4.getText());

int sum1 = a1+b1;
int sum2 = a2+b2;
int sum3 = a3+b3;
int sum4 = a4+b4;

resultant1.setText(""+sum1);

resultant2.setText(""+sum2);

resultant3.setText(""+sum3);

resultant4.setText(""+sum4);

add(resultant,BorderLayout.EAST);

repaint();

}

else if(e.getActionCommand().equals("r")) { //resta

int a1 = Integer.parseInt(mA1.getText());
int a2 = Integer.parseInt(mA2.getText());
int a3 = Integer.parseInt(mA3.getText());
int a4 = Integer.parseInt(mA4.getText());

int b1 = Integer.parseInt(mB1.getText());
int b2 = Integer.parseInt(mB2.getText());
int b3 = Integer.parseInt(mB3.getText());
int b4 = Integer.parseInt(mB4.getText());

int res1 = a1-b1;
int res2 = a2-b2;
int res3 = a3-b3;
int res4 = a4-b4;

resultant1.setText("");
resultant1.setText(""+res1);

resultant2.setText(""+res2);

resultant3.setText(""+res3);

resultant4.setText(""+res4);

add(resultant,BorderLayout.EAST);

repaint();

}

else if(e.getActionCommand().equals("m")) { //producto

int a1 = Integer.parseInt(mA1.getText());
int a2 = Integer.parseInt(mA2.getText());
int a3 = Integer.parseInt(mA3.getText());
int a4 = Integer.parseInt(mA4.getText());

int b1 = Integer.parseInt(mB1.getText());
int b2 = Integer.parseInt(mB2.getText());
int b3 = Integer.parseInt(mB3.getText());
int b4 = Integer.parseInt(mB4.getText());

int mult1 = (a1*b1)+(a2*b3);
int mult2 = (a1*b2)+(a2*b4);
int mult3 = (a3*b1)+(a4*b3);
int mult4 = (a3*b2)+(a4*b4);

resultant1.setText("");
resultant1.setText(""+mult1);

resultant2.setText(""+mult2);

resultant3.setText(""+mult3);

resultant4.setText(""+mult4);

add(resultant,BorderLayout.EAST);

repaint();

}

else if(e.getActionCommand().equals("t1")) { //transpuesta matriz 1

int a1 = Integer.parseInt(mA1.getText());
int a2 = Integer.parseInt(mA2.getText());
int a3 = Integer.parseInt(mA3.getText());
int a4 = Integer.parseInt(mA4.getText());

int Atrans1 = a1;
int Atrans2 = a3;
int Atrans3 = a2;
int Atrans4 = a4;

resultant1.setText("");
resultant1.setText(""+Atrans1);

resultant2.setText(""+Atrans2);

resultant3.setText(""+Atrans3);

resultant4.setText(""+Atrans4);

add(resultant,BorderLayout.EAST);

repaint();

}

else if(e.getActionCommand().equals("t2")) { //transpuesta matriz 2

int b1 = Integer.parseInt(mA1.getText());
int b2 = Integer.parseInt(mA2.getText());
int b3 = Integer.parseInt(mA3.getText());
int b4 = Integer.parseInt(mA4.getText());

int Btrans1 = b1;
int Btrans2 = b3;
int Btrans3 = b2;
int Btrans4 = b4;

resultant1.setText("");
resultant1.setText(""+Btrans1);

resultant2.setText(""+Btrans2);

resultant3.setText(""+Btrans3);

resultant4.setText(""+Btrans4);

add(resultant,BorderLayout.EAST);

repaint();

}

else if(e.getActionCommand().equals("i1")) { //inversa matriz 1

int a1 = Integer.parseInt(mA1.getText());
int a2 = Integer.parseInt(mA2.getText());
int a3 = Integer.parseInt(mA3.getText());
int a4 = Integer.parseInt(mA4.getText());

int Ainv1 = a4/((a1*a4)-(a3*a2));
int Ainv2 = -a2/((a1*a4)-(a3*a2));
int Ainv3 = -a3/((a1*a4)-(a3*a2));
int Ainv4 = a1/((a1*a4)-(a3*a2));

resultant1.setText("");
resultant1.setText(""+Ainv1);

resultant2.setText(""+Ainv2);

resultant3.setText(""+Ainv3);

resultant4.setText(""+Ainv4);

add(resultant,BorderLayout.EAST);

repaint();

}

else if(e.getActionCommand().equals("i2")) { //inversa matriz 2

int b1 = Integer.parseInt(mA1.getText());
int b2 = Integer.parseInt(mA2.getText());
int b3 = Integer.parseInt(mA3.getText());
int b4 = Integer.parseInt(mA4.getText());

int Binv1 = b4/((b1*b4)-(b3*b2));
int Binv2 = -b2/((b1*b4)-(b3*b2));
int Binv3 = -b3/((b1*b4)-(b3*b2));
int Binv4 = b1/((b1*b4)-(b3*b2));

resultant1.setText("");
resultant1.setText(""+Binv1);

resultant2.setText(""+Binv2);

resultant3.setText(""+Binv3);

resultant4.setText(""+Binv4);

add(resultant,BorderLayout.EAST);

repaint();

}
}



public CalculMatriu() { //constructor
super();

setLayout(new BorderLayout());

Panel panelm=new Panel();
panelm.setLayout(new GridLayout(6,2));

Label mat1 = new Label ("MATRIU 1");
mat1.setBackground(Color.green);
panelm.add(mat1);

Label verd = new Label ();
verd.setBackground(Color.green);
panelm.add(verd);

panelm.add(mA1);
panelm.add(mA2);
panelm.add(mA3);
panelm.add(mA4);


Label mat2 = new Label ("MATRIU 2");
mat2.setBackground(Color.green);
panelm.add(mat2);

Label verd1 = new Label ();
verd1.setBackground(Color.green);
panelm.add(verd1); //separación matriz y matriz 2

panelm.add(mB1);
panelm.add(mB2);
panelm.add(mB3);
panelm.add(mB4);


add(panelm,BorderLayout.WEST);


Panel panel = new Panel();
panel.setLayout(new GridLayout(1,8));

Button but_0 = new Button("Suma");
panel.add(but_0);
but_0.setActionCommand("s");
but_0.addActionListener(this);

Button but_1 = new Button("Resta");
panel.add(but_1);
but_1.setActionCommand("r");
but_1.addActionListener(this);

Button but_2 = new Button("Multiplicació");
panel.add(but_2);
but_2.setActionCommand("m");
but_2.addActionListener(this);

Button but_3 = new Button("Transposada 1");
panel.add(but_3);
but_3.setActionCommand("t1");
but_3.addActionListener(this);

Button but_4 = new Button("Transposada 2");
panel.add(but_4);
but_4.setActionCommand("t2");
but_4.addActionListener(this);

Button but_5 = new Button("Inversa 1");
panel.add(but_5);
but_5.setActionCommand("i1");
but_5.addActionListener(this);

Button but_6 = new Button("Inversa 2");
panel.add(but_6);
but_6.setActionCommand("i2");
but_6.addActionListener(this);

Button but_sort = new Button("SORTIR");
panel.add(but_sort);
but_sort.setActionCommand("sortir");
but_sort.addActionListener(this);



add(panel,BorderLayout.NORTH);




}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder