Rotating and dragging of image using mouse

hi everyone,
I've got problems with dragging and rotating of images using the mouse
Can anyone show me how to that?
Thanks a million

Implement a MouseMotionListener on the Panel that you want to drag the image on.
public void mouseDragged  (   MouseEvent e   )
       if ( e.getModifiers() == MouseEvent.BUTTON1_MASK )
          if ( dragged )
              processMove( new Point2D.Double( e.getPoint().x, e.getPoint().y ) );
         else
           wasDragged = true;
              dragged    = true;
              dragPoint  = new Point2D.Double( e.getPoint().x, e.getPoint().y );
              xDiff      = 0;
              yDiff      = 0;
private void processMove
     Point2D.Double    point
          Point2D.Double paintPoint = new Point2D.Double( point.x - dragPoint.x, point.y - dragPoint.y );
       BufferedImage  imageBak   = getCurrentBackgroundImage();
       BufferedImage  background = new BufferedImage( imageBak.getWidth(), imageBak.getHeight(), imageBak.getType() );
       Graphics2D     gr         = background.createGraphics();
       gr.setColor( adaptee.getUserProfile().getBackgroundColor() );
       gr.fillRect( 0, 0, imageBak.getWidth(), imageBak.getHeight() );
       gr.drawImage( imageBak, null, (int) paintPoint.x, (int) paintPoint.y );
          gr.finalize();
       xDiff += paintPoint.x;
       yDiff += paintPoint.y;
       dragPoint = point;
       getGraphicsForPanelToDrawOn()..drawImage( background, null, 0, 0 );       
       setCurrentBackgroundImage(  background );
          }

Similar Messages

  • Rotate and scale an image witohout deforming it

    Hello you all!
    I have to rotate and scale an image, but i don't wont to deform it.
    I'm thinking to something like 16/9 images on 4/3 screens (with two horizontal or vertical black lines around the scaled image)...
    I thinked to transform the image in bitmap format, then create a bigger image and fill the empty spaces with zero-pixels...
    Is there a simplest and more efficient way to do it with 2D java classes?
    Thank you!

    See reply 8 in Help to rotate image for an idea.

  • Is it possible to rotate and scale an image?

    Is it possible to rotate and scale an image with Grapchis2D at the same time?
    One method call to do it all?
    lets say the original image size is 200x200
    I can scale the image with
    Graphics.drawImage(image, 0,0, 500,500,this);
    But now i need to rotate it as well and keep the new size which is 500x500
    how do i do that ?

    Have you already tried the scale(double sx, double
    sy) and rotate(double theta) methods of Graphics2D?no.

  • Can anyone help me to add method to rotate the image using mouse?

    Hi everyone, i am currently creating a game which require the user to be able to drag and rotate the image
    on the screen..but sadly...i'm only able to do the dragging part..
    Can anyone be able to help me add the method to rotate the image using the mouse?
    Thanks :-)
    the code
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.geom.*;
    class Game extends JFrame {
         DisplayCanvas canvas;
         public Game() {
              super("My Game");
              Container container = getContentPane();
              canvas = new DisplayCanvas();
              TitledBorder border = new TitledBorder("Game Window");
              border.setTitlePosition(TitledBorder.BOTTOM);
              canvas.setBorder(border);
              container.add(canvas);
              addWindowListener(new WindowEventHandler());
              setSize(450,400);
              show();
              class WindowEventHandler extends WindowAdapter {
                   public void windowClosing(WindowEvent e) {
                        System.exit(0);
              public static void main(String arg[]) {
                   new Example();
         class DisplayCanvas extends JPanel {
              int x, y;
              BufferedImage bi;
              DisplayCanvas() {
                   setBackground(Color.white);
                   setSize(450,400);
                   addMouseMotionListener(new MouseMotionHandler());
                   Image image = getToolkit().getImage("duke.gif");
                   MediaTracker mt = new MediaTracker(this);
                   mt.addImage(image, 1);
                   try {
                        mt.waitForAll();
                   catch (Exception e) {
                        System.out.println("Exception while loading image.");
                   if (image.getWidth(this) == -1) {
                        System.out.println("***Make sure you have the image "
                        + "(duke.gif) file in the same directory.*****");
                        System.exit(0);
                   bi = new BufferedImage(image.getWidth(this),
                   image.getHeight(this),
                   BufferedImage.TYPE_INT_ARGB);
                   Graphics2D big =bi.createGraphics();
                   big.drawImage(image, 0, 0, this);
              public void paintComponent(Graphics g) {
                   super.paintComponent(g);
                   Graphics2D g2D = (Graphics2D) g;
                   g2D.drawImage(bi, x, y, this);
              class MouseMotionHandler extends MouseMotionAdapter {
                   public void mouseDragged(MouseEvent e) {
                        x = e.getX(); y = e.getY();
                        repaint();

    research a bit on AffineTransforms. you can set the rotated instance of the Graphics2D class and it will do the rotations for you :-D

  • I used to be able to use the reduce picture ( middle button ) using Firefox and dragging the image to another screen. How do I restore this as the reduce button will not work ?

    The top right corner of the screen allows you to reduce the size of your entire image on Firefox . I used to be able to do this so I could drag the image to another screen . Suddenly I cannot do so anymore from my laptop. What can I do ?

    Could you clarify where you are doing your search?
    ''For Firefox's "Search bar",'' which is the box on the right end of the main navigation toolbar with the icon indicating your currently selected search engine, there is a hidden setting for this:
    (1) In a new tab, type or paste '''about:config''' in the address bar and press Enter. Click the button promising to be careful.
    (2) In the search box above the list, type or paste '''sear''' and pause while the list is filtered
    (3) Double-click the '''browser.search.openintab''' preference to switch it from false to true.
    If you test, does it work?
    ''For searches from other places,'' could you be more specific about where you are entering your query (e.g., address bar, built-in home page, on a search engine site)?

  • Rotating and cropping an image.

    I am taking a Photoshop for school and we are using CS5. I have CS6 and I'm stuck on my picture rotating back straight when I am trying to crop it.  I use the rotate view and rotate it 6degrees. As soon as I hit the crop button the picture goes straight. How can I keep my picture rotated to the 6degrees and crop it without the perspective crop tool?

    Rotate view only rotates...  the view; not the actual picture.  It's temporary and non-destructive. It's used to help move the canvas to a better angle for drawing brush strokes.
    You want to actually rotate the image or canvas.  Either Ctrl T for Free Transform and rotate by moving the cursor outside one of the corners and dragging.  Or Edit > Transform > Rotate

  • Cannot move an image using mouse (was: Hi;)

    I m trying to move a image via mouse.
    But i cant move that image.
    I wrote this;
    this.onMove(e.pageX, e.pageY);
    and took mouse position but i cant find how can i move image with X and Y coordinats.
    Can you help me pls

    Hi im using this code
    >>MySymbol.click<<
    sym.$('MySymbol').offset({
        left: 50,
        top: 20
    but it doesnt change anything. Can you help me plss

  • HELP!!! new iMac. screen shifts side to side and i can't use mouse when this happens.

    i bought this imac back in january and have had nothing but problems since then with it. first my OS (lion 10.7.4) crashed or some **** where i got extremely slow and would give me the colored wheel of death so i take it to worst but where i bought it and they say its a corupt hard drive so its sent out. get it back, they say it whats the os system so they re-boot the os on to it. mean while i was having the same problem ive have from the beginning which was the screen would shake side to side when ever i would be doing something and the mouse locks up and i would have to wait for it to stop. this is VERY ANOYING!!! i took it to apple and they ran a series of stress tests on it and they couldnt even get it to produce it once while they had it. so now its STILL doing it!  can anyone please help me. im ready to go to apple and demand a new computer or punch someone in the face...

    I'm speaking from experience.
    You're using a wireless Magic Mouse right?  Yeah, thought so. 
    I first noticed the problem on Lion (10.7.5) but that was also the time my Mighty Mouse or whatever that thing was called crapped out and I upgraded to the Magic Mouse.  I always thought the problem was with the OS.  My system is not supported by Mountain Lion (late 2006 model, the white one) so I did a system reinstall and stil had the issue.  Being stupid, I still attributed it to the OS but had my doubts about the mouse with all the silly swipes here and there and everywhere.  Until today.  I said enough of this garbage and switched off the mouse and plugged in a beat up wired Logitech.  Going on 4 hours now and not one screen shake.
    The "iMac screen shake".  Not very well documented.  But **** if that isn't one frustrating mofo.  Seriously, how did I use this thing for many moons with the screen shaking every 3-5 minutes.  ***??   And by the way, to stop the screen from shaking while it's doing it, madly swipe back and forth on the magic mouse shell to take control back.  Man, I'm getting upset just thinking about this problem.  And the waste of $70 for the mouse.
    And for what it's worth, I bought an iMac (2010) for my folks and I've noticed the problem on their machine too.  Yep, Magic Mouse.

  • Rotating and Translating an Image

    I am making a game with a tank that can rotate and move forwards and backwards.
    I am able to get the tank Image to rotate, but when I call a translation, it resets the image back to its original image. How can I fix this?

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TankGame extends JPanel {
        Walker walker = new Walker(this);
        BufferedImage image;
        AffineTransform at = new AffineTransform();
        Point2D.Double loc = new Point2D.Double(200,150);
        double theta = 0;
        double t = 3.0;
        boolean goAhead = true;
        public TankGame(BufferedImage image) {
            this.image = image;
            setTransform();
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.drawRenderedImage(image, at);
            //g2.setPaint(Color.red);
            //g2.fill(new Ellipse2D.Double(loc.x-2, loc.y-2, 4, 4));
        public synchronized void step() {
            int sign = goAhead ? 1 : -1;
            double x = loc.x + sign*t*Math.cos(theta);
            double y = loc.y + sign*t*Math.sin(theta);
            loc.setLocation(x, y);
            setTransform();
            repaint();
        private void setTransform() {
            double x = loc.x - image.getWidth()/2;
            double y = loc.y - image.getHeight()/2;
            at.setToTranslation(x, y);
            at.rotate(theta, image.getWidth()/2, image.getHeight()/2);
        private JPanel getUIPanel() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(1,0,1,0);
            gbc.weightx = 1.0;
            String[] ids = { "ahead", "stop", "back" };
            ActionListener al = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String ac = e.getActionCommand();
                    if(ac.equals("stop")) {
                        walker.stop();
                    } else {
                        if(ac.equals("ahead")) {
                            goAhead = true;
                        } else if(ac.equals("back")) {
                            goAhead = false;
                        walker.start();
            for(int j = 0; j < ids.length; j++) {
                JButton button = new JButton(ids[j]);
                button.addActionListener(al);
                if(j == ids.length-1)
                    gbc.gridwidth = GridBagConstraints.REMAINDER;
                panel.add(button, gbc);
            JSlider slider = new JSlider(-180, 180, 0);
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    int angle = ((JSlider)e.getSource()).getValue();
                    theta = Math.toRadians(angle);
                    setTransform();
                    repaint();
            gbc.gridwidth = 3;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            panel.add(slider, gbc);
            return panel;
        public static void main(String[] args) throws IOException {
            String path = "images/geek/geek----t.gif";
            BufferedImage image = ImageIO.read(new File(path));
            TankGame test = new TankGame(image);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.add(test.getUIPanel(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    class Walker implements Runnable {
        TankGame game;
        Thread thread;
        boolean moving = false;
        long delay = 100;
        public Walker(TankGame tg) {
            game = tg;
        public void run() {
            while(moving) {
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    moving = false;
                game.step();
        public void start() {
            if(!moving) {
                moving = true;
                thread = new Thread(this);
                thread.setPriority(Thread.NORM_PRIORITY);
                thread.start();
        public void stop() {
            moving = false;
            if(thread != null)
                thread.interrupt();
            thread = null;
    }

  • Cut and move image using mouse ?

    Hello,
    I am developing one paint program. I am making it similar to microsoft paint. In This paint, I want to use lasso, magic wand tool and select tool. But I am not too familiar with java. And want to do with your help. Please help on this topic. If lasso and magic wand tools behave same. Then please tell me about only select tool that will use for the purpose of select a particular area from canvas (canvas is drawing sheets that holds the drawing). Please help me.
    If there is any snippet of this above declaration please send. Can we use system clipboard to cut, move and paste for this drawing by the help of mouse. Please help me. I will really thankful to you.
    Thanks in advance.
    Manveer

    For the Magic Wand (at least for what I suppose it is) I would consider the image as a graph, where two pixels are connected if the difference between their color values is below some tolerance value. Then you can easily perform the iterative breadth-first algorithm to collect the pixels of the selection. I recently did something similar (although not on an image itself, but on some higher abstraction of it).
    For the Lasso i would collect all the pixels, the user selects, then calculate the bounding box over the selected pixel area and finally determine for each pixel inside the bounding box, that is no member of the lasso/selection pixels, whether it lies inside or outside the selection area. This could be accomplished by performing another bfs from one pixel, that includes all pixels besides the ones lying outside the bounding box or are pixels of the selection lasso.

  • Can no longer cut/copy/paste and drag items with my mouse

    I can no longer cut/copy and paste items. I've tried restarting and loading all updates, but that didnt help. I can also no longer drag items using my mouse. This includes in programs and on my desktop. Any ideas?

    Chitown2000 wrote:
    I can no longer cut/copy and paste items. I've tried restarting and loading all updates, but that didnt help. I can also no longer drag items using my mouse. This includes in programs and on my desktop. Any ideas?
    Many months ago I had a problem with my mouse on my iMac G5. Basically, I just wasn't able do much with my mouse anymore, sorta like you. It was terrible, having a mouse but not being able to do much with it.
    Is your problem the same one I have? Don't know, but it sure sounds familiar. I did manage to fix the problem by accident, because I'm not really sure what I did to fix it, but I wrote down the incident what I was doing when it was corrected. I stored it away just in case it ever happened again.
    I don't even remember what forum I was on when I wrote this, but here's what I wrote and maybe it will help, hopefully:
    *Ok everybody, I finally got proper behavior, it's just too bad I'm not sure of the procedure.*
    *"When I go to bed I usually run the mouse pointer to the upper left corner to activate the screensaver. Of course it didn't work. But I also knew that clicking a button causes the mouse to be sensed, so I was pushing some of the buttons while in the corner to see if I could get the screensaver going.*
    *Well, after doing this, everything is back to normal operation again. So, I fixed it totally by accident. Makes me wonder if there's some features that are turned on and off somehow by doing whatever it was I did up in that corner."*

  • Rotate and save an image

    Hi All,
    I am a new comer. Could I ask you a question about java 2D? I want to rotate an image and then save it as another file, but now I don't know how to make the rotated iamge as a new one so that I can save it. Could you give me some ideas?
    Thanks,
    Regards,

    look up the Affine Transforms or use JAI.

  • Opens fine, when scrolling pixalates the screen and drags the image to the right

    opens fine started after auto update sunday night, makes total crap out of image, have to refresh to correct

    Try to disable hardware acceleration in Firefox.
    *Tools > Options > Advanced > General > Browsing: "Use hardware acceleration when available"
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes
    See also:
    *https://support.mozilla.org/kb/upgrade-graphics-drivers-use-hardware-acceleration

  • How to rotate and transform art board using extended javascript

    1.How  to rotate artboard using extended javascript
    2.way to write code which will work similar to transform feature , in adove illustrator
    Thanks in Advance

    it is not possible to rotate an artboard in the UI, and there don't seem to be any transform commands for it at all in the JS reference, so I'd guess its not possible.

  • Rotating and saving an image

    Hi, i'm trying to rotate an image and then save the rotated image to the hard drive in jpeg format. I've managed to get the rotation part working, but i can't seem to save the image correctly.

    Here is some code. You'll need to catch exceptions etc.
    BufferedImage expImage = new BufferedImage( (int)component.getWidth(), (int)component.getHeight(), BufferedImage.TYPE_INT_RGB );
                   Graphics g2d = expImage.getGraphics();
                   draftGrid.update(g2d);
                   layoutGrid.update(g2d);
                   g2d.dispose();
                        fileName = verifyFileName(fileName, "jpeg");
                        OutputStream out = new FileOutputStream( fileName );
                        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                        encoder.encode(expImage);
                        out.flush();
                        out.close();
                        expImage.flush();
    /** This draft has been saved to fileName **/

Maybe you are looking for

  • Memory mixing

    I have a few sticks of PC2700 memoery, totalling about 1gb. I already have 512 of PC3200 in the motherboard, what will increase performance? Leaving the 512m and  not installing the other stuff (to take to 1.5GB), or have more memory running slower -

  • How can I access my movies from my iTunes account using my iPad.

    I thought that when you accessed iTunes from your iPad, using iOS5 you could access all of your iTunes like you did on your PC.  However I can get my music but not movies.  Please can someone help me.  I am new to the apple game. Thank you Apple grov

  • Adobe Reader XI freezes when saving in Windows 8.1

    I'm trying to update the editable fields in the US Federal tax forms using Adobe Reader XI on Windows 8.1 (PC - not a tablet).  I'm up-to-date on all Windows softward and Adobe Reader software. I could save the original file downloaded from the irs.g

  • Beat Mapping Logic 9 Problem

    Hi. I'm having difficulty getting beat mapping to work with Logic 9.1.6. I have tried to select the notes in my guide sequence but that yellow line all too often refuses to line up with the start of the relevant note. I tried copying all the beat not

  • Get next id

    Hi there, When inserting a new record I want to assign the next property_id from the property.property_id table. The query: <cfquery name="GetNewID" datasource="#application.DSN_Name#"> SELECT top 1 property_id FROM property ORDER BY property_id desc