I'm trying to create a program on NetBeans that allows me to load an image into an internal frame. Then I can click on the image and a box pops up, basically just like the one on facebook. Im done with the loading and image part. Now, I"m working on the box.
Here's my code:

private void jInternalFrame1MouseClicked(java.awt.event.MouseEvent evt) {

}

My professor showed us something that goes inside the above code, but I don't know how to code it in. Anyone care to explain? :D Thanks guys! Been so helpful

public void mouseClicked (MouseEvent arg0)
this.x = arg0,getX();
this.y = arg0.getY();

this.repaint();

Recommended Answers

All 15 Replies

Can you explain your problem? You very short pieces of code don't say much.

Well, My problem is that when I load a image using fFileChooser, I want to be able to click my mouse on the image and upon the action of clicking, a box appears. Just like how facebook picture tagging works.

Which of the steps is giving you a problem?
Loading the image from a disk file into a component in your GUI?
Recognizing the click on the component that contains the image?
Defining a "box"? What is a box?
Making the box appear?

I'm done with the loading the image. I've loaded it unto an internal frame. Now, I'm trying to figure out how to generate the box when I click on a specific part on the image. Thanks for replying NormR1 :D I really appreciate your help

What is a "box"?

Like the one on facebook. Like when you click on the image, it generates a rectangle, I'm guessing with drawRect.

Where is the rectangle drawn? In a panel or ???

In the internal frame where the image is loaded into.

What component are you loading the image into? A JPanel?
Can you override its paintComponent method and draw the "box" there?

I'm loading the image on the internal frame. Not sure what you mean to that; I'm a bit new to this. :D

Can you post the code that "loads" the image on the internal frame.
What classes and methods do you use?

I'm using NetBeans btw:

private void LoadImageActionPerformed(java.awt.event.ActionEvent evt) {                                          
int returnVal = jFileChooser1.showOpenDialog( this );        
    if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
            try {
                File file = jFileChooser1.getSelectedFile();
            
             BufferedImage image = ImageIO.read(file);
             
             jLabel1.setIcon(new ImageIcon(image));
            } catch (IOException ex) {
                Logger.getLogger(OpenImage.class.getName()).log(Level.SEVERE, null, ex);
            }
                      
                
    }
}

That code reads in an image from a file and sets the icon for the jLabel1.
What is done with jLabel1?
Where is the internal frame?

Can you override the paintComponent method for jLabel1 and draw the "box" there?

Heres my whole code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OpenImage.java
 *
 * Created on Dec 1, 2011, 9:45:44 PM
 */
/**
 *
 * @author Kevin
 */
public class OpenImage extends javax.swing.JFrame {
    private BufferedImage image;

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

        jFileChooser1 = new javax.swing.JFileChooser();
        jToolBar1 = new javax.swing.JToolBar();
        LoadImage = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        Exit = new javax.swing.JButton();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setRollover(true);

        LoadImage.setText("Load an image");
        LoadImage.setFocusable(false);
        LoadImage.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        LoadImage.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        LoadImage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadImageActionPerformed(evt);
            }
        });
        jToolBar1.add(LoadImage);
        jToolBar1.add(jSeparator1);

        Exit.setText("Exit");
        Exit.setFocusable(false);
        Exit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Exit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        Exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitActionPerformed(evt);
            }
        });
        jToolBar1.add(Exit);

        jInternalFrame1.setVisible(true);
        jInternalFrame1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jInternalFrame1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 443, 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(10, 10, 10)
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 915, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jInternalFrame1)
                .addContainerGap())
        );

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

private void ExitActionPerformed(java.awt.event.ActionEvent evt) {                                     
System.exit(0);
}                                    

private void LoadImageActionPerformed(java.awt.event.ActionEvent evt) {                                          
int returnVal = jFileChooser1.showOpenDialog( this );        
    if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
            try {
                File file = jFileChooser1.getSelectedFile();
            
             BufferedImage image = ImageIO.read(file);
             
             jLabel1.setIcon(new ImageIcon(image));
            } catch (IOException ex) {
                Logger.getLogger(OpenImage.class.getName()).log(Level.SEVERE, null, ex);
            }
                      
                
    }
}                                         

private void jInternalFrame1MouseClicked(java.awt.event.MouseEvent evt) {                                             

}                                            

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OpenImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OpenImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OpenImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OpenImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new OpenImage().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton Exit;
    private javax.swing.JButton LoadImage;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration                   
}

Have you tried overriding the paintComponent method and adding drawing?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.