Programmation Java/Swing/Distance
< Programmation Java | Swing
Aller à la navigation
Aller à la recherche
Interfaces graphiques avec swing[modifier | modifier le wikicode]
Calculer la distance entre 2 points[modifier | modifier le wikicode]
Le fichier Distance.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Distance extends JFrame implements ActionListener
{
private JLabel textXA,textYA,textXB,textYB,textDistance;
private JTextField editXA,editXB,editYA,editYB,editDistance;
private JButton editA,editB;
private Point A,B;
public static void main(String [] args)
{
Distance app=new Distance();
}
Distance()
{
setBounds(10,10,400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c;
c=getContentPane();
BorderLayout bl=new BorderLayout();
c.setLayout(bl);
JPanel p=new JPanel();
p.setLayout(null);
textXA= new JLabel("Abscisse de A : ");
textXA.setBounds(10,20,100,20);
p.add(textXA);
textYA= new JLabel("Ordonnée de A : ");
textYA.setBounds(10,50,100,20);
p.add(textYA);
textXB= new JLabel("Abscisse de B : ");
textXB.setBounds(10,90,100,20);
p.add(textXB);
textYB= new JLabel("Ordonnée de B : ");
textYB.setBounds(10,120,100,20);
p.add(textYB);
textDistance= new JLabel("La distance AB vaut : ");
textDistance.setBounds(10,170,150,20);
p.add(textDistance);
editXA=new JTextField(20);
editXA.setBounds(120,20,100,20);
editXA.setEditable(false);
p.add(editXA);
editYA=new JTextField(20);
editYA.setBounds(120,50,100,20);
editYA.setEditable(false);
p.add(editYA);
editXB=new JTextField(20);
editXB.setBounds(120,90,100,20);
editXB.setEditable(false);
p.add(editXB);
editYB=new JTextField(10);
editYB.setBounds(120,120,100,20);
editYB.setEditable(false);
p.add(editYB);
editDistance=new JTextField(20);
editDistance.setBounds(150,170,150,20);
editDistance.setEditable(false);
p.add(editDistance);
Border bord=BorderFactory.createRaisedBevelBorder();
editA=new JButton("MODIFIER A");
editA.setActionCommand("EDITA");
editA.addActionListener(this);
editA.setBorder(bord);
editA.setBounds(240,35,100,20);
p.add(editA);
editB=new JButton("MODIFIER B");
editB.setActionCommand("EDITB");
editB.addActionListener(this);
editB.setBorder(bord);
editB.setBounds(240,105,100,20);
p.add(editB);
c.add(p, BorderLayout.CENTER);
setVisible(true);
A=new Point();
B=new Point();
ShowPoint();
}
void ShowPoint()
{
editXA.setText(String.valueOf(A.getX()));
editYA.setText(String.valueOf(A.getY()));
editXB.setText(String.valueOf(B.getX()));
editYB.setText(String.valueOf(B.getY()));
editDistance.setText(String.valueOf(A.distance(B)));
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("EDITA"))
{
PointDialog dialog=new PointDialog(this,"Modification du point A",A);
dialog.setVisible(true);
ShowPoint();
}
else
if(e.getActionCommand().equals("EDITB"))
{
PointDialog dialog=new PointDialog(this,"Modification du point B",B);
dialog.setVisible(true);
ShowPoint();
}
}
}
Le fichier Point.java
public class Point {
private double x,y;
Point()
{
x=0;
y=0;
}
public double getX(){return x;}
double getY(){return y;}
void setXY(double x,double y)
{
this.x=x;
this.y=y;
}
double distance(Point P)
{
double dx,dy;
dx=x-P.x;
dy=y-P.y;
return java.lang.Math.sqrt(dx*dx+dy*dy);
}
}
Le fichier PointDialog.java
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;
import java.awt.*;
public class PointDialog extends JDialog implements ActionListener
{
private Point P;
JButton OK,Cancel;
JLabel textX,textY;
JTextField editX,editY;
PointDialog(JFrame f,String s,Point P)
{
super(f,s,true);
this.P=P;
setLocationRelativeTo(f);
setBounds(500,100,400,200);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
Container c=getContentPane();
c.setLayout(null);
textX=new JLabel("Abscisse du point :");
textX.setBounds(10,20,120,20);
c.add(textX);
textY=new JLabel("Ordonnée du point :");
textY.setBounds(10,50,120,20);
c.add(textY);
editX=new JTextField(20);
editX.setBounds(140,20,100,20);
c.add(editX);
editY=new JTextField(20);
editY.setBounds(140,50,100,20);
c.add(editY);
Border bord=BorderFactory.createRaisedBevelBorder();
OK=new JButton("OK");
OK.setActionCommand("OK");
OK.addActionListener(this);
OK.setBorder(bord);
OK.setBounds(260,25,60,20);
c.add(OK);
Cancel=new JButton("Annuler");
Cancel.setActionCommand("CANCEL");
Cancel.addActionListener(this);
Cancel.setBorder(bord);
Cancel.setBounds(260,50,60,20);
c.add(Cancel);
ShowPoint();
}
private void ShowPoint()
{
editX.setText(String.valueOf(P.getX()));
editY.setText(String.valueOf(P.getY()));
}
public void actionPerformed(ActionEvent e)
{
double x,y;
if(e.getActionCommand().equals("OK"))
{
boolean ok=true;
if(ok) {
try{
x=Double.parseDouble(editX.getText());
}catch(RuntimeException ex)
{x=P.getX();editX.setText(String.valueOf(x));ok=false;}
try{
y=Double.parseDouble(editY.getText());
}catch(RuntimeException ex)
{y=P.getY();editY.setText(String.valueOf(y));ok=false;}
if(ok)
{
P.setXY(x,y);
dispose();
}
}
}
else if(e.getActionCommand().equals("CANCEL"))
{
dispose();
}
}
}