2011-08-22 58 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