I want to draw an image at X Y position when the user click the mouse
so I'm using an InternalFrame with layout set to NULL , inside a JFrame with Layout free - using netbeans
here is the image class
package myPak_1;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
public class Images extends Component{
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
public Images() {
try {
img = ImageIO.read(new File("C:\\Users\\Ibrahim\\Documents\\NetBeansProjects\\Info306_prev_1\\src\\myPak_1\\state1.png"));
} catch (IOException e) {
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
}
and here is the JFrame
package myPak_1;
public class MainWindow extends javax.swing.JApplet {
/** Initializes the applet MainWindow */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//next is generated code
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jInternalFrame1 = new javax.swing.JInternalFrame();
jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jPanel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jButton1.setText("STATE ");
jButton2.setText("jButton2");
jButton3.setText("jButton3");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)
.addContainerGap(203, Short.MAX_VALUE))
);
jLabel1.setText("jLabel1");
jInternalFrame1.setVisible(true);
jInternalFrame1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jInternalFrame1MouseClicked(evt);
}
});
jInternalFrame1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
jInternalFrame1MouseMoved(evt);
}
});
jInternalFrame1.getContentPane().setLayout(null);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, 605, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(21, 21, 21))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jInternalFrame1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
.addComponent(jLabel1))
);
}// </editor-fold>
private void jInternalFrame1MouseMoved(java.awt.event.MouseEvent evt) {
this.jLabel1.setText(String.format("Mouse is at X:%d Y:%d",evt.getX(),evt.getY()));
}
private void jInternalFrame1MouseClicked(java.awt.event.MouseEvent evt) {
System.out.println(String.format("Mouse is at X:%d Y:%d",evt.getX(),evt.getY()));
Images image= new Images();
this.jInternalFrame1.add(image);
image.setBounds(evt.getX(), evt.getY(),image.getWidth(), image.getHeight());
this.jInternalFrame1.revalidate();
this.jInternalFrame1.repaint();
this.validate();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
private void jInternalFrame1MouseClicked this is the method where i'm trying to do the job
i'm following this tutorial
http://download.oracle.com/javase/tutorial/uiswing/layout/none.html
"If a container holds components whose size is not affected by the container's size or by font, look-and-feel, or language changes, then absolute positioning might make sense. Desktop panes, which contain internal frames, are in this category"