|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 23
|
Java problema visualizzazione immagine in un jpanel
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 |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Aug 2011
Messaggi: 672
|
mhhh
Ma Si può creare un Pannello partendo da una bufferedImage? Sicuro? Che editor usi? Eclipse? Codice:
JPanel pannello = new JPanel(immagine);
__________________
Trattative positive: Ares17, TerrorSwing, HaraN.BanjO, Stevejedi, rtype, lucariello86, Leland Gaunt, antotuning, goudkamp Vendo Ricambi Acer Extensa 5235 -- Ricambi HP dv9000 -- Ricambi Hp dv6000 -- Ventole, Cavi, Adattatori, Schede di Rete, Ecc |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 23
|
Uso Netbeans, si di questo sono sicura..il problema è che alla fine dovrei ottenere una finestra con i due bottoni "Apri Immagine" e "Seleziona Punti"
con l'immagine aperta, invece vedo solo i jbutton, ma non l'immagine :-// |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Aug 2011
Messaggi: 672
|
__________________
Trattative positive: Ares17, TerrorSwing, HaraN.BanjO, Stevejedi, rtype, lucariello86, Leland Gaunt, antotuning, goudkamp Vendo Ricambi Acer Extensa 5235 -- Ricambi HP dv9000 -- Ricambi Hp dv6000 -- Ventole, Cavi, Adattatori, Schede di Rete, Ecc |
|
|
|
|
|
#5 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 23
|
La classe JPanel è quella che mi sono costruita appositamente, non è questo il problema :-//
L'ho anche copiata..quelle che ho messo sono le tre classi che ho fatto. |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Aug 2011
Messaggi: 672
|
Ah scusa non me n'ero accorto, diciamo che non è molto convenzionale
Invece initComponents(); cosa fa?..
__________________
Trattative positive: Ares17, TerrorSwing, HaraN.BanjO, Stevejedi, rtype, lucariello86, Leland Gaunt, antotuning, goudkamp Vendo Ricambi Acer Extensa 5235 -- Ricambi HP dv9000 -- Ricambi Hp dv6000 -- Ventole, Cavi, Adattatori, Schede di Rete, Ecc |
|
|
|
|
|
#7 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 23
|
ho risolto
(cmq initComponents() è un metodo che "ottengo" in automatico, dato che per creare il jframe e il jpanel, ora solo il jpanel, ho utilizzato gli strumenti di netbeans ) Ultima modifica di meryalc : 15-03-2012 alle 18:15. |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Aug 2011
Messaggi: 672
|
ahok, magari era proprio quello a dare problemi !
__________________
Trattative positive: Ares17, TerrorSwing, HaraN.BanjO, Stevejedi, rtype, lucariello86, Leland Gaunt, antotuning, goudkamp Vendo Ricambi Acer Extensa 5235 -- Ricambi HP dv9000 -- Ricambi Hp dv6000 -- Ventole, Cavi, Adattatori, Schede di Rete, Ecc |
|
|
|
|
|
#9 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 23
|
Grazie cmq
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:54.



















