1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class fenetre extends JFrame {
JButton Calculer,AfficherGraph,Quitter;
JLabel label1,label2,label3;
JTextField recupa,recupb,recupc;
JTextArea text1;
JPanel panneau1;
panneau2 graph;
//gestion des evenement pour les elements de controle
class ecouteur implements ActionListener {
public void actionPerformed(ActionEvent e) {
String boutton;
boutton= e.getActionCommand();
if(boutton.equals("Calculer"))
{
initialisePoly();
}
if(boutton.equals("Quitter"))
{
System.exit(0);
}
if(boutton.equals("AfficherGraph"))
{
graph.repaint();
}
}
}
//constructeur
fenetre(String titre) {
super(titre);
//creation des elements de l interface
Calculer =new JButton("Calculer");
AfficherGraph =new JButton("AfficherGraph");
Quitter=new JButton("Quitter");
label1=new JLabel("Enter les valeur pour a,b et c");
label2=new JLabel("X² + ");
label3=new JLabel("X + ");
recupa=new JTextField(4);
recupb=new JTextField(4);
recupc=new JTextField(4);
text1=new JTextArea(4,40);
panneau1 =new JPanel(); //va recevoir text1
panneau1.setLayout(new FlowLayout());
panneau1.setBackground(Color.yellow);
panneau1.add(text1);
graph = new panneau2();
//gestionnaire de positionnement des element de l interface
setLayout(new FlowLayout());
//integration des elements.
add(label1);
add(recupa);
add(label2);
add(recupb);
add(label3);
add(recupc);
add(panneau1);
add(graph);
add(Calculer);
add(AfficherGraph);
add(Quitter);
//fermeture de l application a la fermeture de la fenetre
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Enregistrement des boutons
Calculer.addActionListener(new ecouteur());
AfficherGraph.addActionListener(new ecouteur());
Quitter.addActionListener(new ecouteur());
}
//methode pour initialiser le polynome
void initialisePoly()
{
double a,b,c;
a=Double.parseDouble(recupa.getText());
b=Double.parseDouble(recupb.getText());
c=Double.parseDouble(recupc.getText());
polynome poly =new polynome(a,b,c);
if (poly.calculDelta() >=0)
{
System.out.println("delta= "+poly.calculDelta());
System.out.println("s1= "+poly.calculS1());
System.out.println("s2= "+poly.calculS2());
String Afficher1,Afficher2,Afficher3; //variable qui recoivent une chaine de caractere pour envoyer dans text1 (JTexteArea)
Afficher1=("delta=" +poly.calculDelta()+"\n");
Afficher2=("Solution1= " +poly.calculS1() +"\n");
Afficher3=("Solution2= " +poly.calculS2() +"\n");
text1.setText(Afficher1+Afficher2+Afficher3);
}
else
{
System.out.println("delta= "+poly.calculDelta());
System.out.println("s1= "+poly.calculScomp1());
System.out.println("s2= "+poly.calculScomp2());
String Afficher1,Afficher2,Afficher3; //variable qui recoivent une chaine de caractere pour envoyer dans text1 (JTexteArea)
Afficher1=("delta=" +poly.calculDelta()+"\n");
Afficher2=("Solution1= " +poly.calculScomp1() +"\n");
Afficher3=("Solution2= " +poly.calculScomp2() +"\n");
text1.setText(Afficher1+Afficher2+Afficher3);
}
}
//creation du panneau pour dessiner le graph
class panneau2 extends JPanel {
panneau2() {
setBackground(Color.black);
setForeground(Color.green);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
double x,y;
int xpos,ypos;
//pacement de l origine
g.translate(150,150);
//dessin des axes
g.setColor(Color.red);
g.drawLine(0,-150,0,150);
g.drawLine(-150,0,150,0);
g.drawString("-3",-150,12);
g.drawString("-3",4,147);
g.drawString("+3",135,12);
g.drawString("+3",4,-140);
//couleur servant a dessiner la fonction
g.setColor(new Color(255,255,0));
}
}
} |
Partager