クリックすると自分自身を再描画するフレームを作成しましたが(新しい幾何学的図形も描画されます)、すばやくクリックすると応答が遅くなり、クリックごとに0.5秒ほど必要です。私は何を間違えましたか?
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();
}
}