views:

249

answers:

1

I have a GUI written using netbeans with a few simple components in place. I want to be able to draw images (any file type, whatever's easiest) alongside the GUI components inside the JFrame.

Don't have to resize them, just draw them as they are, at the x and y location of my choosing. There will be several images being drawn at each update, some will become hidden and others displayed. The updating will only occur every 5 seconds or so, so it's being fast isn't really an issue.

If it would be possible to attach events to the images' being clicked, that would be nice, but not essential.

This is a supremely simple task that I have as yet been unable to get a simple answer to.

How do I go about doing this?

Thanks

package Pokertable;

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

/* * ClientWindow.java * * Created on Sep 12, 2009, 9:10:48 PM */

/** * * @author Robert */ public class ClientWindow extends javax.swing.JFrame {

/** Creates new form ClientWindow */
public ClientWindow() {
    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() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTextField1 = new javax.swing.JTextField();
    jScrollPane2 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jCheckBox2 = new javax.swing.JCheckBox();
    imagePanel1 = new Pokertable.ImagePanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Not Logged In");
    getContentPane().setLayout(null);

    jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            jTextField1KeyTyped(evt);
        }
    });
    jScrollPane1.setViewportView(jTextField1);

    getContentPane().add(jScrollPane1);
    jScrollPane1.setBounds(0, 540, 170, 22);

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane2.setViewportView(jTextArea1);

    getContentPane().add(jScrollPane2);
    jScrollPane2.setBounds(0, 440, 166, 96);

    jCheckBox2.setText("Sit Out Next Hand");
    jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jCheckBox2ActionPerformed(evt);
        }
    });
    getContentPane().add(jCheckBox2);
    jCheckBox2.setBounds(0, 410, 113, 23);
    getContentPane().add(imagePanel1);
    imagePanel1.setBounds(130, 130, 100, 100);

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

private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
}                                          

private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                     
    // TODO add your handling code here:
}                                    


/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ClientWindow().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
private Pokertable.ImagePanel imagePanel1;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

}

+5  A: 

Create your custom component, and add it to the NetBeans pallete:

public class ImagePanel extends JPanel {

  private Image img;

  public void setImage(String img) {
    setImage(new ImageIcon(img).getImage());
  }

  public void setImage(Image img) {
    int width = this.getWidth();
    int height = (int) (((double) img.getHeight(null) / img.getWidth(null)) * width);
    this.img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
  }

  @Override
  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, this);
  }
}

I've included some simple scaling, you can remove it if you like.

A bit of explanation now:

  • extending JPanel makes the component conform to the JavaBean spec. If you want to be able to set the img property via the NetBeans property editor, you should define a simpel getter;
  • otherwise you can manually call setImage() and call repaint(), which will make the image draw
  • the overridden paintComponent method is called on each repaint of the component, and there, you can see, you draw your image in the boundries of the current JPanel (ImagePanel)
Bozho
I don't have much experience with netbeans, so this looks great but I'm not sure how to take advantage of it.How do I add the class as a custom component?How/when do these objects get instantiated?Also the image itself cannot know whether or not it should be made visible, and if so, where it should be drawn upon each update.That information lies in another class, which will be making periodic calls to the graphics to update themselves, with information on what is to be displayed and where.Thanks again for the help...this will be a huge step forward.
Allen
I can't recall now where exactly was the "add to pallete" button, but look for it, it shouldn't be hard to locate. Then you choose your class and add it.You can actually drag the class from the package explorer onto your form and it will appear there.The visibility of the JPanel is controlled in the superclass, JPanel, so don't worry about that. Just try it.
Bozho
Does it matter how big i make the ImagePanel component, relative to the size of the images I want to display?Do i need one of these panels per image displayed?Where in the code above can I call the setImage() method?Also, I can't just drag everything into place ahead of time, as the images will be displayed at varying x and y coordinates throughout.So how do I go about adding them on the fly, at the location of my choosing, and how do i remove an existing one from view in a given update cycle?
Allen
Also, several of the images will end up overlapping a large background image. Will these components be able to overlap one another?
Allen
well, you should read some swing tutorial, or ask those questions seperately when you stubmle upon something that doesn't happen the way you want
Bozho