meryalc
12-03-2012, 16:34
Ciao a tutti ! Ho un problema con la visualizzazione di un'immagine presa da file, in un jPanel; ho creato una classe principale, separando le classi JPanel e JFrame.
package provaimg;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class ProvaIMG {
public ProvaIMG(String nomeFile){
super();
}
private static int POSIZIONE_ALTEZZA = 0;
private static int POSIZIONE_ALTEZZA_OFFSET = 30;
static int x,y;
public static void main(String[] args) {
try {//finestra seleziona file
JFileChooser fileChooser = new JFileChooser("./");
fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
BufferedImage immagine = ImageIO.read(file);
x = immagine.getHeight(); //altezza M righe
y = immagine.getWidth(); //larghezza N colonne
//JOptionPane.showMessageDialog(null, "height: "+x+ " width: "+y); //verifica acquisizione dimensione
apriFrame(immagine, false);
} catch(IOException e) {
System.out.println(e.getMessage());
} catch(Exception e){
JOptionPane.showMessageDialog(null, " Errore: formato non supportato! "); //es qualsiasi altro formato
}
}// end main
public static void apriFrame(final BufferedImage immagine, boolean startCustomJFrame1View){
JPanel pannello = new JPanel(immagine);
JFrame1 frame = new JFrame1(immagine, pannello);
frame.getContentPane().add(pannello, BorderLayout.CENTER);
frame.repaint();
frame.pack();
frame.setVisible(true);
if(startCustomJFrame1View){
frame.setLocation(0, POSIZIONE_ALTEZZA);
if(Toolkit.getDefaultToolkit().getScreenSize().height - frame.getSize().height > POSIZIONE_ALTEZZA){
POSIZIONE_ALTEZZA = POSIZIONE_ALTEZZA + POSIZIONE_ALTEZZA_OFFSET;
}else{
POSIZIONE_ALTEZZA = 0;
}
}
if(!startCustomJFrame1View){
frame.setLocationRelativeTo(null);
}
}
}// end class
La classe JPanel:
package provaimg;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class JPanel extends javax.swing.JPanel {
private BufferedImage immagineBuf;
public JPanel(BufferedImage immagineBuf) {
this.immagineBuf = immagineBuf;
initComponents();
this.setPreferredSize(new Dimension(immagineBuf.getWidth(), immagineBuf.getHeight()) );
}
@SuppressWarnings("unchecked")
@Override
public void paint(Graphics g) {
super.paintComponent(g);
System.out.print("CIAO");
g.clearRect(0, 0, this.getWidth(), this.getHeight());
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(immagineBuf, null, 0, 0);
g2.dispose();
}
}
La classe JFrame:
public class JFrame1 extends javax.swing.JFrame {
private BufferedImage bufImmagine;
private JPanel customJPanel;
public JFrame1( BufferedImage bufImmagine, JPanel customJPanel ) {
super();
this.bufImmagine = bufImmagine;
this.customJPanel = customJPanel;
initComponents();
this.setTitle("Finestra principale");
}
Quello che riesco a vedere è una finestra grigia (con una jtoolbar che ho messo sul jframem utilizzando lo strumento apposito di netbeans), ma non riesco a vedere l'immagine
p.s. jpanel della stessa dimensione del jframe.
Qualcuno sà dirmi cosa sbaglio? o cosa mi manca?
Grazie in anticipo :)
package provaimg;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class ProvaIMG {
public ProvaIMG(String nomeFile){
super();
}
private static int POSIZIONE_ALTEZZA = 0;
private static int POSIZIONE_ALTEZZA_OFFSET = 30;
static int x,y;
public static void main(String[] args) {
try {//finestra seleziona file
JFileChooser fileChooser = new JFileChooser("./");
fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
BufferedImage immagine = ImageIO.read(file);
x = immagine.getHeight(); //altezza M righe
y = immagine.getWidth(); //larghezza N colonne
//JOptionPane.showMessageDialog(null, "height: "+x+ " width: "+y); //verifica acquisizione dimensione
apriFrame(immagine, false);
} catch(IOException e) {
System.out.println(e.getMessage());
} catch(Exception e){
JOptionPane.showMessageDialog(null, " Errore: formato non supportato! "); //es qualsiasi altro formato
}
}// end main
public static void apriFrame(final BufferedImage immagine, boolean startCustomJFrame1View){
JPanel pannello = new JPanel(immagine);
JFrame1 frame = new JFrame1(immagine, pannello);
frame.getContentPane().add(pannello, BorderLayout.CENTER);
frame.repaint();
frame.pack();
frame.setVisible(true);
if(startCustomJFrame1View){
frame.setLocation(0, POSIZIONE_ALTEZZA);
if(Toolkit.getDefaultToolkit().getScreenSize().height - frame.getSize().height > POSIZIONE_ALTEZZA){
POSIZIONE_ALTEZZA = POSIZIONE_ALTEZZA + POSIZIONE_ALTEZZA_OFFSET;
}else{
POSIZIONE_ALTEZZA = 0;
}
}
if(!startCustomJFrame1View){
frame.setLocationRelativeTo(null);
}
}
}// end class
La classe JPanel:
package provaimg;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class JPanel extends javax.swing.JPanel {
private BufferedImage immagineBuf;
public JPanel(BufferedImage immagineBuf) {
this.immagineBuf = immagineBuf;
initComponents();
this.setPreferredSize(new Dimension(immagineBuf.getWidth(), immagineBuf.getHeight()) );
}
@SuppressWarnings("unchecked")
@Override
public void paint(Graphics g) {
super.paintComponent(g);
System.out.print("CIAO");
g.clearRect(0, 0, this.getWidth(), this.getHeight());
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(immagineBuf, null, 0, 0);
g2.dispose();
}
}
La classe JFrame:
public class JFrame1 extends javax.swing.JFrame {
private BufferedImage bufImmagine;
private JPanel customJPanel;
public JFrame1( BufferedImage bufImmagine, JPanel customJPanel ) {
super();
this.bufImmagine = bufImmagine;
this.customJPanel = customJPanel;
initComponents();
this.setTitle("Finestra principale");
}
Quello che riesco a vedere è una finestra grigia (con una jtoolbar che ho messo sul jframem utilizzando lo strumento apposito di netbeans), ma non riesco a vedere l'immagine
p.s. jpanel della stessa dimensione del jframe.
Qualcuno sà dirmi cosa sbaglio? o cosa mi manca?
Grazie in anticipo :)