Courbe en java

unreachable21 Messages postés 2 Date d'inscription mercredi 16 décembre 2009 Statut Membre Dernière intervention 6 novembre 2010 - 6 nov. 2010 à 22:17
uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 - 7 nov. 2010 à 08:18
Bonjour,
Je voudrais dessiner une courbe en java via une IHM.
J'ai une classe "Fenetre " qui étend JFrame qui contient les JTextfield ,JPanel e JButton .
Une autre classe MainClass que j'instancie dans Fenetre pour dessiner le repere cartesien .
Mon objectif est de tracer la courbe qui sera saisie dans le JTextfield selon le domaine de définition saisie aussi dans les Jtextfield DANS le repere cartesien qui est affiché au debut avant saisie des données.( la courbe sera dessiné apres clic sur le bouton)
Alors j'ai pensé à faire dans l'action performed une boucle pour
dessiner le tout avec g.drawline :
Probleme : la methode paintcomponent qui definit g.drawline ne peut etre appelé pour ce faire !!! et je ne peux pas faire g.drawline directement car il demande une instance de g qui un graphics :s

Je ne sais pas si je me suis bien exprimé mais bon , je veux dessiner une couuuuuuuuuurbe .
svp help et merci d'avance
voici le code de Fenetre:



package tp;



import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;

import javax.swing.*;

import tp.FenetrePrin.*;

import com.sun.codemodel.internal.fmt.JBinaryFile;



import java.util.StringTokenizer;



public class Fenetre extends JFrame {
JLabel fct = new JLabel("Entrez votre fonction ");
JLabel dom=new JLabel("Domaine de définition");
JLabel de=new JLabel("de");
JLabel a=new JLabel("à");
JTextField f1= new JTextField();
JTextField f2= new JTextField();
JTextField f3= new JTextField();
JButton b1=new JButton ("ok");
JPanel p1=new JPanel(new FlowLayout());
JPanel gauche=new JPanel();
JPanel droite=new JPanel();
JPanel centre = new JPanel();
JPanel esp=new JPanel();
JPanel spc=new JPanel();
JPanel enc=new JPanel();
JPanel courbe = new JPanel();
MainClass mm= new MainClass();
FenetrePrin ff=new FenetrePrin();
CardLayout gestionnaireDesCartes = new CardLayout();
JPanel wow = new JPanel();

public Fenetre(){
super();
build();
}

// foct dessiner



private void build() {

setTitle("courbe java");
setSize(700,700);
setLocationRelativeTo(null);
setResizable(false);
Font police = new Font("monotype corsiva", Font.BOLD, 30);
mm.setPreferredSize(new Dimension (410,430));
// ff.setPreferredSize(new Dimension (300,300));
gauche.setPreferredSize(new Dimension(100,50));
droite.setPreferredSize(new Dimension(180,50));
centre.setPreferredSize(new Dimension(180,50));
esp.setPreferredSize(new Dimension(50,50));
spc.setPreferredSize(new Dimension(200,50));
enc.setPreferredSize(new Dimension(40,50));
wow.setPreferredSize(new Dimension(100,400));
f1.setColumns(30);
f2.setColumns(5);
f3.setColumns(5);
p1.add(fct);
p1.add(f1);
p1.add(gauche);
p1.add(dom);
p1.add(esp);
p1.add(de);
p1.add(f2);
p1.add(enc);
p1.add(a);
p1.add(f3);
p1.add(droite);
p1.add(spc);
p1.add(b1);
p1.add(centre);
p1.setBackground(Color.white);
wow.setBackground(Color.white);
p1.add(mm);

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String nb1=f1.getText();
String min=f2.getText();
String max=f3.getText();
Graphics g=null;

int minConv= 0;
try {
minConv = Integer.parseInt(min);
} catch (NumberFormatException lol){
// n'est pas un nombre, gerer ce cas
}

int maxConv= 0;
try {
maxConv = Integer.parseInt(max);
} catch (NumberFormatException lol){
// n'est pas un nombre, gerer ce cas
}
/*for (int i=minConv;i<=maxConv-1;i++){

g.drawLine(i, i+1, Aide.fonction(nb1,i),Aide.fonction(nb1,i+1));
}
*/
p1.add(wow);

} });




this.setContentPane(p1);
setVisible(true);

}
public static void main (String [] args)
{
new Fenetre() ;
}
}


*****************************
le code de la classe pour le repère:
package tp;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {
static int Int_Ligne_Horizontal_1 = 0;
static int Int_Ligne_Horizontal_2 = 0;
static int Int_Ligne_Horizontal_Fin = 0;

static int Int_Ligne_Vertical_1 = 0;
static int Int_Ligne_Vertical_2 = 0;
static int Int_Ligne_Vertical_Fin = 0;
static double Db_x_Charge1 = 50;
static double Db_x_Pt = 0;
static double Db_y_Pt = 0;

static int Valider_OK_1 = 0;
static int Valider_OK_2 = 0;
static JPanel Panel_Options = new JPanel(null);

private int x;
private int y;
private String fct;
public MainClass () {super();}

public void method(int x,int y,String fct){
this.x=x;
this.y=y;
this.fct=fct;
}



public void paintComponent(Graphics g) {
Int_Ligne_Horizontal_Fin 0; Int_Ligne_Horizontal_1 0; Int_Ligne_Horizontal_2 = 0;
Int_Ligne_Vertical_Fin 0; Int_Ligne_Vertical_1 0; Int_Ligne_Vertical_2 = 0;

while(Int_Ligne_Horizontal_Fin <= 40)
{
g.setColor(Color.magenta);
g.drawLine(0,Int_Ligne_Horizontal_1,400,Int_Ligne_Horizontal_2);
Int_Ligne_Horizontal_1 = Int_Ligne_Horizontal_1 + 10;
Int_Ligne_Horizontal_2 = Int_Ligne_Horizontal_2 + 10;
Int_Ligne_Horizontal_Fin = Int_Ligne_Horizontal_Fin + 1;
}

while(Int_Ligne_Vertical_Fin <=40)
{
g.setColor(Color.cyan);
g.drawLine(Int_Ligne_Vertical_1,0,Int_Ligne_Vertical_2,400);
Int_Ligne_Vertical_1 = Int_Ligne_Vertical_1 + 10;
Int_Ligne_Vertical_2 = Int_Ligne_Vertical_2 + 10;
Int_Ligne_Vertical_Fin = Int_Ligne_Vertical_Fin + 1;
}

g.setColor(Color.black);

// On trace le repère

g.drawLine(0,200,400,200);
g.drawLine(200,0,200,400);

}

}

2 réponses

uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 9
7 nov. 2010 à 08:13
Salut unreachable21,

nous devons dessiner la courbe à l'intérieur de la méthode paintComponent, quelque chose comme ceci:

//Fenetre:
...
mm.courbe(minConv, maxConv, nb1);
...
//MainClass:
...
private String nb1;
private int maxConv;
private int minConv;
...
@Override
public void paintComponent(Graphics g) {
...
// On trace le repère
    g.drawLine(0, 200, 400, 200);
    g.drawLine(200, 0, 200, 400);
// On trace la courbe:
    for (int i = minConv; i <= maxConv - 1; i++) {
        g.drawLine(i, i + 1, Aide.fonction(nb1, i), Aide.fonction(nb1, i + 1));
    }
}
public void courbe(int minConv, int maxConv, String nb1) {
    this.minConv = minConv;
    this.maxConv = maxConv;
    this.nb1 = nb1;
    repaint();
}
...


Salutations,
André
0
uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 9
7 nov. 2010 à 08:18
Tu peux également consulter ce tutoriel: DESSINER AVEC SWING (1): LE MÉCANISME DE BASE

Salutations,
André
0
Rejoignez-nous