Results 1 to 2 of 2

Thread: Can this java code be reduced?

  1. #1

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104

    Can this java code be reduced?

    Code:
    final class TraverseGrid{
      private final static int UP = 1;
      private final static int DOWN = 2;
      private final static int LEFT = 3;
      private final static int RIGHT = 4;
    
      private static Vector getAllFocusableComponents(Container cycleRoot){
        Vector components = new Vector();
        int iCtr;
    
        for (iCtr = 0; iCtr < cycleRoot.getComponentCount(); iCtr++) {
          if ((cycleRoot.getComponent(iCtr).isFocusable()) == true){
            components.addElement((Object)cycleRoot.getComponent(iCtr));
          }
        }
        return components;
      }
    
      private static Component mapComponent(Vector components, Component origin, int position){
        Component comp;
        Vector subset = new Vector();
        Vector row = new Vector();
        Point reference = origin.getLocation();
        int minimum = origin.getLocation().y;
        Component temp;
        int iCtr;
        for (iCtr = 0; iCtr < components.size(); iCtr++ ) {
          temp = (Component)components.elementAt(iCtr);
          if ((position == UP && origin.getLocation().y > temp.getLocation().y) ||
              (position == DOWN && origin.getLocation().y < temp.getLocation().y)) {
            if (minimum == origin.getLocation().y)
              minimum = temp.getLocation().y;
            else if ((position == UP && minimum < temp.getLocation().y)||
                     (position == DOWN && minimum > temp.getLocation().y))
              minimum = temp.getLocation().y;
            subset.addElement(temp);
          }
        }
    
        comp = origin;
        for (iCtr = 0; iCtr < subset.size(); iCtr++) {
          temp = (Component)subset.elementAt(iCtr);
          comp = temp;
          if (temp.getLocation().y == minimum &&  temp.getLocation().x >= origin.getLocation().x)
            break;
        }
        return comp;
      }
    
      private static Component getComponent(Vector components, Component theComponent, int iDirection){
        Component comp = theComponent;
        int iCtr;
    
        for (iCtr = 0; iCtr < components.size(); iCtr++) {
          if (theComponent.equals((Component)components.elementAt(iCtr)) == true){
            if (iDirection == RIGHT)
              iCtr++;
            else
              iCtr--;
            break;
          }
        }
        if (iCtr > -1 && iCtr < components.size())
          comp = (Component)components.elementAt(iCtr);
        return comp;
      }
      synchronized public static Component getAboveComponent(Container cycleRoot, Component origin){
        Vector comp = getAllFocusableComponents(cycleRoot);
        return mapComponent(comp, origin, UP);
      }
      synchronized static Component getBelowComponent(Container cycleRoot, Component origin){
        Vector comp = getAllFocusableComponents(cycleRoot);
        return mapComponent(comp, origin, DOWN);
      }
      synchronized static Component getNextComponent(Container container, Component origin){
        return getComponent(getAllFocusableComponents(container), origin, LEFT);
      }
      synchronized static Component getPreviousComponent(Container container, Component origin){
        return getComponent(getAllFocusableComponents(container), origin, RIGHT);
      }
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I doubt that the code itself can be made much faster.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width