2013-04-29 52 views
0

即時消息我試圖做的是繪製圖像,然後我使用這種方法來隱藏並再次繪製鼠標所在的圖像。MouseMoved事件

即時通訊面臨的問題,我仍然從舊圖像中得到一些小的東西,它並不完全隱藏圖片,當我用「KeyPressed」等不同的方法試用時,一切正常。

這是做鼠標事件的正確方法嗎?或者這個代碼不正確?

public class GameFrame extends javax.swing.JFrame implements java.awt.event.ActionListener 
{ 

    private java.util.Vector<Milk> foodList = new java.util.Vector<Milk>(); 
    private javax.swing.Timer moveTimer = new javax.swing.Timer(500, this); 
    private MotherFigure motherFig; 
    private BabyFigure babyFig; 
    private FastFood burgerFig; 
    private int speedTrack = 200; 
    private int milkCount = 1; 

    @Override 
    public void actionPerformed(ActionEvent ae) 
    { 
     motherFig.draw(); 
     babyFig.draw(); 
     moveFood(); 
     collided(); 
     motherFig.hide(); 
    } 

    /** 
    * Creates new form GameFrame 
    */ 
    public GameFrame() 
    { 
     initComponents(); 
     motherFig = new MotherFigure(gamePanel); 
     babyFig = new BabyFigure(gamePanel); 
     burgerFig = new FastFood(gamePanel); 
     moveTimer.start(); 
     gamePanel.requestFocus(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the 
    * form. WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     gamePanel = new javax.swing.JPanel(); 
     txtCount = new javax.swing.JTextField(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setBackground(new java.awt.Color(255, 255, 255)); 
     setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)); 
     getContentPane().setLayout(null); 

     gamePanel.setBackground(new java.awt.Color(255, 255, 255)); 
     gamePanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { 
      public void mouseMoved(java.awt.event.MouseEvent evt) { 
       gamePanelMouseMoved(evt); 
      } 
     }); 
     gamePanel.addFocusListener(new java.awt.event.FocusAdapter() { 
      public void focusGained(java.awt.event.FocusEvent evt) { 
       gamePanelFocusGained(evt); 
      } 
     }); 
     gamePanel.addKeyListener(new java.awt.event.KeyAdapter() { 
      public void keyPressed(java.awt.event.KeyEvent evt) { 
       gamePanelKeyPressed(evt); 
      } 
     }); 

     txtCount.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
     txtCount.setBorder(null); 
     txtCount.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); 
     txtCount.setSelectionColor(new java.awt.Color(255, 255, 255)); 

     javax.swing.GroupLayout gamePanelLayout = new javax.swing.GroupLayout(gamePanel); 
     gamePanel.setLayout(gamePanelLayout); 
     gamePanelLayout.setHorizontalGroup(
      gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gamePanelLayout.createSequentialGroup() 
       .addContainerGap(596, Short.MAX_VALUE) 
       .addComponent(txtCount, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap()) 
     ); 
     gamePanelLayout.setVerticalGroup(
      gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(gamePanelLayout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(txtCount, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(545, Short.MAX_VALUE)) 
     ); 

     getContentPane().add(gamePanel); 
     gamePanel.setBounds(80, 10, 700, 580); 

     pack(); 
    }// </editor-fold>       

    private void gamePanelKeyPressed(java.awt.event.KeyEvent evt)          
    {           
     if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_LEFT) 
     motherFig.move(-10, 0); //babyFig.move(-10, 0); 
     else if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_RIGHT) 
     motherFig.move(10, 0); //babyFig.move(10, 0); 
     motherFig.draw(); 

    }          

    private void gamePanelFocusGained(java.awt.event.FocusEvent evt)          
    {           
     // TODO add your handling code here: 
     txtCount.setText("Milk Catched: "); 
     Milk b = new Milk(gamePanel); 
     b.draw(); 
     foodList.add(b); 
    }          

    private void gamePanelMouseMoved(java.awt.event.MouseEvent evt) {          
     motherFig.hide(); 
     int X = evt.getX(); 
     int Y = evt.getY(); 
     motherFig.move(X, Y); 
     motherFig.reDraw(X, Y); 

    }          

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) 
    { 
     /* 
     * Set the Nimbus look and feel 
     */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* 
     * If Nimbus (introduced in Java SE 6) is not available, stay with the 
     * default look and feel. For details see 
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try 
     { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
     { 
      if ("Nimbus".equals(info.getName())) 
      { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
     } 
     catch (ClassNotFoundException ex) 
     { 
     java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     catch (InstantiationException ex) 
     { 
     java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     catch (IllegalAccessException ex) 
     { 
     java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     catch (javax.swing.UnsupportedLookAndFeelException ex) 
     { 
     java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* 
     * Create and display the form 
     */ 
     java.awt.EventQueue.invokeLater(new Runnable() 
     { 

     @Override 
     public void run() 
     { 
      new GameFrame().setVisible(true); 
     } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JPanel gamePanel; 
    private javax.swing.JTextField txtCount; 
    // End of variables declaration     

    private void moveFood() 
    { 
     for (int i = 0; i < foodList.size(); i++) 
     { 
     foodList.get(i).hide(); 
     } 

     for (int i = 0; i < foodList.size(); i++) 
     { 
     foodList.get(i).move(); 
     } 
     for (int i = 0; i < foodList.size(); i++) 
     { 
     foodList.get(i).draw(); 
     } 
    } 
+1

Java || JavaScript的?他們是不一樣的 - 一些額外的上下文和/或[SSCCE](http://sscce.org/)將有幫助 – MadProgrammer 2013-04-29 01:54:57

+0

對不起,我不太瞭解java和JavaScript之間的差異。我正在使用netbeans。 – Kaaky 2013-04-29 02:20:23

回答

2
  • 你做你擺動拉絲不正確。您不應該在組件上調用getGraphics(),然後在其上繪圖,因爲如此獲得的Graphics對象將不會保留,並且您的繪圖也不會。
  • 取而代之的是在JComponent的paintComponent(Graphics g)中繪製方法覆蓋。
  • 你會想在上面提到的覆蓋中調用super.paintComponent(g);,通常在你的方法覆蓋的第一行。這將爲您擦除任何舊圖像。
  • 基本的Performing Custom Painting with Swing Tutorial將幫助你的細節
  • Painting in AWT and Swing article會告訴你更多你想知道的高級信息。
+0

謝謝你,但hide()方法在抽象類中,我不應該將其修改爲「作爲賦值的要求」。 jpanel也有多個繪圖,我使用隱藏的大部分圖形,但它不適用於MouseMove。 – Kaaky 2013-04-29 02:50:30

+0

@Kaaky:你是說你上面的'hide()'方法是你的老師給你的嗎? – 2013-04-29 02:54:04

+0

是的抽象類是由我們的教師給出的,它隱藏了()和其他一些方法。 – Kaaky 2013-04-29 03:07:34