|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Dec 2010
Messaggi: 85
|
[JAVA]Jfilechooser non ridimensionabile con antemprima
Salve a tutti,
dovrei realizzare una progetto in java 2d che prevede il caricamento immagini e la successiva modifica di queste applicando filtri. Vorrei far visualizzare prima dell'apertura file l'anteprima dell'immagine, per cui ho creato una classe che estende JPanel ed implements PropertyChangeListener, i cui codice è il seguente: Codice:
public PannelloMiniaturaJFileChooser(Vista vista) {
this.vista = vista;
minWidth = 245;
this.setPreferredSize(new Dimension(150, 100));
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createLineBorder(Color.GREEN));
this.setBorder(BorderFactory.createTitledBorder("Anteprima"));
initComponents();
}
public void propertyChange(PropertyChangeEvent evt) {
Icon icon = null;
String propertyName = evt.getPropertyName();
if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(propertyName)) {
File newFile = (File) evt.getNewValue();
if (newFile != null) {
String path = newFile.getAbsolutePath();
logger.info("path --> " + path);
if (path.endsWith(".JPG") || path.endsWith(".jpg") || path.endsWith(".gif") || path.endsWith(".png")
|| path.endsWith(".bmp") || path.endsWith(".jpeg")) {
try {
BufferedImage img = ImageIO.read(newFile);
float width = img.getWidth();
float height = img.getHeight();
float scale = height / width;
width = minWidth;
height = (width * scale);
icon = new ImageIcon(img.getScaledInstance(Math.max(1, (int) width),
Math.max(1, (int) height), Image.SCALE_SMOOTH));
} catch (IOException e) {
//Some error reported!
}
}
}
labelMiniaturaFoto.setIcon(icon);
this.repaint();
}
}
Codice:
private String acquisisciFile() {
Vista vista = this.controllo.getVista();
JFileChooser fileChooser = vista.getJfileChooser();
PannelloMiniaturaJFileChooser pannelloMfc = new PannelloMiniaturaJFileChooser(vista);
pannelloMfc.getLabelMiniaturaFoto().setHorizontalAlignment(SwingConstants.CENTER);
pannelloMfc.add(pannelloMfc.getLabelMiniaturaFoto(), BorderLayout.CENTER);
fileChooser.setAccessory(pannelloMfc);
fileChooser.addPropertyChangeListener(pannelloMfc);
fileChooser.setApproveButtonText("Apri Foto");
fileChooser.setDialogTitle("Selezionare un file");
int codice = fileChooser.showOpenDialog(vista);
if (codice == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
percorso = file.getPath();
image = new Immagine(file);
Modello modello = this.controllo.getModello();
modello.putBean(Costanti.IMMAGINE, image);
return percorso;
//return file.toString();
} else if (codice == JFileChooser.CANCEL_OPTION) {
logger.info("Comando apri annullato");
}
//Reset the file chooser for the next time it's shown.
fileChooser.setSelectedFile(null);
return null;
}
Dove sbaglio? Inoltre vorrei che le dimensioni del filechooser oltre una certa dimensione non devono essere modificate, come posso fare anche questo? Grazie per la disponibilità. Saluti |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:48.



















