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 212 213 214 215 216 217 218 219 220 221
|
/**
*
* @authort marc003
*
* */
package javaapplication1;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class NewJScrollPane extends JPanel implements ActionListener {
private static Color myColor=Color.BLACK;
private static String myFichier;
private static String myPattern="_small";
private static File currDir;
private static JLabel myCible;
private static JPanel grBoutons = new JPanel();
private static GroupLayout mainPanelLayout;
public NewJScrollPane(JLabel ImageCible) {
super();
myCible=ImageCible;
JScrollPane myScrollPane = new JScrollPane();
grBoutons.setBackground(Color.BLACK);
myScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
myScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
myScrollPane.setPreferredSize(new Dimension(500,155));
myScrollPane.setViewportView(grBoutons);
Icon IconPath = createImageIcon("images/dossier.gif");
JButton mySelectionChemin = new JButton();
mySelectionChemin.setIcon(IconPath);
mySelectionChemin.setPreferredSize(new Dimension(20,20));
mySelectionChemin.setActionCommand("Dossier");
mySelectionChemin.addActionListener(this);
// Mise en forme de l'objet
BoxLayout layout = new BoxLayout(this, BoxLayout.X_AXIS);
this.setLayout(layout);
mySelectionChemin.setAlignmentX(0.5f);
this.add(mySelectionChemin);
myScrollPane.setAlignmentX(0.5f);
this.add(myScrollPane);
}
protected static ImageIcon createImageIcon(String path) {
URL imgURL = NewJScrollPane.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.out.println("Couldn't find file: " + path+" "+NewJScrollPane.class.getResource("/"));
return null;
}
}
private void chargeIcon(){
int k=0;
int IconMaxHeight=0;
int IconWidth=0;
String[] fileList = currDir.list();
String[] DetailFichier = new String[2];
ImageIcon IconList[] = new ImageIcon[fileList.length];
Pattern p = Pattern.compile(myPattern);
// On vide l'ensemble des elements
grBoutons.removeAll();
for (int i=0; i<fileList.length; i++ )
{
int j=0;
StringTokenizer st = new StringTokenizer(fileList[i], ".");
while (st.hasMoreTokens()) {
DetailFichier[j++]=st.nextToken();
}
if((DetailFichier[1].equalsIgnoreCase("jpg"))||(DetailFichier[1].equalsIgnoreCase("jpeg"))||(DetailFichier[1].equalsIgnoreCase("gif"))||(DetailFichier[1].equalsIgnoreCase("bmp")))
{
Matcher m = p.matcher(DetailFichier[0]);
if(m.find())
{
try {
Image myImage = null;
File Fichier = null;
Fichier = new File(currDir.getAbsolutePath() + "/" + fileList[i]);
myImage = ImageIO.read(Fichier);
IconList[k] = new ImageIcon(myImage);
// Sauvegarde de la hauteur la plus haute
if(IconList[k].getIconHeight()>IconMaxHeight) IconMaxHeight=IconList[k].getIconHeight();
IconWidth+=IconList[k].getIconWidth();
} catch (IOException ex) {
Logger.getLogger(NewJScrollPane.class.getName()).log(Level.SEVERE, null, ex);
}
JButton button=new JButton();
// Gestion de l'appuie de sur les boutons
button.setActionCommand(fileList[i]);
button.addActionListener(this);
button.setOpaque(false);
button.setIcon(IconList[k++]);
button.setMargin(new Insets(0,0,0,0));
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Ajout du bouton
grBoutons.add(button);
}
}
}
// Rafraichissement de l'objet
invalidate();
//this.setSize(IconWidth,IconMaxHeight+30);
validate();
}
private void chargeImage(String nomImage){
try {
Image myImage = null;
File Fichier = null;
int indice=0;
indice = nomImage.indexOf(myPattern);
nomImage=nomImage.substring(0,indice)+nomImage.substring(indice+myPattern.length());
Fichier = new File(currDir.getAbsolutePath() + "/" + nomImage);
myImage = ImageIO.read(Fichier);
// Respecter la taille de l'objet par rapport a la taille de l'image
ImageIcon myIcon = new ImageIcon(myImage);
//System.out.println("Taille de l'image : "+myIcon.getIconHeight()+" par "+myIcon.getIconWidth());
Dimension myDim = new Dimension(myCible.getParent().getPreferredSize());
//System.out.println("Taille de l'objet : "+myDim);
/* On crée une nouvelle image aux bonnes dimensions. */
BufferedImage buf = new BufferedImage(myDim.width,myDim.height, BufferedImage.TYPE_INT_ARGB);
/* On dessine sur le Graphics de l'image bufferisée. */
Graphics2D g = buf.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(myImage, 0, 0, myDim.width, myDim.height, null);
g.dispose();
// Mise a jour de l'image de l'objet
myCible.setIcon(new ImageIcon(buf));
} catch (IOException ex) {
Logger.getLogger(NewJScrollPane.class.getName()).log(Level.SEVERE, null, ex);
}
}
// Pour la gestion des appuies sur les différents boutons
public void actionPerformed(ActionEvent arg0) {
String nomArg = arg0.getActionCommand();
if(nomArg.equals("Dossier")){
JFileChooser myFileChoose = new javax.swing.JFileChooser();
myFileChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = myFileChoose.showOpenDialog(null);
if(returnVal==0) {currDir=myFileChoose.getSelectedFile();
chargeIcon();
}
}
else chargeImage(nomArg);
}
// Pour la gestion des caractéristiques
@Override
public void setBackground(Color choixColor){
myColor = choixColor;
grBoutons.setBackground(myColor);
grBoutons.validate();
}
} |
Partager