Results 1 to 16 of 16
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default How to drag and drop the copy of image?

    Hi!

    I'm developing a GUI that allows a user to create pictures using a library of small images. All small images are grouped in JPanel1, and each of them is stored in JLabel. How to drag the copy of image from JPanel1 onto JPanel2 so that the original image remains in JPanel1?

    I have a code that implements just moving of an image without copying.

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

    jLabel3.setLocation(MouseInfo.getPointerInfo().get Location().x - jPanel2.getX(), MouseInfo.getPointerInfo().getLocation().y-jPanel2.getY());

    }

    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    If you have code that 'moves' the image, it should be easy to modify that code to allow copying it. A Move is often a Copy followed by a Delete.
    Can you post your code that does the 'move'?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can't use JComponents such as JLabels or JPanels in more than one location at the same time, but you can use ImageIcons in multiple locations.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    It rather looks like you want the second panel to display an image which shows the present appearance of the first panel. If that's correct, camickr's Screen Image may be useful.
    Screen Image Java Tips Weblog

    db

  5. #5
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Thanks for ur answers!

    I've tried ImageIcons. However, I cannot see the image on the screen. Pliz help:(

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

    ImageIcon img = new ImageIcon("/resources/smile1.png");
    JLabel lb = new JLabel(img);
    lb.setLocation(MouseInfo.getPointerInfo().getLocat ion().x - jPanel2.getX(),MouseInfo.getPointerInfo().getLocat ion().y-jPanel2.getY());
    lb.setVisible(true);

    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by LianaN View Post
    I've tried ImageIcons. However, I cannot see the image on the screen. Pliz help:(

    Java Code:
    private void jLabel3MouseDragged(java.awt.event.MouseEvent evt) {
    
        ImageIcon img = new ImageIcon("/resources/smile1.png");
        JLabel lb = new JLabel(img);
        lb.setLocation(MouseInfo.getPointerInfo().getLocation().x - jPanel2.getX(),MouseInfo.getPointerInfo().getLocation().y-jPanel2.getY());
        lb.setVisible(true);
    
    }
    I don't know where your images are in relationship to your class files, but you may wish to get your images in the same way as previous (if it worked before), and then use a constructor overload for ImageIcon that takes an Image as a parameter, not a String.

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Much luck!

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    How are you laying out the GUI? Do you have a layout manager?
    Where do you add the JLabel: lb to a container?

    Is the paintComponent method for the JLabel: lb being called?
    Override it and add a println() to verify.

  8. #8
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Hi!

    sorry, i'm just a bit new to java. therefore, maybe my questions will be a bit stupid. but still...

    I'm using NetBeans IDE 6.9. All images that I have are placed in "resources" folder inside the package.

    >Is the paintComponent method for the JLabel: lb being called?
    >Override it and add a println() to verify.
    No, I don't use this method...

    Now I have just the following code:

    Java Code:
    package my.pack1;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class Class1UI extends javax.swing.JFrame {
    
    ...
    
    private void jLabel3MouseDragged(java.awt.event.MouseEvent evt) {                                     
           ImageIcon img = new ImageIcon("resources/beads_crystal.png");
           int width = img.getIconWidth();
           int height = img.getIconHeight();
           JLabel lbl = new JLabel();
           lbl.setPreferredSize(new Dimension(width,height));
           lbl.setIcon(img);
           lbl.setLocation(MouseInfo.getPointerInfo().getLocation().x-jPanel1.getX(),MouseInfo.getPointerInfo().getLocation().y-jPanel1.getY());
        } 
    }
    As I understand I must somewhere put the code shown below. However, I don't know how to connect it with my Image Icon, and where this code must be inserted. Any suggestions?

    Java Code:
    @Override
    public void paintComponent(Graphics g) {
             super.paintComponent(g);
             g.drawImage(((ImageIcon)getIcon()).getImage(), MouseInfo.getPointerInfo().getLocation().x-jPanel2.getX(),MouseInfo.getPointerInfo().getLocation().y-jPanel2.getY(), getWidth(), getHeight(), null);
           }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    How are you laying out the GUI? Do you have a layout manager?
    Where do you add the JLabel: lbl to a container?

    Look at any other component that you are using in your GUI. Do you call an add() method on a container to add that component to the container?

    If your JLabel is not added to a container, it's paintComponent method won't be called when the GUI is repainted.


    The call to the setLocation method: lbl.setLocation(...
    implies that you are not using a layout manager.

  10. #10
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    >How are you laying out the GUI? Do you have a layout manager?
    it seems that i'm not using a layout manager. should i?

    >Where do you add the JLabel: lbl to a container?
    i've just included JLabel: lbl to a container jPanel2: jPanel2.add(lbl).

    >Look at any other component that you are using in your GUI. Do you call an add() method on a container to add that component to the container?
    When I drag a Label: jLabel4 from Swing Controls onto the JPanel2, then an automatically generated code is (there is no add()):

    Java Code:
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                    .addContainerGap(303, Short.MAX_VALUE)
                    .addComponent(jLabel4)
                    .addGap(205, 205, 205))

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default

    it seems that i'm not using a layout manager. should i?
    If you don't set the layout to null, there is a default one.

    automatically generated code
    Oh Oh. Are you asking about how to use your IDE? That's a different problem than I was trying to solve.

  12. #12
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    I'm trying to learn some basics about adding images to the jframe... So, I found a source code posted here: Display a JPG Image in Jpanel. But the problem is that I can see simply the jframe colored with CYAN, no picture (of course, I've created the folder "resources" and changed the image address to resources/smile1.png). Where is the problem??!!

  13. #13
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    sorry for misunderstanding:)

  14. #14
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    the last post was addressed to Norm:)

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    My guess is that you're still not looking for the image in the right location but without compilable code and more information, about all I can do is guess.

    You should first and foremost learn to create Swing projects by hand by going through the Sun/Oracle tutorials. This way you can learn about the rudiments of Swing, and if you run into a problem, you can post a reasonable amount of compilable code to this forum and let us compile it, run it and inspect it.

  16. #16
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

Similar Threads

  1. help with drag and drop on JList
    By ysl530 in forum AWT / Swing
    Replies: 4
    Last Post: 01-22-2011, 04:58 PM
  2. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 01:01 PM
  3. Drag and Drop
    By carderne in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 09:18 AM
  4. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 03:04 PM
  5. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 02:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •