Results 1 to 6 of 6

Thread: Inserting an image on mouse click

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Inserting an image on mouse click

    Hello,
    I use the "canvas" to create a program similar to "PacketTracer". I want to insert a picture into drawing "canvas" (or another) every time I click with the mouse. Please give me some hints because it is several days I want a solution. So, any solution will be helpful to me. Thanks in advance for the help.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Inserting an image on mouse click

    Hello,
    Uses the ImageIO class to read each image. When you view the image in a JPanel or a panel, put a MouseListener on it to see where you click. Then retrieves an instance of Graphics from the BufferedImage that you have obtained the very beginning and calls the drawImage method of Graphics class, passing in parameter the coordinates of the click and others. Hope the following information will help you.

  3. #3
    Join Date
    Dec 2009
    Posts
    192

    Re: Inserting an image on mouse click

    Hello,
    I am also trying a similar kind of a program which will insert an image with a mouse click. I have an idea me, please tell me if this is right or not. Is what I can create a new Label (with image as icon) in a panel, each time you click disappointed? is this possible? because I think I can design a code the the same. If it is correct and if i am in the right direction then please confirm me with the same and I will start to code. Thanks in advance.

  4. #4
    Join Date
    Dec 2009
    Posts
    204

    Re: Inserting an image on mouse click

    Hello,
    This is my updated code, please check it and if I am wrong somewhere in the code then please guide me with the correct
    Code:
    Public class frm extends javax.swing.JFrame (
    
        int x, y;
        private int prx;
        private int pry;
        private boolean pressOut;
        / ** Creates new form frame * /
        Public frm () (
            initComponents ();
        )
    
        class nlbl extends javax.swing.JLabel implements MouseListener, MouseMotionListener (
            javax.swing.JLabel jlbl = new javax.swing.JLabel ();
    
            Public nlbl (String pci, String t) (
                jlbl.setIcon (new javax.swing.ImageIcon (getClass (). getResource (pci)));
                jlbl.setText (t);
                jlbl.setHorizontalTextPosition (javax.swing.SwingConstants.CENTER);
                jlbl.setVerticalTextPosition (javax.swing.SwingConstants.BOTTOM);
            )
    
            Public void mouseClicked (MouseEvent e) (
            )
            Public void mousePressed (MouseEvent e) (
            )
            Public void mouseReleased (MouseEvent e) (
            )
            Public void mousePressed (MouseEvent e) (
            )
            Public void mouseExited (MouseEvent e) (
            )
            Public void mouseDragged (MouseEvent e) (
            )
            Public void mouseMoved (MouseEvent e) (
            )
        }

  5. #5
    Join Date
    Nov 2009
    Posts
    356

    Re: Inserting an image on mouse click

    Hello,
    Already, it shows that you did not understand the basics of Swing, stop using the RAD tool for Netbeans, you do not even understand what you do and admit that your code is just the anything that is unnecessarily complicated. Do as I have suggested, take back everything from scratch. When I tell you to add a MouseListener to a component, that means that you must call the above method addMouseListener not that you create a class that implements this interface.

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: Inserting an image on mouse click

    Hello,
    I think you can try the following code
    Code:
    MouseAdapter msadp =new MouseAdapter () (
    
    @ Override
        Public Final void mouseClicked (MouseEvent e) (
            if(SwingUtilities.isLeftMouseButton (e))
                (int e.getX x = (), y = e.getY ();
                 / / Store x and y somewhere else or an attribute to serve thee after
                )
        )
    );
    panel.addMouseMotionListener (msadp);
    panel.addMouseListener (msadp);
    Put it in the class derived from JPanel:
    Code:
    @ Override
    protected void paintComponent (Graphics grp) (
        great. paintComponent (grp);
        grp.drawImage (BufferedImage, 0.0,this);
    }

Similar Threads

  1. Replies: 7
    Last Post: 22-12-2011, 05:56 AM
  2. Replies: 6
    Last Post: 12-06-2010, 05:26 AM
  3. How do I right-click on Mac without mouse?
    By Diabloplayer in forum Operating Systems
    Replies: 4
    Last Post: 29-12-2009, 05:44 AM
  4. Inserting image inside the php code
    By garfield1 in forum Software Development
    Replies: 3
    Last Post: 03-12-2009, 02:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,307,123.23247 seconds with 16 queries