0

so I made a custom shaped JFrame using setShape(s); and I got it to look how I wanted the problem when you set the JFrame to be undecorated you can't drag the Frame with your mouse on the screen, so I tried to implement my own draggable frame, but it's not working as supposed to, here's the Frame class :

package rt;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.event.MouseInputAdapter;

public class MyFrame extends JFrame{

private static final int WIDTH = 1024;
private static final int HEIGHT = 1024;
private static int [] OCTAGON_COORDS_X = {300, 524, 680, 680, 524, 300, 144, 144};
private static int [] OCTAGON_COORDS_Y = {300, 300, 457, 680, 836, 836, 680, 457};

public MyFrame() throws IOException {
    // Determine what the default GraphicsDevice can support.
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();

    boolean isUniformTranslucencySupported = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);
    boolean isShapedWindowSupported = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT);
    if (isUniformTranslucencySupported && isShapedWindowSupported) {
        setUndecorated(true);
        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        updateFrame();
        //setOpacity(0.55f);
        setVisible(true);
        MyListener myListener = new MyListener();
        addMouseListener(myListener);
        addMouseMotionListener(myListener);
    }
    else {
        System.err.println("Custom shaped JFrames and trunslucent JFrames are not supported by this device!");
        System.exit(0);
    }
}

public void updateFrame() {
    Shape s = new Polygon (OCTAGON_COORDS_X, OCTAGON_COORDS_Y, OCTAGON_COORDS_X.length);
    setShape(s);
}

private class MyListener extends MouseInputAdapter {

    private int initialPressedX;
    private int initialPressedY;

    @Override
    public void mousePressed(MouseEvent e) {
        initialPressedX = e.getX();
        initialPressedY = e.getY();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        updateFrameCoords(e);
    }


    void updateFrameCoords(MouseEvent e) {
        int dx = e.getX() - initialPressedX;
        int dy = e.getY() - initialPressedY;
        for (int i = 0; i < OCTAGON_COORDS_X.length; i++) {
            OCTAGON_COORDS_X[i] += dx;
            OCTAGON_COORDS_Y[i] += dy;
        }
        updateFrame();
    }
}

}

Main class :

package rt;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        MyFrame frame = new MyFrame();
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

But the dragging action isn't working as I expected I guess the problem lies in those lines : Anyone mind checking and explaining what I did wrong and how to fix it ? Thanks in advance !

user3497284
  • 127
  • 2
  • 7
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 04 '14 at 09:29
  • Ok, so if someone want to see the complete code here it is :http://pastebin.com/fLx5hSDA - just create a new MyFrame instance in Main class – user3497284 Apr 04 '14 at 09:33
  • 1
    *"if someone want to see the complete code"* I don't want to see 'the complete code' and won't follow links. Post an [MCVE](http://stackoverflow.com/help/mcve) direct to the question as an [edit](http://stackoverflow.com/posts/22858498/edit) if you want my help (and many other people think much the same). – Andrew Thompson Apr 04 '14 at 09:37
  • I included the whole code inside the code tabs is that ok ? Sorry if I don't understand what you mean, english isn't my native so forgive me.. – user3497284 Apr 04 '14 at 09:44
  • *"..is that ok ?"* It should be runnable, so it needs a `main(..)`. The imports also seem to be missing. Now you said earlier *"just create a new MyFrame instance in Main class"* - if you could not be bothered doing that, we should we? After all, it is your problem.. The idea of an MCVE is to make code so that it is copy/paste, compile, run & see problem. The more barriers you put in the way of people who are offering free help, the more likely they are to just move to the next post/question. – Andrew Thompson Apr 04 '14 at 09:48
  • Sorry I had problem with the code tabs, whenever I added the full code some parts of it were outside of the code tab, I tried to add it again and the problem still occurred so I figured what was causing it, please tell me if it's ok now. – user3497284 Apr 04 '14 at 09:53
  • Possible [example](http://stackoverflow.com/questions/16869877/how-to-remove-window-box-from-any-java-gui/16869893#16869893) – MadProgrammer Apr 04 '14 at 10:01

2 Answers2

1

I think your problem is worrying about the shape of the frame when executing the mouseDragged. I don't think you need to worry about that.

If you just change your method to the below, it works. Test it out.

void updateFrameCoords(MouseEvent e) {
    setLocation(getLocation().x + e.getX() - initialPressedX,
                getLocation().y + e.getY() - initialPressedY);
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0
  1. You have to update initial coordinates at each drag.
  2. evt.getX() gives coordinates relative to frame. You should use evt.getXOnScreen() in stead.

Replace

void updateFrameCoords(MouseEvent e) {
    int dx = e.getX() - initialPressedX;
    int dy = e.getY() - initialPressedY;
    for (int i = 0; i < OCTAGON_COORDS_X.length; i++) {
        OCTAGON_COORDS_X[i] += dx;
        OCTAGON_COORDS_Y[i] += dy;
    }
    updateFrame();
}

with

void updateFrameCoords(MouseEvent e) {
    int dx = e.getXOnScreen() - initialPressedX;
    int dy = e.getYOnScreen() - initialPressedY;
    for (int i = 0; i < OCTAGON_COORDS_X.length; i++) {
        OCTAGON_COORDS_X[i] += dx;
        OCTAGON_COORDS_Y[i] += dy;
    }
    updateFrame();
    initialPressedX = e.getXOnScreen();
    initialPressedY = e.getYOnScreen();
}

Same in mousePressed event.

Good luck.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45