2014-11-08 44 views
0

我想計算mouseClicks,但我不明白爲什麼我的計數器每增加2點擊一次。嘗試getClickCount(),但它也不是我所需要的。爲什麼我的mouseClicked()計數器增加2?

我計算後的目標是:我會使用計數器在不同點擊次數上繪製不同的東西。可以說,第一和第二總是會得到drawLine()的第三點和drawRect()的座標。

package graphics_training_painting; 

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.JFrame; 

public class U4 extends Canvas implements MouseListener{ 
    private int x1; 
    private int y1; 
    private int x2; 
    private int y2; 
    private int counter = 0; 

    public U4() { 
     setBackground(Color.white); 
    } 

    public static void main(String[] args) { 
     U4 u = new U4(); 
     JFrame f = new JFrame(); 
     f.add(u); 
     f.setSize(800, 600); 
     f.setVisible(true); 
    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 
     counter++; 
     System.out.println(counter); 
    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 


    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mousePressed(MouseEvent e) { 
/*  x1 = e.getX(); 
     y1 = e.getY();*/ 
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
/*  x2 = e.getX(); 
     y2 = e.getY(); 
     repaint();*/   
    } 

    public void paint(Graphics g) { 
     addMouseListener(this); 
     g.setColor(Color.blue); 
     g.drawLine(x1, y1, x2, y2); 

    } 
} 

非常感謝您的建議和意見, Timmy!

回答

4

不要在你的paint方法中添加你的MouseListener!這將增加許多偵聽器,每個偵聽器在激活時遞增計數器。

您需要知道,您無法控制何時或如何調用paint,並且在典型的程序運行過程中很可能會調用很多次。出於這個原因和其他原因,你不應該把程序邏輯,狀態改變代碼或組件創建放在這個方法中。將MouseListener添加到一些初始化代碼中,如構造函數一次

順便說一句,你不想混合使用AWT和Swing組件。相反,你應該讓你的U4類擴展JPanel,並且在其paintComponent方法中進行繪製。

因此,改變這種:

public U4() { 
    setBackground(Color.white); 
} 


// ... 

public void paint(Graphics g) { 
    addMouseListener(this); 
    g.setColor(Color.blue); 
    g.drawLine(x1, y1, x2, y2); 
} 

這樣:

public U4() { 
    setBackground(Color.white); 
    addMouseListener(this); 
} 


// ... 

public void paint(Graphics g) { 
    // addMouseListener(this); 
    super.paint(g); 
    g.setColor(Color.blue); 
    g.drawLine(x1, y1, x2, y2); 
} 

,然後下一個,讓我推薦

類似的變化:

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.Stroke; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.*; 

public class U4b extends JPanel { 
    private static final Color BG = Color.white; 
    private static final Color DRAW_COLOR = Color.red; 
    private static final int PREF_W = 800; 
    private static final int PREF_H = 600; 
    private static final Stroke BASIC_STROKE = new BasicStroke(3f); 
    private int counter = 0; 
    private int x1 = 0; 
    private int y1 = 0; 
    private int x2 = 0; 
    private int y2 = 0; 

    public U4b() { 
     setBackground(BG); 
     MyMouseListener myMouseListener = new MyMouseListener(); 
     addMouseListener(myMouseListener); 
     addMouseMotionListener(myMouseListener); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
     return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setColor(DRAW_COLOR); 
     g2.setStroke(BASIC_STROKE); 
     g2.drawLine(x1, y1, x2, y2); 
    } 

    private class MyMouseListener extends MouseAdapter { 
     @Override 
     public void mousePressed(MouseEvent e) { 
     counter++; 
     System.out.println("Counter: " + counter); 
     x1 = e.getX(); 
     y1 = e.getY(); 
     x2 = x1; 
     y2 = y1; 
     } 

     @Override 
     public void mouseDragged(MouseEvent e) { 
     x2 = e.getX(); 
     y2 = e.getY(); 
     repaint(); 
     } 

     @Override 
     public void mouseReleased(MouseEvent e) { 
     x2 = e.getX(); 
     y2 = e.getY(); 
     repaint(); 
     } 

    } 

    private static void createAndShowGui() { 
     U4b mainPanel = new U4b(); 

     JFrame frame = new JFrame("U4b"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

謝謝你很多!現在開始吸收它。 :-) – TimmyPrograming 2014-11-08 16:15:41