2 Replies Latest reply on Oct 9, 2012 10:28 PM by KonTiki

    Slow Opening Of GUI Class . . .

      A GUI class for drawing simple forms that I wrote uses both ActionListener and MouseListener + MouseMotionListener interfaces.
      The ActionListener is used in conjunction with a number of buttons which govern the shape that will be drawn.
      The MouseListener + MouseMotionListener interfaces are used with the mouse to position and scale the shape that is drawn.

      The class is quite short but still, it takes a long time (sometimes never) for the JFrame window to open.
      Here's the code :
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      public class RectangleBox extends JFrame implements ActionListener, MouseListener, MouseMotionListener
           // Attributes :
           // ----------
           // Graphical Attributes :
           private JTextArea userInstruction = new JTextArea(2, 60);
           private JButton rectButton = new JButton("Rectangle");
           private JButton ovalButton = new JButton("Oval");
           private JButton lineButton = new JButton("Line");
           private String figure; // This variable is allowed the values, "Rectangle", "Oval" and "Line".
           private int x1Pos;
           private int y1Pos;
           private int x2Pos;
           private int y2Pos;
           private boolean mouseDown;
           // Methods :
           // -------
           // Constructor :
           // -----------
           public RectangleBox()
                // GUI parameters first :
                setLayout(new FlowLayout());
                setLocation(300, 300);
                setSize(800, 800);          
                // Colour buttons and add them to the frame :
                // Add text area :
                // Add action listeners :
                // Add mouse reactors :
                // Write instructions to user in text area and add this text area to the frame :
                userInstruction.setText("Click one of the buttons for the shape you want." + "\n" +
                                        "Then scale that shape with the mouse to any desired size.");
                // Better to have visibility instruction placed at the end of constructor ??
           public void paint(Graphics g)
                if (figure == "Rectangle")
                     g.drawRect(x1Pos, y1Pos, x2Pos-x1Pos, y2Pos - y1Pos);
                if (figure == "Oval")
                     g.drawOval(x1Pos, y1Pos, x2Pos-x1Pos, y2Pos - y1Pos);
                if (figure == "Line")
                     g.drawLine(x1Pos, y1Pos, x2Pos, y2Pos);
           public void actionPerformed(ActionEvent e)
                if (e.getSource() == rectButton)
                     figure = "Rectangle";
                if (e.getSource() == ovalButton)
                     figure = "Oval";
                if (e.getSource() == lineButton)
                     figure = "Line";
           public void mouseDragged(MouseEvent e)      
                x2Pos = e.getX();
                y2Pos = e.getY();
           public void mouseMoved(MouseEvent e) 
                x1Pos = e.getX();
                y1Pos = e.getY();
           public void mouseClicked(MouseEvent e) 
                x1Pos = e.getX();
                y1Pos = e.getY();
                x2Pos = x1Pos;
                y2Pos = y1Pos;
           public void mouseEntered(MouseEvent e) 
           public void mouseExited(MouseEvent e) 
           public void mousePressed(MouseEvent e) 
                mouseDown = true;
           public void mouseReleased(MouseEvent e) 
                mouseDown = false;

      Now, why is this GUI taking so long to open ?

      Edited by: KonTiki on 09-Oct-2012 15:16
        • 1. Re: Slow Opening Of GUI Class . . .
          The Problem you recognized is most likely the early call to <tt>setVisible(true)</tt>. It would be better to move that call to the ent of the constructor or to <tt>main</tt>.

          But there are a lot more problematic things here:
          <h4>Don't have your class implementing Listener interfaces directly.</h4>
          The problem with that is, that the Interface implementation method must distinguish where the call comes from. If you create an anonymous inner class for each Listener instance there is no need to do so.
          Also the anonymous inner class could extent the respective default implementation of a Listener interface (usally *Adapter). This would avoid writing empty methods.

          <h4>Remove Coments not explaining code</h4>
          Comments are only usefull if they explain why the code does what it does. Any other comment just makes us scroll...

          <h4>Declare member references <tt>final</tt> by any chance</h4>
          This way you (or somebody else) cannot accidently set them to <tt>null</tt>.

          And finally please read this:
          SQL and PL/SQL FAQ
          especially the code formatting part...


          Edited by: TPD Opitz-Consulting com on 09.10.2012 19:57
          1 person found this helpful
          • 2. Re: Slow Opening Of GUI Class . . .
            Thanks for your quick response.

            I'm also grateful for the other suggestions on good coding practice and posting formatting and etiquette.

            One thing I noticed was that while moving the setVisible instruction to the end did not help much, the most likely cause of my delay issue - based on my own tinkering with the code - seems to be the size of the JFrame window.
            At its original size of 800 vertically - plus the location vertical offset of 300 - it went beyond the bottom of my screen.
            When I reduced the window size to 500 vertically, it was much faster.

            Again, thanks and good wishes for a pleasant autumn.