2011-08-22 2 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 필요하지만 어떤 이유로 마우스에 반응하지 않는 것. 그 목적은 마우스를 누를 때 또 다른 노란색 원을 그리는 것입니다.

그러나 그것은 repaint()를 호출하는 것만 큼 다른 원을 그리지 않으며, 어떻게 새로운 것을 페인트 할 것인가? 다른 서클을 만들려면 로직을 제공해야합니다. 예를 들어, 두 개 이상의 노란 타원을 그리는 경우 Point 객체의 ArrayList를 만들고 mousePressed 메서드에서 Point 배열을 배열에 추가하려고합니다. 그런 다음 paintComponent 메소드에서 배열 목록을 반복하면서 각 배열에 포함 된 각 포인트에 대해 타원을 페인팅 할 수 있습니다. 예를 들어

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

:

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); 
    } 

이에 :

은 또한이를 변경하려면

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

동의하지만 난 당신의 코드에서 어떤 차이를 볼 수 없습니다 샘플 ... 아마 너무 지쳐 메신저 ... –

+0

@Sebastien : 그것은 "s"가없는 두려운 paintComponentS 대 paintComponent입니다. –

+0

고마워요. 모두 도움이됩니다. – user807398

관련 문제