1

我制作了一个框架,当我点击它时它会重新绘制(也绘制了新的几何图形)但是当我快速点击它时它的响应速度并不快,它需要在点击之间半秒。我做错了什么?

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


}
4

1 回答 1

2

没有一个完整的例子,我只能做几个观察:

  • 当鼠标在同一个组件中释放时触发,而不是mouseClicked(),您可能想要响应mousePressed()

  • 也考虑MouseAdapterimplements MouseListener

  • 正如这里提到的,“Swing 程序应该覆盖paintComponent()而不是覆盖paint()。”

  • Swing GUI 对象应该事件分派线程上构建和操作。

  • 这里引用了一个没有明显延迟的更详细的示例。

于 2013-04-06T09:58:11.830 回答