2012-04-12 21 views
0

Kann ich einen Schieberegler in LWUIT erstellen, dh einen Balken, den der Benutzer bewegen kann, um einen Wert zu steuern?Schieberegler in LWUIT

bitte wenn jemand kann mit gutem Beispiel oder Tutorial helfen werde ich

Antwort

0

ich dies mit Bewegungskomponente getan haben und dem ich Ihnen liefert

public MotionComponent(final Image background, final Image button, 
      final int x, final int minX, final int maxX, final boolean 
      useStops, final int numStops, final int time) { 
     this.background = background; 
     this.button = button; 
     this.minX = minX; 
     this.maxX = maxX; 
     this.useStops = useStops; 
     this.numStops = numStops; 
     this.time = time; 
     positionX = (maxX - minX)/2; 
     this.moveToPoint = this.minX; 

     this.getStyle().setBgTransparency(0); 

     if (this.maxX < this.minX) { 
      int tmp = this.minX; 
      this.minX = this.maxX; 
      this.maxX = tmp; 
     } 

     if (button != null) { 
      this.maxX += (button.getWidth()/2); 
      this.minX += (button.getWidth()/2); 
     } 
    } 

    public void setStepSize(int step) { 
     this.stepSize = step; 
    } 

    public int getStepSize() { 
     return stepSize; 
    } 



    protected Dimension calcPreferredSize() { 
     //Style style = getStyle(); 
     Dimension dimension = null; 
     dimension = new Dimension(background.getWidth(), 36); 

     return dimension; 
    } 

    public void initComponent() { 
     getComponentForm().registerAnimated(this); 
    } 

    public void paint(Graphics g) { 


     if (background != null) { 
      g.drawImage(background, getX(), 
        (getHeight() - background.getHeight())/2); 
     } 
     if (button != null) { 
      g.drawImage(button, positionX + button.getWidth()/2, 
        (getHeight() - button.getHeight())/2); 
     } 

    } 

    public void keyPressed(int keyCode) { 
     super.keyPressed(keyCode); 
     switch (Display.getInstance().getGameAction(keyCode)) { 
     case Display.GAME_DOWN: 
     case Display.GAME_UP: 
      break; 
     case Display.GAME_LEFT: 
     case Display.GAME_RIGHT: 
      if (useStops && motionX == null) { 
       System.out.println("key pressed motion"); 
       motionX = Motion.createSplineMotion(positionX, 
         getDestPoint(positionX, numStops, minX, maxX, 
           Display.getInstance().getGameAction(keyCode)), time); 
       motionX.start(); 
      } else { 
       if (motionX == null) { 
        motionX = Motion.createSplineMotion(positionX, 
          moveStep(positionX, stepSize, 
            Display.getInstance().getGameAction(keyCode), minX, maxX), time); 
        motionX.start(); 
       } 
      } 
      System.out.println("key pressed"); 
      break; 
     default: 
      System.out.println("key default"); 
      return; 
     } 
    } 

    public void keyRepeated(int keyCode) { 
     super.keyRepeated(keyCode); 
     switch (Display.getInstance().getGameAction(keyCode)) { 
     case Display.GAME_DOWN: 
     case Display.GAME_UP: 
      break; 
     case Display.GAME_LEFT: 
     case Display.GAME_RIGHT: 
      if (useStops && motionX == null) { 
       motionX = Motion.createSplineMotion(positionX, 
         getDestPoint(positionX, numStops, minX, maxX, 
           Display.getInstance().getGameAction(keyCode)), time); 
       motionX.start(); 
      } 
      System.out.println("key Repeated"); 
      break; 
     default: 
      return; 
     } 
    } 

    private int moveStep(int x, int stepSize, int keyCode, int min, int 
      max) { 
     if (keyCode == Display.GAME_LEFT) { 
      if (x > min) { 
       if (x - stepSize < min) { 
        return min; 
       } else { 
        return x - stepSize; 
       } 
      } else { 
       return min; 
      } 
     } else { 
      if (x < max) { 
       if (x + stepSize > max) { 
        return max; 
       } else { 
        return x + stepSize; 
       } 
      } else { 
       return max; 
      } 
     } 
    } 

    /** 
    * returns a normalized value 
    * @return 
    */ 
    public float getValue() { 
     return ((positionX - minX)/(maxX - minX)); 
    } 

    public void pointerPressed(int x, int y) { 
     Rectangle absoluteBounds = new Rectangle(getAbsoluteX(), getAbsoluteY(), getBounds().getSize()); 
     if (absoluteBounds.contains(x, y)) { 
      System.out.println("pointerPressed " + x + " y " + y); 
      if (x != positionX) { 
       if (motionX == null) { 
        if (x < minX) { 
         motionX = Motion.createSplineMotion(positionX, 
           minX, time); 
        } else if (x > maxX) { 
         motionX = Motion.createSplineMotion(positionX, 
           maxX, time); 
        } else { 
         motionX = Motion.createSplineMotion(positionX, x, 
           time); 
        } 
        motionX.start(); 
       } else { 
        // do what? 
       } 
      } 
     }else{ 
      System.out.println("pointerPressed pointerPressed else " + x + " y " + y); 
      System.out.println("getBounds() x " + getBounds().getX() + ", y " + getBounds().getY() + ", w " + getBounds().getSize().getWidth() + ", h " + getBounds().getSize().getHeight()); 
     } 
    } 
    int draggedX, draggedY; 

    public void pointerDragged(int x, int y) { 
     Rectangle absoluteBounds = new Rectangle(getAbsoluteX(), getAbsoluteY(), getBounds().getSize()); 
     if (absoluteBounds.contains(x, y)) { 
      System.out.println("pointerDragged " + x + " y " + y); 
      draggedX = x; 
      draggedY = y; 
      if (motionX != null) { 
       //motionX.getValue() 
      } else { 
       positionX = x; 

       if (positionX < minX) { 
        positionX = minX; 
       } 

       if (positionX > maxX) { 
        positionX = maxX; 
       } 
      } 
     } else { 
      System.out.println("pointerDragged " + x + " y " + y); 
      System.out.println("pointerReleased "); 
      //when it exits bounds, call pointerReleased with the last 
      pointerReleased(draggedX, draggedY); 
     } 
    } 

    private int getDestPoint(int x, int numStops, int min, int max, int 
      direction) { 
     if (numStops < 2) { 
      if (direction == Display.GAME_LEFT) { 
       return min; 
      } else { 
       return max; 
      } 
     } 
     int distance = (max - min)/(numStops - 1); 

     if (direction == Display.GAME_LEFT) { 
      return moveToStopPoint(x - distance, numStops, min, max); 
     } else { 
      return moveToStopPoint(x + distance, numStops, min, max); 
     } 
    } 

    private int moveToStopPoint(int x, int numStops, int min, int max) { 
     int distance = max - min; 

     // will either go to min or max. 
     if (numStops < 2) { 
      if (x > min + (distance/2)) { 
       return max; 
      } else { 
       return min; 
      } 
     } 
     float averageDistance = distance/(numStops - 1); // 5 stops, 

     if (x > max) { 
      return max; 
     } 

     if (x > min && x <= (min + (int) (averageDistance/2))) { 
      return min; 
     } else if (x < max && x >= (max - (int) (averageDistance/2))) { 
      return max; 
     } else { 
      for (int i = 0; i < numStops; i++) { 
       int currentMin = min + (int) (i * averageDistance); 
       if (x >= currentMin && x < currentMin + (int) 
         averageDistance/2) { 
        return currentMin; 
       } else if (x >= currentMin + (int) averageDistance/2 && 
         x < currentMin + (int) averageDistance) { 
        return currentMin + (int) averageDistance; 
       } 
      } 
     } 
     return min; 
    } 

    public void pointerReleased(int x, int y) { 
     Rectangle absoluteBounds = new Rectangle(getAbsoluteX(), getAbsoluteY(), getBounds().getSize()); 
     if (absoluteBounds.contains(x, y)) { 
      System.out.println("pointerReleased " + x + " y " + y); 
      if (useStops) { 
       if (x > maxX) { 
        x = maxX; 
       } 
       if (x < minX) { 
        x = minX; 
       } 

       if (motionX != null) { 
        motionX = 
         Motion.createSplineMotion(motionX.getValue(), 
           moveToStopPoint(motionX.getValue(), numStops, minX, maxX), time); 
       } else { 
        motionX = Motion.createSplineMotion(x, 
          moveToStopPoint(x, numStops, minX, maxX), time); 
       } 
       motionX.start(); 
      } 
     } else { 
      // outside bounds. Should be caught previously with 
     } 
    } 

    public boolean animate() { 
     boolean val = false; 
     if (motionX != null) { 
      positionX = motionX.getValue(); 
      if (motionX.isFinished()) { 
       motionX = null; 
      } 
      val = true; 
     } 
     return val; 
    } 
} 

the out put of the above code is this and i think this is want you are searching. User can move the button left and right Pranav

1

In LWUIT 1.5 gibt es die Slider Klasse geschätzt.

+0

können Sie ein Tutorial oder Beispiel vorsehen, dass erkläre es bitte? –