Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click and drag pictures

Status
Not open for further replies.

louisgnarf

Programmer
Jul 11, 2002
14
US
Sorry if this is a tall order...

All I want to do is create a program where I can load a graphic (jpeg/gif) into a window, and then click and drag on that picture to move it around. Can someone give me an idea where to start as far as implementing the dragging action on the picture?
 
I'm wondering if you ever figured out how to do this operation. I'm trying to visualize how to drag an image around a JPanel like you would a window in Windows OS. So the JPanel or whatever you're displaying in isn't restricted to east, west north south or any type of grid layout or display manager. Almost as if the picture were free floating in space and could be moved to any x,y inside the pane. Anyone have any ideas... the above website cleared up a lot of questions I had about DnD since I started reading it this week. Anyone else have any more thoughts/ideas?

Thanks in Advance,

Andrew
 
i remember making a java app in which a picture could be dragged around be calculating mouse movement in mouseDrag, then updating the pucture by extending paintCompnent with a bit of code that probably went a little like this:

Toolkit tk = Toolkit.getDefaultToolkit();
Image myImage = tk.getImage("picture.jpg");
//g is the graphics object passed to paintComponent
//x and y are ints specifying image position
g.drawImage(myImag, x, y, this);
 
Yeah,


I had the same idea, was trying to see if I was reinventing the wheel or not. I'm just having the biggest problem getting the picture to display, I've been trying all the tutorials and documentation code I can find on java.sun.com. If I was to want to load a picture into an application, with a Jframe and a Jpanel added to that JFrame, where or how rather could I add that picture? I have a paint function and I just can't for the life of me get it to draw any type of picture.

Any help would be greatly appreciated.
 
For those who are curious how to drag images this is what I've come up with this far. The one problem that I am running into is a bluring effect where the image isn't being removed from the background. Run the code and see what I mean if you come up with a fix or have any suggestions I would apprecite it very much.

Thanks in Advance,

Andrew


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.ImageIcon.*;

public class AddPicture extends JFrame implements MouseListener, MouseMotionListener
{
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		String imageFile = "BetaQuadPolitcal.jpg";
		Image image = toolkit.getImage(imageFile);
		JLabel label;
		int x = 100;
		int y = 100;
		
	public AddPicture()
	{
		ImageIcon icon = new ImageIcon(image, "Tooltop?");
		label = new JLabel("PlP DnD", icon, JLabel.CENTER);
		label.addMouseListener(this);
		label.addMouseMotionListener(this);
		getContentPane().add(label);
		addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

	}
	
	
	public void paint(Graphics g)
	{
		g.drawImage(image, x, y, this);
	}
	
	public void setX(int x1)
	{
		x = x1;
	}
	public void setY(int y1)
	{
		y = y1;
	}
	
	
	public void mouseClicked(MouseEvent e)
	{
		repaint();
	}

	public void mousePressed(MouseEvent e)
	{
		//setX(e.getX());
		//setY(e.getY());
		repaint();
	}
	
	public void mouseDragged(MouseEvent e)
	{
		setX(e.getX());
		setY(e.getY());
		repaint();
	}
	public void mouseMoved(MouseEvent e)
	{
	}
	
	public void mouseReleased(MouseEvent e)
	{
		repaint();
	}
	
	public void mouseEntered(MouseEvent e)
	{
	}
	
	public void  mouseExited(MouseEvent e)
	{
	}

	public static void main(String args[])
	{
	
		AddPicture ap = new AddPicture();
		ap.setSize(1086,768);
		ap.setVisible(true);
		ap.setBackground(Color.white);
	}


}
 
try adding the line 'super.paint(g);' above the line 'g.drawImage(image, x, y, this);' in 'paint'.

incedently, i use 'paintComponent' rather than 'paint', so you may want to try the same (with super.paintComponent(g) though).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top