Drag two images with a mouse anywhere in the frame

B

bH

Hi All,
I need a java application/applet program that will load two
images in one frame. I should also be able to drag each
image independently with the mouse within that
one frame. Where I drag the images to will be independent
and thus the placement of each image will
not have preset locations available. I already have
a java program where images can be moved with the mouse
to a open, preset locations.

I have searched for demos. I find that there are none.
Perhaps I didn't word the search phrase correctly.
For instance "java demo 'move two images with a mouse.'"

Your help is appreciated.

I have Windows7, using jdk1.6.0_21.

TIA.

bH
 
D

Daniele Futtorovic

Hi All, I need a java application/applet program that will load two
images in one frame. I should also be able to drag each image
independently with the mouse within that one frame. Where I drag the
images to will be independent and thus the placement of each image
will not have preset locations available. I already have a java
program where images can be moved with the mouse to a open, preset
locations.

I have searched for demos. I find that there are none. Perhaps I
didn't word the search phrase correctly. For instance "java demo
'move two images with a mouse.'"

How about a layered layout? Put your images in a JLabel on some
different layers, then attach a simple MouseListener to each of the
labels, store the mouse position (careful whether it's absolute or
relative) on MOUSE_DOWN, compare to the position on MOUSE_UP, do a diff
and setBounds, possibly with some sandboxing. Or implement mouseDragged
and do the diffs in real-time.

How's that sound?
 
J

John B. Matthews

Daniele Futtorovic said:
How about a layered layout? Put your images in a JLabel on some
different layers, then attach a simple MouseListener to each of the
labels, store the mouse position (careful whether it's absolute or
relative) on MOUSE_DOWN, compare to the position on MOUSE_UP, do a
diff and setBounds, possibly with some sandboxing. Or implement
mouseDragged and do the diffs in real-time.

bH: Let me endorse DF's suggestion. Here's one of my favorite examples
of using JLayeredPane in this way:

<http://stackoverflow.com/questions/2562685>

As an alternative, JInternalFrame is also worth a look:

<http://download.oracle.com/javase/tutorial/uiswing/components/internalframe.html>
 
B

bH

bH: Let me endorse DF's suggestion. Here's one of my favorite examples
of using JLayeredPane in this way:

<http://stackoverflow.com/questions/2562685>

As an alternative, JInternalFrame is also worth a look:

<http://download.oracle.com/javase/tutorial/uiswing/components/interna...>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>- Hide quoted text -

- Show quoted text -

Hi Daniele and John,
A big "THANKS" for your replies.
The program below takes 3 images
allows for showing them in random order
at random locations. The user then uses the mouse to
move the images to reorder. It is a work in progress.
I have not studied the entire list of your
recommendations but eventually hope to do so.

I appreciate your responses very much.
bH

/* original: http://www.thatsjava.com/java-essentials/49032/
*
* Version2: 22 March 2011:
* Three XX.png images stored in folder named "images"
* The program takes the images puts them in random order
* at random locations. The user then uses the mouse to
* move the images to reorder. bH
*/
import java.awt.EventQueue;
import java.awt.Container;
import java.awt.Component;
import java.awt.GridLayout;

import java.awt.event. MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing. JLayeredPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class LabelDragAndDropVer2 extends JFrame implements
MouseListener, MouseMotionListener
{
JLayeredPane layeredPane;
Component dragComponent;
int xAdjustment;
int yAdjustment;
private String strNumber[] = new String [3];
private int number = 0 ;

private Random wheel = new Random();
int high= 60;
int low = 20;
int numPictures = 3;

// two arrays stores x,y values
int[] xPoints = new int[numPictures];
int[] yPoints = new int[numPictures];

public LabelDragAndDropVer2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
setVisible(true);
}

public void init() {
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
layeredPane.setLayout( null );
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );
getContentPane().add(layeredPane);


// ordering the location of picture random
for (int i= 0;i <numPictures; i++){
int m = wheel.nextInt(high - low + 1 ) + low;
xPoints= m;
int n = wheel.nextInt(high - low + 1 ) + low;
yPoints= n;
System.out.println(i+ " "+ m + " ," + n);
}
System.out.println("");

for(int i = 0; i<3; i++){
strNumber = Integer.toString(i);
number = Integer.parseInt(strNumber);
System.out.println(number);
}
// ordering the images randomly
List<String> inputList = Arrays.asList(strNumber);
Collections.shuffle(inputList);

for (int i= 0;i <3; i++){
number = Integer.parseInt(strNumber);
ImageIcon julia = new ImageIcon("images/MH"+
number + ".png");
JLabel label = new JLabel(julia);
label.setLocation(xPoints, yPoints);
label.setSize( label.getPreferredSize() );
layeredPane.add(label);
}

Container contentPane= getContentPane();
contentPane.setLayout(new GridLayout(1,2) );
setSize(600,600);// frame size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*
** Determine if we clicked on a moveable component
*/
public void mousePressed(MouseEvent e)
{
dragComponent = null;
Component c = layeredPane.findComponentAt(e.getX(),
e.getY());
if (c instanceof JLayeredPane) return;
dragComponent = c;
xAdjustment = dragComponent.getLocation().x - e.getX();
yAdjustment = dragComponent.getLocation().y - e.getY();
dragComponent.setLocation(e.getX() + xAdjustment,
e.getY() + yAdjustment);
layeredPane.moveToFront(dragComponent);
}
/*
** Move the component around the panel
*/
public void mouseDragged(MouseEvent me)
{
if (dragComponent == null) return;
dragComponent.setLocation(me.getX() + xAdjustment,
me.getY() + yAdjustment);
}
/*
** Deselect the component
*/
public void mouseReleased(MouseEvent e)
{
dragComponent = null;
}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new LabelDragAndDropVer2();
}
});
}
}
 
D

Daniele Futtorovic

Hi Daniele and John,
A big "THANKS" for your replies.
The program below takes 3 images
allows for showing them in random order
at random locations. The user then uses the mouse to
move the images to reorder. It is a work in progress.
I have not studied the entire list of your
recommendations but eventually hope to do so.

I appreciate your responses very much.
bH

/* original: http://www.thatsjava.com/java-essentials/49032/
*
* Version2: 22 March 2011:
* Three XX.png images stored in folder named "images"
* The program takes the images puts them in random order
* at random locations. The user then uses the mouse to
* move the images to reorder. bH
*/
import java.awt.EventQueue;
import java.awt.Container;
import java.awt.Component;
import java.awt.GridLayout;

import java.awt.event. MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing. JLayeredPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class LabelDragAndDropVer2 extends JFrame implements
MouseListener, MouseMotionListener
{
JLayeredPane layeredPane;
Component dragComponent;
int xAdjustment;
int yAdjustment;
private String strNumber[] = new String [3];
private int number = 0 ;

private Random wheel = new Random();
int high= 60;
int low = 20;
int numPictures = 3;

// two arrays stores x,y values
int[] xPoints = new int[numPictures];
int[] yPoints = new int[numPictures];

public LabelDragAndDropVer2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
setVisible(true);
}

public void init() {
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
layeredPane.setLayout( null );
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );
getContentPane().add(layeredPane);


// ordering the location of picture random
for (int i= 0;i<numPictures; i++){
int m = wheel.nextInt(high - low + 1 ) + low;
xPoints= m;
int n = wheel.nextInt(high - low + 1 ) + low;
yPoints= n;
System.out.println(i+ " "+ m + " ," + n);
}
System.out.println("");

for(int i = 0; i<3; i++){
strNumber = Integer.toString(i);
number = Integer.parseInt(strNumber);
System.out.println(number);
}
// ordering the images randomly
List<String> inputList = Arrays.asList(strNumber);
Collections.shuffle(inputList);

for (int i= 0;i<3; i++){
number = Integer.parseInt(strNumber);
ImageIcon julia = new ImageIcon("images/MH"+
number + ".png");
JLabel label = new JLabel(julia);
label.setLocation(xPoints, yPoints);
label.setSize( label.getPreferredSize() );
layeredPane.add(label);
}

Container contentPane= getContentPane();
contentPane.setLayout(new GridLayout(1,2) );
setSize(600,600);// frame size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*
** Determine if we clicked on a moveable component
*/
public void mousePressed(MouseEvent e)
{
dragComponent = null;
Component c = layeredPane.findComponentAt(e.getX(),
e.getY());
if (c instanceof JLayeredPane) return;
dragComponent = c;
xAdjustment = dragComponent.getLocation().x - e.getX();
yAdjustment = dragComponent.getLocation().y - e.getY();
dragComponent.setLocation(e.getX() + xAdjustment,
e.getY() + yAdjustment);
layeredPane.moveToFront(dragComponent);
}
/*
** Move the component around the panel
*/
public void mouseDragged(MouseEvent me)
{
if (dragComponent == null) return;
dragComponent.setLocation(me.getX() + xAdjustment,
me.getY() + yAdjustment);
}
/*
** Deselect the component
*/
public void mouseReleased(MouseEvent e)
{
dragComponent = null;
}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new LabelDragAndDropVer2();
}
});
}
}


Looking quite good! I would however advise that you used local variables
wherever possible (case in point: wheel, number and strNumber). If you
come from a different language culture, let me note that it is neither
canonical nor even common in Java to find code with all the variable
declarations at the start of the block. Personally, I generally define
variables the closest to the point I need them.

In the same sense, I would strongly advise you made the MouseListeners
instances of an inner, possibly anonymous, class. It will make your code
a lot more readable and, again if you come from a different language,
will give you some practice with inner classes, which are invaluable
tools in Java programming.

See: http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
 
J

John B. Matthews

Daniele Futtorovic said:
Looking quite good! I would however advise that you used local
variables wherever possible (case in point: wheel, number and
strNumber). If you come from a different language culture, let me
note that it is neither canonical nor even common in Java to find
code with all the variable declarations at the start of the block.
Personally, I generally define variables the closest to the point I
need them.

In the same sense, I would strongly advise you made the
MouseListeners instances of an inner, possibly anonymous, class. It
will make your code a lot more readable and, again if you come from a
different language, will give you some practice with inner classes,
which are invaluable tools in Java programming.

bH: If I may amplify on DF's good advice, MouseAdapter is a popular
choice for this:

private class MouseHandler extends MouseAdapter {
// your overrides, but without the empty ones
}

Then you can use an instance of it as needed:

MouseHandler mh = new MouseHandler();
layeredPane.addMouseListener(mh);
layeredPane.addMouseMotionListener(mh);

Also, consider making init() private. It's generally a bad idea to call
public methods in the constructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top