1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| package dock;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.*;
/**
*
* @author Ivelios
*/
public class Accueil extends JFrame implements ActionListener, MouseMotionListener, MouseListener{
private GridBagLayout layout = new GridBagLayout();
private GridBagConstraints gbc= new GridBagConstraints();
private JPanel panel;
private Point mousePos;//Position de la souris
private ArrayList<JButton> listButton= new ArrayList<JButton>();//Liste des boutons
private JLabel infoBulle;
/**
* Constructeur
*/
public Accueil(){
super("Accueil");
this.setSize(350,520);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseMotionListener(this);
this.addMouseListener(this);
panel = new JPanel(layout){public void paintComponent(Graphics g){g.drawImage(new ImageIcon("./data/img/menu/fond.png").getImage(), 0, 0, null);}};
JPanel pane = new JPanel();
pane.setOpaque(false);
infoBulle = new JLabel("");
infoBulle.setFont(new Font("Dialog",Font.BOLD,35));
infoBulle.setForeground(new Color(54,95,145));
JButton jouer = new JButton(new ImageIcon(scale(new ImageIcon("./data/img/menu/jouer.png").getImage(),50,50)));
JButton option = new JButton(new ImageIcon(scale(new ImageIcon("./data/img/menu/option.png").getImage(),50,50)));
JButton credit = new JButton(new ImageIcon(scale(new ImageIcon("./data/img/menu/credit.png").getImage(),50,50)));
JButton quitter = new JButton(new ImageIcon(scale(new ImageIcon("./data/img/menu/quitter.png").getImage(),50,50)));
paramBoutton("jouer",jouer);
paramBoutton("option",option);
paramBoutton("credit",credit);
paramBoutton("quitter",quitter);
this.ajouterComposant(pane, 0, 0, 1, 5, new Insets(10,10,10,10), 1.0f, 1.0f, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
this.ajouterComposant(infoBulle, 1, 0, 1, 1, new Insets(30,20,20,30), 0.0f, 0.0f, GridBagConstraints.NONE, GridBagConstraints.LINE_END);
this.ajouterComposant(jouer, 1, 1, 1, 1, new Insets(2,10,2,10), 0.0f, 0.6f, GridBagConstraints.NONE, GridBagConstraints.LAST_LINE_END);
this.ajouterComposant(option, 1, 2, 1, 1, new Insets(2,10,2,10), 0.0f, 0.0f, GridBagConstraints.NONE, GridBagConstraints.LINE_END);
this.ajouterComposant(credit, 1, 3, 1, 1, new Insets(2,10,2,10), 0.0f, 0.0f, GridBagConstraints.NONE, GridBagConstraints.LINE_END);
this.ajouterComposant(quitter, 1, 4, 1, 1, new Insets(2,10,2,10), 0.0f,0.4f, GridBagConstraints.NONE, GridBagConstraints.FIRST_LINE_END);
this.setContentPane(panel);
this.setVisible(true);
}
/**
* Paramètre un boutton
* @param str : ActionCommand
* @param jbutton : le JButton
* @return
*/
private void paramBoutton(String str,JButton jbutton) {
listButton.add(jbutton);
jbutton.setFocusPainted(false);
jbutton.setBorderPainted(false);
jbutton.setContentAreaFilled(false);
jbutton.setActionCommand(str);
jbutton.addActionListener(this);
jbutton.setMargin(new Insets(0, 0, 0, 0));
jbutton.addMouseMotionListener(this);
jbutton.addMouseListener(this);
jbutton.setMinimumSize(new Dimension(50,50));
jbutton.setPreferredSize(new Dimension(50,50));
jbutton.setMaximumSize(new Dimension(120,120));
}
/**
* Ajoute un composant à la fenetre avec le GridBagLayout
* @param composant
* @param colonne
* @param ligne
* @param largeur
* @param hauteur
* @param ins
* @param weightx
* @param weighty
* @param fill
* @param anchor
*/
private void ajouterComposant(Component composant, int colonne, int ligne, int largeur, int hauteur,Insets ins, float weightx,float weighty,int fill, int anchor){
gbc.gridx = colonne;
gbc.gridy = ligne;
gbc.gridwidth = largeur;
gbc.gridheight = hauteur;
gbc.insets = ins;
gbc.fill =fill;
gbc.anchor = anchor;
gbc.weightx=weightx;
gbc.weighty=weighty;
layout.setConstraints(composant, gbc);
panel.add(composant);
}
/**
* Gere les evenements des boutons
* @param e
*/
public void actionPerformed(ActionEvent e) {
String cmd= e.getActionCommand();
if(cmd.equals("jouer")){
this.dispose();
//A compléter
}else if(cmd.equals("option")){
//A compléter
}else if(cmd.equals("credit")){
//A compléter
}else if(cmd.equals("quitter")){
int rep=JOptionPane.showConfirmDialog(null,"Voulez-vous vraiment quitter","Tu fais une bêtise !",JOptionPane.YES_NO_OPTION);
if(rep == 0) System.exit(0);
}
}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
mousePos = e.getSource() instanceof JButton ? SwingUtilities.convertPoint((Component)e.getSource(), new Point(e.getPoint().x+2,e.getPoint().y+30), getContentPane()) : e.getPoint();
this.redimensionnementBoutton();
panel.revalidate();
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {
mousePos = e.getSource() instanceof JButton ? SwingUtilities.convertPoint((Component)e.getSource(), new Point(e.getPoint().x+2,e.getPoint().y+30), getContentPane()) : e.getPoint();
//Si le curseur est en dehors de la fenêtre
if(mousePos.x>this.getWidth() || mousePos.y>this.getHeight()){
mousePos = new Point(-10000,-10000);
this.redimensionnementBoutton();
panel.revalidate();
}
}
/**
* Redimmensionne le boutton
*/
private void redimensionnementBoutton(){
if(mousePos!=null){
for(int i = 0;i<listButton.size();i++){
JButton bCourant = listButton.get(i);
String img ="./data/img/menu/"+ bCourant.getActionCommand()+".png";
int posX = bCourant.getX()+10+(bCourant.getPreferredSize().height)-50;
int posY = bCourant.getY()+2+(bCourant.getPreferredSize().width/2)+20;
int dist = (int) Math.sqrt(((mousePos.x - posX)*(mousePos.x - posX)) + ((mousePos.y - posY)*(mousePos.y - posY)));
if(dist<100){
bCourant.setPreferredSize(new Dimension(100-(dist/2),100-(dist/2)));
bCourant.setIcon(new ImageIcon(scale(new ImageIcon(img).getImage(),100-(dist/2),100-(dist/2))));
}else{
bCourant.setPreferredSize(new Dimension(50,50));
bCourant.setIcon(new ImageIcon(scale(new ImageIcon(img).getImage(),50,50)));
}
}
}
}
/**
* Redimmensionne une image
* @param source : image
* @param width : longeur
* @param height : hauteur
* @return l'image redimmensionne
* CR : développez.com
*/
private static Image scale(Image source, int width, int height) {
BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = buf.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(source, 0, 0, width, height, null);
g.dispose();
return buf;
}
/**
* Lanceur, paramètre le Look and Feel
* @param args
* @throws Exception
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Accueil();
}
});
}
} |
Partager