2011-08-22 56 views
2

我一直在努力做這個任務,併爲它工作,我需要的事件mousePressed工作,但由於某種原因,它不響應鼠標。其目的是在按下鼠標時繪製另一個黃色圓圈。沒有的mousePressed工作

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.util.Random; 

import javax.swing.ImageIcon; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class CatchMonster extends JPanel 
{ 
    private int height = 300; 
    private int width = 600; 
    private final int delay = 4001; 

    private ImageIcon image; 
    private Timer timer; 
    private int x, y, moveX, moveY, xPoint, yPoint; 

public CatchMonster() { 

    DotListener dot = new DotListener(); 
    addMouseListener(dot); 


    timer = new Timer(delay, new timerListener()); 
    x = 40; 
    y = 40; 

    moveX = moveY = 3; 
    setPreferredSize(new Dimension(width, height)); 
    setBackground(Color.black); 
    timer.start(); 

} 

public void paintComponent(Graphics g) { 
    super.paintComponents(g); 
    g.setColor(Color.yellow); 
    g.fillOval(x, y, 60, 60); 
} 

private class timerListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) { 
     Random gn = new Random(); 
     x = gn.nextInt(width); 
     y = gn.nextInt(height); 

     repaint(); 
    } 
    } 

private class DotListener implements MouseListener 
{ 
    public void mousePressed(MouseEvent event) 
    { 
     repaint(); 
    } 

    @Override 
    public void mouseClicked(MouseEvent event) { 


    } 

    @Override 
    public void mouseEntered(MouseEvent event) { 
     // TODO Auto-generated method stub 

    } 

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

    } 

    @Override 
    public void mouseReleased(MouseEvent event) { 
     // TODO Auto-generated method stub 

    } 


} 

}

回答

2

我需要的mousePressed事件工作,但由於某種原因,它不響應鼠標。其目的是在按下鼠標時繪製另一個黃色圓圈。

但它不會畫另一個圈子爲它所做的就是調用重繪(),那就是如何去畫什麼新的東西?如果你想要它創建另一個圈子,你必須給它邏輯來做到這一點。舉例來說,如果你想畫一個以上的黃色橢圓形的,你需要創建點對象的ArrayList,並添加一個點進去的方法的mousePressed數組列表。然後在paintComponent方法中,您可以遍歷數組列表,爲它包含的每個Point繪製橢圓。

此外,要改變這樣的:

public void paintComponent(Graphics g) { 
     super.paintComponents(g); // this is not the "super" method of paintComponent 
     g.setColor(Color.yellow); 
     g.fillOval(x, y, 60, 60); 
    } 

這樣:

public void paintComponent(Graphics g) { 
     super.paintComponent(g); // See the difference? 
     g.setColor(Color.yellow); 
     g.fillOval(x, y, 60, 60); 
    } 

例如:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class CatchMonster extends JPanel { 
    private int height = 300; 
    private int width = 600; 
    private final int delay = 4001; 

    private ImageIcon image; 
    private Timer timer; 
    private int x, y, moveX, moveY, xPoint, yPoint; 
    private List<Point> points = new ArrayList<Point>(); 

    public CatchMonster() { 

     DotListener dot = new DotListener(); 
     addMouseListener(dot); 

     timer = new Timer(delay, new timerListener()); 
     x = 40; 
     y = 40; 

     moveX = moveY = 3; 
     setPreferredSize(new Dimension(width, height)); 
     setBackground(Color.black); 
     timer.start(); 

    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.yellow); 
     g.fillOval(x, y, 60, 60); 

     int radius = 30; 
     g.setColor(Color.green); 
     for (Point p : points) { 
     int x = p.x - radius; 
     int y = p.y - radius; 
     g.fillOval(x, y, 2 * radius, 2 * radius); 
     } 
    } 

    private class timerListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
     Random gn = new Random(); 
     x = gn.nextInt(width); 
     y = gn.nextInt(height); 

     repaint(); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("Foo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new CatchMonster()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    private class DotListener extends MouseAdapter { 
     public void mousePressed(MouseEvent event) { 
     points.add(event.getPoint()); 
     repaint(); 
     } 

    } 
} 
+0

同意,但是我沒有看到你的代碼有什麼區別樣品......也許IM太累了... –

+0

@Sebastien:這是可怕的paintComponentS與無的paintComponent的「S」 –

+0

謝謝大家它確實有助於 – user807398