Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2011
    Messaggi
    2

    [JAVA] Problema con JPanel

    Ciao a tutti ragazzi, vorrei gentilmente il vostro aiuto per risolvere una cosa per cui proprio non riesco a venirne a capo!
    allora,ho creato la classe:

    public class ImagePanel extends JPanel {
    Image im;
    public ImagePanel(Image img){
    im=img;
    }
    @Override
    public void paintComponent(Graphics g){
    g.drawImage(im, 5, 5, this);
    }

    }

    come faccio a trasferire un'immagine presente in un ImagePanel in un altro ImagePanel, dopo un click del mouse? se creo i metodi getImage() e setImage() e li utilizzo mi lancia una nullpointerexception...e volendo utilizzare semplici JPanel questi non includono metodi per poter prendere il contenuto presente..

    grazie e spero possiate aiutarmi

  2. #2
    Qual è il codice? Mettilo insieme a tutto il traceback dell'eccezione
    Neb

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2011
    Messaggi
    2
    codice:

    import java.io.IOException;



    public class ScambioCarta extends javax.swing.JFrame {

    /** Creates new form ScambioCarta */
    public ScambioCarta() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    try{
    javax.swing.JPanel jPanel1 = new ImagePanel(Immagine.getIm());
    javax.swing.JPanel jPanel2 = new ImagePanel(null);

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    }catch(IOException ex){
    System.out.print("errore");
    }
    jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
    jPanel1MouseClicked(evt);
    }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGap(0, 204, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGap(0, 317, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGap(0, 218, Short.MAX_VALUE)
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGap(0, 321, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(43, 43, 43)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(47, 47, 47)
    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap())
    );

    pack();
    }// </editor-fold>

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
    jPanel2.setIm(jPanel1.getIm());

    }


    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new ScambioCarta().setVisible(true);
    }
    });
    }

    private ImagePanel jPanel2;
    private ImagePanel jPanel1;


    }




    eccezioni:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at tressette.ScambioCarta.initComponents(ScambioCarta .java:48)
    at tressette.ScambioCarta.<init>(ScambioCarta.java:27 )
    at tressette.ScambioCarta$2.run(ScambioCarta.java:111 )
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:642)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 612)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)


    metodo per prendere l'immagine:

    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;



    public class Immagine {
    static String url ="src/tressette/Re.jpg";
    public static Image getIm() throws IOException{
    return ImageIO.read(new File(url));
    }
    public static String getUrl(){
    return url;
    }

    }

  4. #4
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,477

    Moderazione

    Esiste un apposito sottoforum per Java.
    Sposto la discussione.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  5. #5
    a me dà un errore su questa:

    @Override
    public void paintComponent(Graphics g){
    g.drawImage(im, 5, 5, this); --> ERRORE
    }

    mi dice che nella classe Graphics non esiste il metodo drawImage che riceve (Image,int,int,ImagePanel).

    Infatti invece del ImagePanel ci dovrebbe essere un ImageObserver...
    Neb

  6. #6
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,326

    Moderazione

    C'è anche un Regolamento interno dove è scritto che il posting del codice deve essere fatto con l'indentazione e con l'ausilio degli appositi tag CODE, altrimenti, come si è visto, si perde indentazione e formattazione, rendendo il codice di difficile lettura.


    In questa discussione, tutto il codice postato è completamente privo di indentazione, quindi è perfettamente inutile che io aggiunga i tag mancanti... rimarrà illeggibile.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.