Simple Java Display Images
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 ja开发者_C百科vax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
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 callrepaint()
, 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 currentJPanel
(ImagePanel
)
精彩评论