2015-05-25 60 views
1

我有一個小程序,創建了一個東西,可以被認爲是一個遊戲杆。一切正常,但有一個小故障,我無法解決如何解決。如果鼠標移動得太快,我拖動的JLabel就會粘住,因爲鼠標已經移出了繪製的框。我可以增加Jlabel的大小,但是「O」與鼠標偏移太多。 (我寧願減小尺寸,但通過這種實現,最大鼠標速度太低)。 任何想法如何解決這個問題?JLabel跟不上鼠標

這是整個代碼,順便說一句,任何人都可以繼續編譯,看看到底是什麼問題,當鼠標移動太快。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class MainClass implements ActionListener, MouseListener, MouseMotionListener 
{ 
int labelSize = 20; 
int screenOffsetX = 58; 
int screenOffsetY = 130; 
JFrame frame; 
JLabel xAxis; 
JLabel move; 
JLabel drag; 
JLabel yAxis; 
JLabel xAxisDrag; 
JLabel yAxisDrag; 
JLabel radio; 
JLayeredPane panel; 
Robot rob; 
public static void main(String[] args) 
{ 
    new MainClass(); 
} 
public MainClass() 
{ 
    frame = new JFrame("app"); 
    frame.setLayout(null); 
    frame.setBounds(20, 20, 400, 500); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 

    move = new JLabel("Movement"); 
    move.setBounds(150, 0, 100, 15); 
    frame.add(move); 

    xAxis = new JLabel("X"); 
    xAxis.setBounds(100, 20, 50, 15); 
    frame.add(xAxis); 

    yAxis = new JLabel("Y"); 
    yAxis.setBounds(200, 20, 50, 15); 
    frame.add(yAxis); 

    drag = new JLabel("Dragging"); 
    drag.setBounds(150, 40, 100, 15); 
    frame.add(drag); 

    xAxisDrag = new JLabel("X"); 
    xAxisDrag.setBounds(100, 60, 50, 15); 
    frame.add(xAxisDrag); 

    yAxisDrag = new JLabel("Y"); 
    yAxisDrag.setBounds(200, 60, 50, 15); 
    frame.add(yAxisDrag); 

    radio = new JLabel("O"); 
    radio.setBounds(0, 0, labelSize, labelSize); 
    radio.setOpaque(false); 
    radio.setEnabled(false); 
    panel = new JLayeredPane(); 
    panel.setBounds(50, 100, 257, 257); 
    panel.setLayout(null); 
    panel.setBackground(new Color((float)1.0,(float)1.0,(float)1.0)); 
    panel.setOpaque(true); 
    panel.add(radio); 
    radio.setLocation(128, 128); 
    frame.add(panel); 
    panel.addMouseMotionListener(this); 
    panel.addMouseListener(this); 
    frame.revalidate(); 
    frame.repaint(); 
} 
@Override 
public void actionPerformed(ActionEvent e) {} 
@Override 
public void mouseDragged(MouseEvent e) 
{ 
    if(!(e.getPoint().x<0 || e.getPoint().y<0 || e.getPoint().x>257 || e.getPoint().y>257)) 
    { 
     xAxis.setText("X: "+((e.getPoint().x/4)-32)); 
     yAxis.setText("Y: "+((e.getPoint().y/4)-32)); 
    } 
    if(e.getPoint().x<0 || e.getPoint().y<0 || e.getPoint().x>257 || e.getPoint().y>257) 
    { //Neļauj iziet ārpus paneļa; 
     try 
     { 
      rob = new Robot(); 
      if(e.getPoint().x<0) 
       rob.mouseMove((screenOffsetX+frame.getX()), (e.getPoint().y+screenOffsetY+frame.getY())); 
      if(e.getPoint().y<0) 
       rob.mouseMove((e.getPoint().x+screenOffsetX+frame.getX()), (screenOffsetY+frame.getY())); 
      if(e.getPoint().x>257) 
       rob.mouseMove((257+screenOffsetX+frame.getX()), (e.getPoint().y+screenOffsetY+frame.getY())); 
      if(e.getPoint().y>257) 
       rob.mouseMove((e.getPoint().x+screenOffsetX+frame.getX()), (257+screenOffsetY+frame.getY())); 
     } 
     catch (AWTException e1) 
     { 
      e1.printStackTrace(); 
     } 
    } 
    if( (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) && 
      (e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize) 
      ) 
    { //Ja kursors ir uz JLabel, tad pārvieto; 
     xAxisDrag.setText("X: "+((e.getPoint().x/4)-32)); 
     yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32)); 
     radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize); 
    } 
} 
@Override 
public void mouseMoved(MouseEvent e) 
{ 
    xAxis.setText("X: "+((e.getPoint().x/4)-32)); 
    yAxis.setText("Y: "+((e.getPoint().y/4)-32)); 
} 
@Override 
public void mouseClicked(MouseEvent arg0) {} 
@Override 
public void mouseEntered(MouseEvent e) {} 
@Override 
public void mouseExited(MouseEvent e) {} 
@Override 
public void mousePressed(MouseEvent e) {} 
@Override 
public void mouseReleased(MouseEvent e) 
{ 
    radio.setLocation(124, 124); 
    xAxisDrag.setText("X: "+0); 
    yAxisDrag.setText("Y: "+0); 
} 

}

回答

0

更換

 if( (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) && 
     (e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize) 
     ) 
{ //Ja kursors ir uz JLabel, tad pārvieto; 
    xAxisDrag.setText("X: "+((e.getPoint().x/4)-32)); 
    yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32)); 
    radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize); 
} 

由:

if(  (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) && 
     (e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize) 
     ) 
{ 
    //Has the mouse been clicked inside the radio before grabbing started ? 
    if(grab==0) //if it is the first iteration of the function mousedragged 
     grab=2; //proceed to "grab" 
} 

if(grab==2 && (e.getPoint().x>=0 && e.getPoint().x<panel.getWidth()) && 
     (e.getPoint().y>=0 && e.getPoint().y<panel.getHeight()) 
     ) 
{ //Ja kursors ir uz JLabel, tad pārvieto; 
    System.out.println(radio.getX()+" "+radio.getY()); 
    xAxisDrag.setText("X: "+((e.getPoint().x/4)-32)); 
    yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32)); 
    radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize); 

} 
if(grab!=2) // if grabbing is not started 
    grab=1; // first iteration is over 

更新功能的mouseReleased到:

 @Override 
    public void mouseReleased(MouseEvent e) 
    { 
     //this a new line 
     //grabbing is over 
     grab=0; // go back to idle state 
     radio.setLocation(124, 124); 
     xAxisDrag.setText("X: "+0); 
     yAxisDrag.setText("Y: "+0); 
    } 

最後,一個叫搶新字段添加到您的類MainClass:

private int grab=0; //idle state 

搶的意思是:
1.搶= 0 =>這是該函數的mouseDragged被執行,在第一時間「抓住「還沒有開始。事實上,當你拖動鼠標時,這個函數會像無限循環一樣迭代,直到你釋放鼠標。所以瞭解它何時開始非常重要。
2. grab = 1 =>迭代已經開始但沒有「抓取」
3. grab = 2 =>「抓取」已經開始。在情節化之前你可以注意到條件grab == 0。事實上,我試圖避免這種情況,當鼠標從其他地方拖出來,通過「收音機」,然後收音機被「抓住」。這就是爲什麼我檢查它是第一次迭代。在這種情況下,我們抓住= 1 =>收音機不抓住

+0

它應該是這樣的。如果我使用你的建議,當單擊鼠標時,「radio」移動到面板內的任何一點。它需要像我「抓住」收音機一樣,然後移動它。 – user3665674

+0

@ user3665674如果它解決了您的問題,請將其作爲答案投票。 – Dien

+0

的確如此,謝謝。現在我只是簡單地複製它,現在我會再次通過代碼,以確保我明白爲什麼它的工作原理。再次感謝。 – user3665674