aminetkr
Messages postés2Date d'inscriptionmercredi 2 janvier 2013StatutMembreDernière intervention19 octobre 2014
-
Modifié par KX le 19/10/2014 à 13:26
Twinuts
Messages postés5375Date d'inscriptiondimanche 4 mai 2003StatutModérateurDernière intervention14 juin 2023
-
20 oct. 2014 à 13:04
Bonjour tout le monde:
je veux faire un graphique dans un repère classique orthonormé et tracer la courbe d'une fonction f(x)=sin(x) dés que je clik sur le button "Courbe", comment puis je faire???
j'ai déja fait mon répère voila le code et s'il y'a un autre code ou des modification aider moi SVP et mercie d'avance
package fonction;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.awt.image.ImageObserver.WIDTH;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fonction extends JFrame implements ActionListener{
JPanel p1,p2;
JButton b1;
public Fonction(){
p1=new JPanel();
b1=new JButton("Courbe");
p2=new JPanel();
this.setVisible(true);
this.setSize(800, 800);
b1.setBounds(3,5,50,40);
b1.addActionListener(this);
p1.setLayout(new BorderLayout());
this.add(p1,BorderLayout.CENTER);
p1.add(new mypanel());
this.add(p2,BorderLayout.SOUTH);
p1.setBorder(BorderFactory.createLineBorder(Color.black, WIDTH));
p2.setBorder(BorderFactory.createLineBorder(Color.black, WIDTH));
p2.add(b1);
}
public static void main(String[] args) {
Fonction f = new Fonction();
// TODO code application logic here
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
}
}
package fonction;
import java.awt.Graphics;
import javax.swing.JPanel;
public class mypanel extends JPanel{
public mypanel() {
}
@Override
public void paint(Graphics g) {
super.paint(g); //To change body of generated methods, choose Tools | Templates.
g.drawLine(this.getSize().width/2,0,this.getSize().width/2,this.getSize().height);
g.drawLine(0,this.getSize().height/2,this.getSize().width,this.getSize().height/2);
for(int i=0;i<this.getSize().width;i=i+10){
g.drawLine((this.getSize().width/2)-2,i,(this.getSize().width/2)+2,i);
}
for(int i=0;i<this.getSize().height;i=i+10){
g.drawLine(i,(this.getSize().height/2)-2,i,(this.getSize().height/2)+2);
}
}
}
The rules that apply to AWT's lightweight components also apply
to Swing components -- for instance, paint() gets called when it's
time to render -- except that Swing further factors the paint()
call into three separate methods, which are invoked in the
following order:
Swing programs should override paintComponent() instead of
overriding paint(). Although the API allows it, there is generally
no reason to override paintBorder() or paintComponents() (and
if you do, make sure you know what you're doing!). This
factoring makes it easier for programs to override only the
portion of the painting which they need to extend. For example,
this solves the AWT problem mentioned previously where a
failure to invoke super.paint() prevented any lightweight children
from appearing.