2014-04-04 1 views
0

setShape(s); 을 사용하여 사용자 정의 모양의 JFrame을 만들었으며 JFrame을 장식하지 않도록 설정할 때 어떻게 보이는지 보았습니다. 마우스로 프레임을 드래그 할 수 없습니다.Java - 사용자 정의 모양의 드래그 할 수있는 JFrame

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 클래스 :

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); 
    } 
} 
화면, 그래서 난 내 자신의 드래그 프레임을 구현하기 위해 노력했지만 가정대로 작동하지 않습니다, 여기에 프레임 클래스의

}

그러나 드래그 동작이 작동하지 않습니다. 문제가 해당 줄에 있다고 생각합니다. 누구나 내가 잘못하고 어떻게 수정했는지 확인하고 설명 할 수 있습니까? 미리 감사드립니다.

+1

더 나은 도움을 받으려면 [MCVE] (http://stackoverflow.com/help/mcve) (최소 완료 및 검증 가능한 예)를 게시하십시오. –

+0

좋아, 그럼 여기에 전체 코드를보고 싶다면 : http : //pastebin.com/fLx5hSDA - 메인 클래스에서 새로운 MyFrame 인스턴스를 만들자. – user3497284

+1

* "누군가가 완전한 코드를보고 싶다면"* 나는 돈을 내지 않는다. 완전한 코드 '를보고 싶지 않으며 링크를 따르지 않을 것입니다. 내 도움을 원한다면 [편집] (http://stackoverflow.com/posts/22858498/edit)로 [MCVE] (http://stackoverflow.com/help/mcve) 직접 질문을 게시하십시오. 다른 사람들은 많이 똑같다고 생각합니다). –

답변

1

mouseDragged을 실행할 때 문제가 프레임 모양에 문제가 있다고 생각합니다. 나는 당신이 그것에 대해 걱정할 필요가 없다고 생각합니다.

방금 ​​방법을 아래로 변경하면 작동합니다. 그것을 밖으로 시험하십시오.

void updateFrameCoords(MouseEvent e) { 
    setLocation(getLocation().x + e.getX() - initialPressedX, 
       getLocation().y + e.getY() - initialPressedY); 
} 
+0

감사합니다. – user3497284

0
  1. 는 각 드래그에서 초기 좌표을 업데이트해야합니다.
  2. evt.getX()은 프레임에 대한 좌표가 입니다. 대신 evt.getXOnScreen()을 사용해야합니다.

mousePressed 이벤트에

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(); 
} 

같은과

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(); 
} 

를 교체합니다.

행운을 빈다.

관련 문제