0
A
回答
0
我已經做到了這一點與運動部件 和我將爲您提供
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;
}
}
Pranav
1
在LWUIT 1.5存在Slider
類幫助。
2
我只使用了無限模式。但你有一個教程,
相關問題
- 1. Android中的滑塊控件
- 2. Jquery滑塊控件
- 3. 滑塊控件的EventSetter WPF
- 4. .NET 4.0中的滑塊控件
- 5. XAML-Silverlight中的滑塊控件4
- 6. 範圍滑塊控件7
- 7. jquery滑塊控件問題
- 8. 雙滑塊控件.net
- 9. 「Spring」wpf滑塊控件
- 10. 自定義滑塊控件
- 11. Python tkinter滑塊控件
- 12. ExtJS 3.4滑塊控件
- 13. Flex滑塊 - 如何添加兩個滑塊的相同控件
- 14. 如何使滑塊控件的滑塊更寬? (使用MFC)
- 15. 在滑塊控件中訪問Thumb.DragCompleted
- 16. 滑塊控件和文本塊控件交互 - Metro應用
- 17. Bx滑塊多滑塊自定義控件
- 18. bxSlider - 移動滑塊內的控件
- 19. 自定義滑塊控件上的UIAccessibility
- 20. 像滑塊控件的javascript youtube
- 21. 布爾值的滑塊控件?
- 22. 自定義滑塊控件與wxPython的
- 23. jQueryUI的滑塊控件增強的jQuery UI的滑塊,點子實現問題
- 24. 編輯控件顯示滑塊控件位置的讀數
- 25. YUI API雙滑塊控件問題
- 26. 滑塊控件行爲變化
- 27. Picturebox滑塊控件透明度
- 28. jQuery相機滑塊鍵盤控件
- 29. jquery滑塊控件和自動滾動
- 30. 修改jQuery UI滑塊控件
你可以提供一個教程或例子請解釋一下嗎? – 2012-04-12 14:34:25