Programmation Java/Swing/Distance

Un livre de Wikilivres.

Interfaces graphiques avec swing[modifier | modifier le wikicode]

Calculer la distance entre 2 points[modifier | modifier le wikicode]

Le fichier Distance.java

package org.wikibooks.fr.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;

/**
 * Calcul de la distance entre 2 points.
 * @author fr.wikibooks.org
 */
public class Distance extends JFrame implements ActionListener
{
	private JLabel l_xa, l_ya, l_xb, l_yb, l_distance;
	private JTextField t_xa, t_xb, t_ya, t_yb, t_distance;
	private JButton b_edit_a, b_edit_b;

	private Point
		p_a = new Point(),
		p_b = new Point();

	public static void main(String[] args)
	{
		Distance app = new Distance();
		app.setVisible(true);
	}

	public Distance()
	{
		setBounds(10, 10, 400, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Container c = getContentPane();
		c.setLayout(new BorderLayout());

		JPanel p = new JPanel();
		p.setLayout(null);

		l_xa = new JLabel("Abscisse de A : ");
		l_xa.setBounds(10, 20, 100, 20);
		p.add(l_xa);

		l_ya = new JLabel("Ordonnée de A : ");
		l_ya.setBounds(10, 50, 100, 20);
		p.add(l_ya);

		l_xb = new JLabel("Abscisse de B : ");
		l_xb.setBounds(10, 90, 100, 20);
		p.add(l_xb);

		l_yb = new JLabel("Ordonnée de B : ");
		l_yb.setBounds(10, 120, 100, 20);
		p.add(l_yb);

		l_distance = new JLabel("La distance AB vaut : ");
		l_distance.setBounds(10, 170, 150, 20);
		p.add(l_distance);

		t_xa = new JTextField(20);
		t_xa.setBounds(120, 20, 100, 20);
		t_xa.setEditable(false);
		p.add(t_xa);

		t_ya = new JTextField(20);
		t_ya.setBounds(120, 50, 100, 20);
		t_ya.setEditable(false);
		p.add(t_ya);

		t_xb = new JTextField(20);
		t_xb.setBounds(120, 90, 100, 20);
		t_xb.setEditable(false);
		p.add(t_xb);

		t_yb = new JTextField(10);
		t_yb.setBounds(120, 120, 100, 20);
		t_yb.setEditable(false);
		p.add(t_yb);

		t_distance = new JTextField(20);
		t_distance.setBounds(150, 170, 150, 20);
		t_distance.setEditable(false);
		p.add(t_distance);

		Border bord = BorderFactory.createRaisedBevelBorder();
		b_edit_a = new JButton("MODIFIER A");
		b_edit_a.setActionCommand("EDITA");
		b_edit_a.addActionListener(this);
		b_edit_a.setBorder(bord);
		b_edit_a.setBounds(240, 35, 100, 20);
		p.add(b_edit_a);

		b_edit_b = new JButton("MODIFIER B");
		b_edit_b.setActionCommand("EDITB");
		b_edit_b.addActionListener(this);
		b_edit_b.setBorder(bord);
		b_edit_b.setBounds(240, 105, 100, 20);
		p.add(b_edit_b);

		c.add(p, BorderLayout.CENTER);

		updateValues();
	}

	private void updateValues()
	{
		t_xa.setText(String.valueOf(p_a.getX()));
		t_ya.setText(String.valueOf(p_a.getY()));
		t_xb.setText(String.valueOf(p_b.getX()));
		t_yb.setText(String.valueOf(p_b.getY()));
		t_distance.setText(String.valueOf(p_a.distance(p_b)));
	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("EDITA"))
		{
			PointDialog dialog = new PointDialog(this, "Modification du point A", p_a);
			dialog.setVisible(true);
			updateValues();
		}
		else if (e.getActionCommand().equals("EDITB"))
		{
			PointDialog dialog = new PointDialog(this, "Modification du point B", p_b);
			dialog.setVisible(true);
			updateValues();
		}
	}
}

Le fichier Point.java

package org.wikibooks.fr.swing;

/**
 * Modélisation d'un point.
 * @author fr.wikibooks.org
 */
public class Point
{
	private double x, y;

	public Point()
	{
		x = 0;
		y = 0;
	}

	public double getX()
	{
		return x;
	}

	public double getY()
	{
		return y;
	}

	public void setXY(double x, double y)
	{
		this.x = x;
		this.y = y;
	}

	public 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

package org.wikibooks.fr.swing;

import javax.swing.*;
import javax.swing.border.Border;

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

/**
 * Dialogue pour modifier un point.
 * @author fr.wikibooks.org
 */
public class PointDialog extends JDialog
{
	private Point p;
	private JButton b_ok, b_cancel;
	private JLabel l_x, l_y;
	private JTextField t_x, t_y;

	public 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);

		l_x = new JLabel("Abscisse du point :");
		l_x.setBounds(10, 20, 120, 20);
		c.add(l_x);

		l_y = new JLabel("Ordonnée du point :");
		l_y.setBounds(10, 50, 120, 20);
		c.add(l_y);

		t_x = new JTextField(20);
		t_x.setBounds(140, 20, 100, 20);
		c.add(t_x);

		t_y = new JTextField(20);
		t_y.setBounds(140, 50, 100, 20);
		c.add(t_y);

		Border bord = BorderFactory.createRaisedBevelBorder();
		b_ok = new JButton("OK");
		b_ok.setActionCommand("OK");
		b_ok.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				closeOk();
			}
		});
		b_ok.setBorder(bord);
		b_ok.setBounds(260, 25, 60, 20);
		c.add(b_ok);

		b_cancel = new JButton("Annuler");
		b_cancel.setActionCommand("CANCEL");
		b_cancel.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				close();
			}
		});
		b_cancel.setBorder(bord);
		b_cancel.setBounds(260, 50, 60, 20);
		c.add(b_cancel);

		updatePoint();
	}

	private void updatePoint()
	{
		t_x.setText(String.valueOf(p.getX()));
		t_y.setText(String.valueOf(p.getY()));
	}

	private void closeOk()
	{
		double x, y;
		boolean ok = true;

		try
		{
			x = Double.parseDouble(t_x.getText());
		}
		catch (NumberFormatException ex)
		{
			x = p.getX();
			t_x.setText(String.valueOf(x));
			ok = false;
		}

		try
		{
			y = Double.parseDouble(t_y.getText());
		}
		catch (NumberFormatException ex)
		{
			y = p.getY();
			t_y.setText(String.valueOf(y));
			ok = false;
		}

		if (ok)
		{
			p.setXY(x, y);
			dispose();
		}
	}

	private void close()
	{
		setVisible(false);
	}
}