Results 1 to 16 of 16
- 08-22-2010, 01:33 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
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!
- 08-22-2010, 02:09 PM #2
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'?
-
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.
- 08-22-2010, 03:03 PM #4
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
- 08-22-2010, 03:35 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
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);
}
-
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:
Much luck!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 08-22-2010, 03:55 PM #7
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.
- 08-22-2010, 06:51 PM #8
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
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:
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: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()); } }
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); }
- 08-22-2010, 07:02 PM #9
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.
- 08-22-2010, 07:32 PM #10
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
>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))
- 08-22-2010, 07:43 PM #11
If you don't set the layout to null, there is a default one.it seems that i'm not using a layout manager. should i?
Oh Oh. Are you asking about how to use your IDE? That's a different problem than I was trying to solve.automatically generated code
- 08-23-2010, 12:44 PM #12
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
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??!!
- 08-23-2010, 12:45 PM #13
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
sorry for misunderstanding:)
- 08-23-2010, 12:46 PM #14
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
the last post was addressed to Norm:)
-
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.
- 08-26-2010, 07:31 PM #16
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
Everything works perfectly now! Who is interested may read this topic: http://www.java-forums.org/newreply....ote=1&p=139865
Similar Threads
-
help with drag and drop on JList
By ysl530 in forum AWT / SwingReplies: 4Last Post: 01-22-2011, 04:58 PM -
drag and drop - disable intra component drop
By tomba in forum AWT / SwingReplies: 4Last Post: 12-09-2009, 01:01 PM -
Drag and Drop
By carderne in forum New To JavaReplies: 0Last Post: 08-31-2009, 09:18 AM -
Drag and drop
By thayalan in forum AWT / SwingReplies: 1Last Post: 02-16-2009, 03:04 PM -
Drag and drop
By abhivenugopal in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-30-2008, 02:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks