1

I made a Frame which repaints itself when I click on it(also the new geometric figure is painted) but when I click rapidly It does not responds so fast, it needs like half a sec beetween clicks. What have I done wrong?

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


}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • by deafulr mosue event haven't any issue with prepared Object ready to be visible, not Objects to created on fly bysed on external resurces, nobody knows, because everything is hidden in `switch(r){...}`, then this question is hard to be answerable – mKorbel Apr 06 '13 at 07:53
  • @mKorbel in switch there is only creating of the objects which are made from primitive data types(fields are ints etc.) No hard stuff. – Yoda Apr 06 '13 at 08:02
  • whats happened in the case that you'll paiting to the JPanel with paintComponent instead of to JFrame by using paint – mKorbel Apr 06 '13 at 08:35

1 Answers1

2

Absent a complete example, I can only make several observations:

  • Instead of mouseClicked(), which fires when the mouse is released in the same component, you may want to respond to mousePressed().

  • Also consider MouseAdapter over implements MouseListener.

  • As mentioned here, "Swing programs should override paintComponent() instead of overriding paint()."

  • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

  • A more elaborate example with no perceptible latency is cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045