codice:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import javax.swing.SwingUtilities;
import java.io.*;
import java.util.*;
public class frame1 extends JFrame {
private JPanel panCentro=new JPanel();
private JPanel pannello1=new JPanel();
private JPanel pannello2=new JPanel();
private JButton bChiude = new JButton("Chiude");
private JButton bCaricaFoto = new JButton("carica foto");
private ImageIcon mia_foto = new ImageIcon();
private String SentieroNomeFile="";
private String NomeFile="";
private String Sentiero="";
public frame1() {
//--- inizio eventi ---
bCaricaFoto.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
javax.swing.filechooser.FileFilter filtro = new javax.swing.filechooser.FileNameExtensionFilter("immagini", "bmp", "jpg", "jpeg", "gif");
JFileChooser filechoosed = new JFileChooser();
//filechoosed.setCurrentDirectory(new File("c:\\"));
//--- inizio impostazione dei filtri ---
filechoosed.setAcceptAllFileFilterUsed(false);
filechoosed.addChoosableFileFilter(filtro);
//--- fine impostazione dei filtri ---
int result = filechoosed.showOpenDialog(frame1.this);
if (result == JFileChooser.APPROVE_OPTION) {
File f1 = filechoosed.getSelectedFile();
NomeFile=f1.getName();
SentieroNomeFile = f1.getAbsolutePath();
pannello1.removeAll();
ImageIcon mia_foto = new ImageIcon(SentieroNomeFile);
CaricaLaFoto FotoCaricata = new CaricaLaFoto();
pannello1.add(FotoCaricata);
}else{
String msg="operazione annullata";
JOptionPane.showMessageDialog(null, msg, "file di tipo immagine", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//--- fine eventi ---
//--- inizio pannello1 ---
Disegna mioDisegno = new Disegna();
pannello1.setLayout(new BoxLayout(pannello1, BoxLayout.Y_AXIS));
pannello1.setPreferredSize(new Dimension(300, 290));
pannello1.add(mioDisegno);
//--- fine pannello1 ---
//--- inizio pannello2 ---
pannello2.setLayout(new GridBagLayout());
GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.HORIZONTAL;
//Insets(up,left,bottom,right);
c2.insets = new Insets(3,3,3,3);
c2.gridy = 0;
c2.gridx = 0; pannello2.add(bChiude, c2);
c2.gridx = 2; pannello2.add(bCaricaFoto, c2);
//--- fine pannello2 ---
//--- inizio panCentro ---
panCentro.setLayout(new GridBagLayout());
GridBagConstraints c99 = new GridBagConstraints();
c99.fill = GridBagConstraints.VERTICAL;
c99.anchor = GridBagConstraints.LINE_START;
c99.insets = new Insets(0,0,0,0);
c99.gridx = 0;
int coy=-1;
coy++; c99.gridy = coy; panCentro.add(pannello1, c99);
coy++; c99.gridy = coy; panCentro.add(pannello2, c99);
//--- fine panCentro ---
JScrollPane barre1 = new JScrollPane(panCentro);
barre1.setPreferredSize(new Dimension(320, 330));
Container mioc = getContentPane();
mioc.setLayout(new BoxLayout(mioc, BoxLayout.Y_AXIS));
mioc.add(barre1);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle("frame1");
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new frame1();
}
});
}
class CaricaLaFoto extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (SentieroNomeFile.equals("")) {
g.setColor(Color.white);
g.fillRect(0,0,300,300);
g.setColor(Color.black);
g.drawRect(50,50,128,128);
}else{
//--- inizio mettere la foto ne grafico ---
g.setColor(Color.white);
g.fillRect(0,0,300,300);
g.drawImage(mia_foto, 50, 50, 128, 128, this); //qui fa errore //--- inizio mettere la foto ne grafico ---
}
}
}
}
class Disegna extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0,0,300,300);
g.setColor(Color.black);
g.drawRect(50,50,128,128);
}
}