mouseListener的反应缓慢(重新涂漆)

时间:2013-04-06 07:39:30

标签: java performance swing repaint mouselistener

我制作了一个框架,当我点击它时也重新绘制了自己(也绘制了新的几何图形),但是当我快速点击它没有如此快速响应时,它需要半秒钟点击之间。我做错了什么?

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;


public class Okienko extends Frame implements MouseListener{
public static final int SIZE = 500;
public static int mX = 0,mY = 0;
public ArrayList<Wyrysowywalny> l; //COLLECTION OF OBJECT TO DRAW
    Okienko(){
        l = new ArrayList<Wyrysowywalny>();
        createGUI();
    }
    public void createGUI(){
        setSize(SIZE, SIZE);
        setVisible(true);
        setAlwaysOnTop(true);
        setTitle("Zadanie 1");
        addWindowListener(new WindowListener() {    
            public void windowOpened(WindowEvent arg0) {}           
            public void windowIconified(WindowEvent arg0) {}        
            public void windowDeiconified(WindowEvent arg0) {}              
            public void windowDeactivated(WindowEvent arg0) {}
            public void windowClosed(WindowEvent arg0) {}
            @Override
            public void windowClosing(WindowEvent arg0) {
                JOptionPane.showConfirmDialog(null, "dziekujemy za skorzystanie z programu","",JOptionPane.PLAIN_MESSAGE);
                System.exit(0);     
            }
            @Override
            public void windowActivated(WindowEvent arg0) {
                repaint();

            }
        });
        addMouseListener(this);
    }


    @Override
    public void mouseClicked(MouseEvent e) {///  IMPORTANT!
        System.out.println(e.getX() + " " + e.getY());
        mX = e.getX();
        mY = e.getY();
        int r;

        r = (int) (Math.random() *  6);
        switch(r){
            case 0: l.add(new Trojkat(mX,mY,lXY(),lXY(),lXY(),lXY()));break; // OBJECTS TO DRAW     
            case 1: l.add(new Prostokat(mX,mY,lR(),lR()));break;
            case 2: l.add(new Kwadrat(mX,mY,lR()));break;
            case 3: l.add(new Kolo(mX,mY,lR()));break;
            case 4: l.add(new Elipsa(mX,mY,lR(),lR())); break;
            case 5: l.add(new TrojkatRownoboczny(mX,mY,lR())); break;
        }       
        repaint();

    }
    @Override
    public void mouseEntered(MouseEvent arg0) {}
    @Override
    public void mouseExited(MouseEvent arg0) {}
    @Override
    public void mousePressed(MouseEvent arg0) {}
    @Override
    public void mouseReleased(MouseEvent arg0) {}

    public static int lXY(){
        return (int) (Math.random()*SIZE * 4d/5 + 1d/40*SIZE);
    }
    public static int lR(){
        return (int) (Math.random()*200 - 1d/40*SIZE);
    }
    public void paint(Graphics g){
        super.paint(g);
        for(Wyrysowywalny w : l)
            w.draw(g);//DRAW OBJECT
    }


    public static void main(String[] args) {

        new Okienko();
    }


}

1 个答案:

答案 0 :(得分:2)

如果没有完整的例子,我只能做几点观察:

  • 您可能希望回复mouseClicked()

  • 而不是在同一组件中释放鼠标时触发的mousePressed()
  • 同时考虑MouseAdapter而不是implements MouseListener

  • 如上所述here,“Swing程序应该覆盖paintComponent()而不是覆盖paint()。”

  • 应在event dispatch thread构建和操作Swing GUI对象。

  • 引用了一个没有明显延迟的更详细的例子here