📄 component.java.svn-base
字号:
}
/**
* Returns the Component Form or null if this Component
* is not added yet to a form
*
* @return the Component Form
*/
public Form getComponentForm() {
Form retVal = null;
Component parent = getParent();
if (parent != null) {
retVal = parent.getComponentForm();
}
return retVal;
}
/**
* Repaint the given component to the screen
*
* @param cmp the given component on the screen
*/
void repaint(Component cmp) {
if (isCellRenderer() || cmp.getWidth() <= 0 || cmp.getHeight() <= 0) {
return;
}
// null parent repaint can happen when a component is removed and modified which
// is common ofr a popup
Component parent = getParent();
if (parent != null) {
parent.repaint(cmp);
}
}
/**
* Repaint this Component, the repaint call causes a callback of the paint
* method on the event dispatch thread.
*
* @see Display
*/
public void repaint() {
dirtyRegion = null;
repaint(this);
}
public void repaint(int x, int y, int w, int h) {
if (dirtyRegion == null) {
dirtyRegion = new Rectangle(x, y, w, h);
} else {
Dimension size = dirtyRegion.getSize();
int x1 = Math.min(dirtyRegion.getX(), x);
int y1 = Math.min(dirtyRegion.getY(), y);
int x2 = Math.max(x + w, dirtyRegion.getX() + size.getWidth());
int y2 = Math.max(y + h, dirtyRegion.getY() + size.getHeight());
dirtyRegion.setX(x1);
dirtyRegion.setY(y1);
size.setWidth(x2 - x1);
size.setHeight(y2 - y1);
}
repaint(this);
}
/**
* If this Component is focused, the key pressed event
* will call this method
*
* @param keyCode the key code value to indicate a physical key.
*/
public void keyPressed(int keyCode) {
}
/**
* If this Component is focused, the key released event
* will call this method
*
* @param keyCode the key code value to indicate a physical key.
*/
public void keyReleased(int keyCode) {
}
/**
* If this Component is focused, the key repeat event
* will call this method. Calls key pressed/released by default
*
* @param keyCode the key code value to indicate a physical key.
*/
public void keyRepeated(int keyCode) {
int game = Display.getInstance().getGameAction(keyCode);
if (game == Display.GAME_DOWN || game == Display.GAME_UP || game == Display.GAME_LEFT || game == Display.GAME_RIGHT) {
keyPressed(keyCode);
keyReleased(keyCode);
}
}
/**
* Allows defining the physics for the animation motion behavior directly
* by plugging in an alternative motion object
*
* @param motion new motion object
*/
private void setAnimationMotion(Motion motion) {
animationMotion = motion;
}
/**
* Allows defining the physics for the animation motion behavior directly
* by plugging in an alternative motion object
*
* @return the component motion object
*/
private Motion getAnimationMotion() {
return animationMotion;
}
/**
* Scroll animation speed in milliseconds allowing a developer to slow down or accelerate
* the smooth animation mode
*
* @return scroll animation speed in milliseconds
*/
public int getScrollAnimationSpeed() {
return animationSpeed;
}
/**
* Scroll animation speed in milliseconds allowing a developer to slow down or accelerate
* the smooth animation mode
*
* @param animationSpeed scroll animation speed in milliseconds
*/
public void setScrollAnimationSpeed(int animationSpeed) {
this.animationSpeed = animationSpeed;
}
/**
* Indicates that scrolling through the component should work as an animation
*
* @return whether this component use smooth scrolling
*/
public boolean isSmoothScrolling() {
return smoothScrolling;
}
/**
* Indicates that scrolling through the component should work as an animation
*
* @param smoothScrolling indicates if a component uses smooth scrolling
*/
public void setSmoothScrolling(boolean smoothScrolling) {
this.smoothScrolling = smoothScrolling;
Form f = getComponentForm();
if (f != null) {
if (smoothScrolling) {
f.registerAnimated(this);
} else {
f.deregisterAnimated(this);
}
}
}
/**
* If this Component is focused, the pointer dragged event
* will call this method
*
* @param x the pointer x coordinate
* @param y the pointer y coordinate
*/
public void pointerDragged(int x, int y) {
if (isScrollable() && isSmoothScrolling()) {
int axisValue;
if (isScrollableY()) {
axisValue = y;
} else {
axisValue = x;
}
if (!dragActivated) {
dragActivated = true;
beforeLastScrollY = axisValue;
lastScrollY = axisValue;
}
//save time and locations to create velocity when the
//pointer is released
long currentTime = System.currentTimeMillis();
if (currentTime != lastTime[(pLastDragged + lastTime.length + 1) % lastTime.length]) {
lastTime[pLastDragged] = System.currentTimeMillis();
lastDragged[pLastDragged] = axisValue;
pLastDragged = (++pLastDragged) % lastTime.length;
}
beforeLastScrollY = lastScrollY;
lastScrollY = axisValue;
// we drag inversly to get a feel of grabbing a physical screen
// and pulling it in the reverse direction of the drag
if (isScrollableY()) {
int scroll = getScrollY() + (beforeLastScrollY - axisValue);
if (scroll >= 0 && scroll < getPreferredH() - getHeight()) {
setScrollY(scroll);
}
} else {
int scroll = getScrollX() + (beforeLastScrollY - axisValue);
if (scroll >= 0 && scroll < getPreferredW() - getWidth()) {
setScrollX(scroll);
}
}
} else {
//try to find a scrollable element until you reach the Form
Component parent = getParent();
if (!(parent instanceof Form)) {
parent.pointerDragged(x, y);
}
}
}
private void initScrollMotion() {
Motion m = Motion.createLinearMotion(initialScrollY, destScrollY, getScrollAnimationSpeed());
setAnimationMotion(m);
m.start();
}
/**
* If this Component is focused, the pointer pressed event
* will call this method
*
* @param x the pointer x coordinate
* @param y the pointer y coordinate
*/
public void pointerPressed(int x, int y) {
// if(draggedMotion != null){
draggedMotion = null;
// }
}
/**
* If this Component is focused, the pointer released event
* will call this method
*
* @param x the pointer x coordinate
* @param y the pointer y coordinate
*/
public void pointerReleased(int x, int y) {
if (dragActivated) {
long currentTime = System.currentTimeMillis();
// replace x and y if this is an x scrolling container
if (!isScrollableY()) {
y = x;
}
if (currentTime != lastTime[(pLastDragged + lastTime.length + 1) % lastTime.length]) {
lastTime[pLastDragged] = System.currentTimeMillis();
lastDragged[pLastDragged] = y;
pLastDragged = (++pLastDragged) % lastTime.length;
}
float velocity = (float) (lastDragged[pLastDragged] - lastDragged[(pLastDragged + lastDragged.length + 1) % lastDragged.length]) / (lastTime[pLastDragged] - lastTime[(pLastDragged + lastTime.length + 1) % lastTime.length]);
velocity = velocity * -1;
if (isScrollableY()) {
draggedMotion = Motion.createFrictionMotion(scrollY, velocity, 0.0004f);
} else {
draggedMotion = Motion.createFrictionMotion(scrollX, velocity, 0.0004f);
}
draggedMotion.start();
dragActivated = false;
}
}
/**
* Returns the Component Style allowing us to manipulate the look of the
* component
*
* @return the component Style object
*/
public Style getStyle() {
return style;
}
/**
* Changes the Component Style by replacing the Component Style with the given Style
*
* @param style the component Style object
*/
public void setStyle(Style style) {
if (this.style != null) {
this.style.removeStyleListener(this);
}
this.style = style;
this.style.addStyleListener(this);
if (this.style.getBgPainter() == null) {
this.style.setBgPainter(new BGPainter());
}
setShouldCalcPreferredSize(true);
checkAnimation();
}
/**
* Changes the current component to the focused component, will work only
* for a component that belongs to a parent form.
*/
public void requestFocus() {
Form rootForm = getComponentForm();
if (rootForm != null) {
rootForm.requestFocus(this);
}
}
/**
* Overriden to return a useful value for debugging purposes
*
* @return a string representation of this component
*/
public String toString() {
String className = getClass().getName();
className = className.substring(className.lastIndexOf('.') + 1);
return className + "[" + paramString() + "]";
}
/**
* Returns a string representing the state of this component. This
* method is intended to be used only for debugging purposes, and the
* content and format of the returned string may vary between
* implementations. The returned string may be empty but may not be
* <code>null</code>.
*
* @return a string representation of this component's state
*/
protected String paramString() {
return "x=" + getX() + " y=" + getY() + " width=" + getWidth() + " height=" + getHeight();
}
/**
* Makes sure the component is up to date with the current style object
*/
public void refreshTheme() {
refreshTheme(getUIID());
}
/**
* Makes sure the component is up to date with the given UIID
*
* @param id The Style Id to update the Component with
*/
protected void refreshTheme(String id) {
if (style.isModified()) {
style.merge(UIManager.getInstance().getComponentStyle(id));
} else {
setStyle(UIManager.getInstance().getComponentStyle(id));
}
checkAnimation();
UIManager.getInstance().getLookAndFeel().bind(this);
}
/**
* Internal method indicating whether we are in the middle of a drag
*
* @return true if we are in the middle of a drag; otherwise false
*/
boolean isDragActivated() {
return dragActivated;
}
void checkAnimation() {
Image bgImage = getStyle().getBgImage();
if (bgImage != null && bgImage.isAnimation()) {
Form parent = getComponentForm();
if (parent != null) {
parent.registerAnimated(this);
}
}
}
/**
* @inheritDoc
*/
public boolean animate() {
Image bgImage = getStyle().getBgImage();
boolean animateBackground = bgImage != null && bgImage.isAnimation() && ((Animation) bgImage).animate();
Motion m = getAnimationMotion();
//preform regular scrolling
if (m != null && destScrollY != -1 && destScrollY != getScrollY()) {
// change the variable directly for efficiency both in removing redundant
// repaints and scroll checks
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -