I am trying to create a GUI that will coexist with several images.
There is one background image that will take up the most room. On top of the background image several other images will be displayed in varying locations. These locations will be updated every 5 seconds or so, so speed is not a huge factor.
So all I need is the ability to display or remove images from display at the coordinates of my choosing upon each update.
I am using netbeans to put it all together.
Here is the most reasonable code I have come across to get the job done so far, but I don't know how to put it into practice, or whether it can accomplish what I need.
A short simple explanation of what a JPanel is would also be helpful.
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);
}
}
And here is the code from the main class that I have so far. I can easily add code to determine whether or not given images should be displayed, and where they need to be, but I don't know where to put what.
public class ClientWindow extends javax.swing.JFrame {
/** Creates new form ClientWindow */
public ClientWindow() {
initComponents();
imagePanel1.setImage("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\TableAndChairs.png");
}
/** 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();
imagePanel3 = new Pokertable.ImagePanel();
jPanel1 = new javax.swing.JPanel();
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);
imagePanel1.add(imagePanel3);
imagePanel1.add(jPanel1);
getContentPane().add(imagePanel1);
imagePanel1.setBounds(0, 0, 520, 370);
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 Pokertable.ImagePanel imagePanel3;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
For simplicity, suppose that I want to have the TableAndChairs.png as a stable background image, and the Button.png image (the D with a circle around it) to move to a new location every so often.