Salut,
C'est mon jour de bonté!
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* panneau debile
*
* @author jojolemariole
*/
public class PanneauDebile
extends JPanel {
/**
* serial qui sert a rien
*/
private static final long serialVersionUID = 2283111163155978507L;
private JButton bouton1;
private JButton bouton2;
public PanneauDebile() {
bouton1 = new JButton("Bouton 1");
bouton2 = new JButton("Bouton 2");
add(bouton1);
add(bouton2);
}
public void paintComponent(Graphics graphics) {
/*
* calcul des coordonnees du trait en fonction de la position des
* boutons
*/
Rectangle rectangleBouton1 = bouton1.getBounds();
Rectangle rectangleBouton2 = bouton2.getBounds();
int xBouton1 = rectangleBouton1.x;
int xBouton2 = rectangleBouton2.x;
int x1, x2, y1, y2;
if (xBouton1 < xBouton2) {
x1 = xBouton1 + rectangleBouton1.width;
x2 = xBouton2;
y1 = rectangleBouton1.y + rectangleBouton1.height / 2;
y2 = rectangleBouton2.y + rectangleBouton2.height / 2;
} else {
x1 = xBouton2 + rectangleBouton2.width;
x2 = xBouton1;
y1 = rectangleBouton2.y + rectangleBouton2.height / 2;
y2 = rectangleBouton1.y + rectangleBouton1.height / 2;
}
/*
* on dessine le fond du panneau
*/
graphics.setColor(getBackground());
graphics.fillRect(0, 0, getWidth(), getHeight());
/*
* on dessine les deux boutons
*/
bouton1.paintComponents(graphics);
bouton2.paintComponents(graphics);
/*
* on dessine le trait
*/
graphics.setColor(Color.BLACK);
graphics.drawLine(x1, y1, x2, y2);
}
/**
* lance un test du panneau debile dans une fenetre a la con
*
* @param args inutiles
*/
public static void main(String[] args) {
JFrame fenetreALaCon = new JFrame("Je suis une fenêtre à la con.");
fenetreALaCon.setSize(640, 480);
fenetreALaCon.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PanneauDebile panneauDebile = new PanneauDebile();
fenetreALaCon.add(panneauDebile);
fenetreALaCon.setVisible(true);
}
}