Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i have done this with motion component and which i will provide you </p> <pre><code>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 &lt; 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 &amp;&amp; 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 &amp;&amp; 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 &gt; min) { if (x - stepSize &lt; min) { return min; } else { return x - stepSize; } } else { return min; } } else { if (x &lt; max) { if (x + stepSize &gt; 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 &lt; minX) { motionX = Motion.createSplineMotion(positionX, minX, time); } else if (x &gt; 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 &lt; minX) { positionX = minX; } if (positionX &gt; 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 &lt; 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 &lt; 2) { if (x &gt; min + (distance / 2)) { return max; } else { return min; } } float averageDistance = distance / (numStops - 1); // 5 stops, if (x &gt; max) { return max; } if (x &gt; min &amp;&amp; x &lt;= (min + (int) (averageDistance / 2))) { return min; } else if (x &lt; max &amp;&amp; x &gt;= (max - (int) (averageDistance / 2))) { return max; } else { for (int i = 0; i &lt; numStops; i++) { int currentMin = min + (int) (i * averageDistance); if (x &gt;= currentMin &amp;&amp; x &lt; currentMin + (int) averageDistance / 2) { return currentMin; } else if (x &gt;= currentMin + (int) averageDistance / 2 &amp;&amp; x &lt; 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 &gt; maxX) { x = maxX; } if (x &lt; 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; } } </code></pre> <p><img src="https://i.stack.imgur.com/w7SJd.png" alt="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</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload