📄 component.java
字号:
/**
* @return The height, in pixels, for this component.
*/
public int getHeight ()
{
return height;
}
/**
* @return The horizontalAlignment of the text in the label. It is one of
* <code>Graphics.LEFT</code>, <code>Graphics.HCENTER</code>, and <code>Graphics.RIGHT</code>.
*/
public int getHorizontalAlignment ()
{
return horizontalAlignment;
}
/**
* @param alignment is how the text in the label is justified. It is one of
* <code>Graphics.LEFT</code>, <code>Graphics.HCENTER</code>, and <code>Graphics.RIGHT</code>.
*/
public void setHorizontalAlignment (int alignment)
{
if ( (alignment != Graphics.LEFT) && (alignment != Graphics.HCENTER) && (alignment != Graphics.RIGHT) )
{
throw new IllegalArgumentException("setHorizontalAlignment only takes Graphics.LEFT, HCENTER, or RIGHT");
}
this.horizontalAlignment = alignment;
}
/**
* Sets if the component is currently shown on the screen or not.
* A call to <code>show(true)</code> must be made before <code>paint</code>.
* When the component is no longer visible call <code>show(false)</code>
* so that the component may clean up any resources.
*
* @param visible when <code>true</code> indicates the component is painted (or
* about to be) on the screen.
*/
public void show (boolean visible)
{
if ( this.visible != visible )
{
this.visible = visible;
// Raise an event for the component.
if ( visible )
{
showNotify();
}
else
{
hideNotify();
}
}
}
/**
* Returns if this component is shown on the screen now.
*
* @return <code>true</code> if this component is currently visible on the
* screen; <code>false</code> if not.
*/
public boolean isShown ()
{
return visible;
}
/**
* An event raised whenever the component is made visible on the screen.
* This is called before the <code>paintComponent</code> method.
* <p>
* The default implementation does nothing. Override it to initialize
* any resources required by the component.
*/
protected void showNotify ()
{
}
/**
* An event raised whenever the component is removed from the screen.
* <p>
* The default implementation does nothing. Override it to clean up
* any resources required by the component.
*/
protected void hideNotify ()
{
}
/**
* Forces this component to repaint itself.
*/
public void repaint ()
{
if ( isShown() && (screen != null) )
{
screen.repaint( x, y, width, height );
}
}
/**
* Called when a key is pressed. It can be identified using the
* constants defined in the <code>DeviceScreen</code> class. Note to
* receive key events the component must override <code>acceptsInput</code>
* to return <code>true</code>.
* <p>
* The default implementation does nothing. If a component requires
* keypad interaction, such as to enter text, it should override this
* method.
*
* @param keyCode is the key code of the key that was pressed.
*/
public void keyPressed (int keyCode)
{
}
/**
* Called when a key is repeated (held down). It can be identified using the
* constants defined in the <code>DeviceScreen</code> class. Note to
* receive key events the component must override <code>acceptsInput</code>
* to return <code>true</code>.
* <p>
* The default implementation does nothing. If a component requires
* keypad interaction, such as to enter text, it should override this
* method.
*
* @param keyCode is the key code of the key that was held down.
*/
public void keyRepeated (int keyCode)
{
}
/**
* Called when a key is released. It can be identified using the
* constants defined in the <code>DeviceScreen</code> class. Note to
* receive key events the component must override <code>acceptsInput</code>
* to return <code>true</code>.
* <p>
* The default implementation does nothing. If a component requires
* keypad interaction, such as to enter text, it should override this
* method.
*
* @param keyCode is the key code of the key that was released.
*/
public void keyReleased (int keyCode)
{
}
/**
* Called when the pointer is pressed.
*
* @param x is the horizontal location where the pointer was pressed
* relative to the top-left corner of the component.
* @param y is the vertical location where the pointer was pressed
* relative to the top-left corner of the component.
*/
public void pointerPressed (int x, int y)
{
}
/**
* Called when the pointer is released.
*
* @param x is the horizontal location where the pointer was released
* relative to the top-left corner of the component.
* @param y is the vertical location where the pointer was released
* relative to the top-left corner of the component.
*/
public void pointerReleased (int x, int y)
{
}
/**
* Called when the pointer is dragged.
*
* @param x is the horizontal location where the pointer was dragged
* relative to the top-left corner of the component.
* @param y is the vertical location where the pointer was dragged
* relative to the top-left corner of the component.
*/
public void pointerDragged (int x, int y)
{
}
/**
* Paints a rectangle used within a component. If the rectangle is
* <code>selected</code> it will have rounded edges and be highlighted. If it
* is not it will have square edges and be slightly inset from
* (<code>x</code>, <code>y</code>). The border color, highlight color, and inner
* part of the rectangle (background color) all come from the <code>theme</code>.
*
* @param g is the <code>Graphics</code> object to be used for rendering the item.
* @param theme is the application's theme. Use it to get fonts and colors.
* @param x is the left side of the box.
* @param y is the top of the box.
* @param width is the width, in pixels, to paint the component.
* @param height is the height, in pixels, to paint the component.
* @param selected is <code>true</code> when this components is currently selected
* and <code>false</code> when it is not.
* @return The offset, in pixels, of the interior of the box. This is the
* usuable area by a component: <code>(x + offset, y + offset,
* width - 2 * offset, height - 2 * offset)</code>.
*/
protected static int paintRect (Graphics g, Theme theme, int x, int y, int width, int height, boolean selected)
{
// Paint the border.
int rounding = height / 4;
if ( selected )
{
// Draw a thick, highlighted border.
int border = theme.getHighlightColor();
g.setColor( border );
g.fillRoundRect( 0, y, width, height, rounding, rounding );
}
// Draw a normal border.
int border = theme.getBorderColor();
g.setColor( border );
int bo = HIGHLIGHTED_BORDER_WIDTH - 1;
int bx = bo;
int by = y + bo;
int bs = 2 * bo;
int bw = width - bs;
int bh = height - bs;
g.fillRect( bx, by, bw, bh );
// Subtract out a rectangle from the middle.
int interior = theme.getBackgroundColor();
g.setColor( interior );
int offset = Math.max( HIGHLIGHTED_BORDER_WIDTH, rounding / 2 );
int rx = offset;
int ry = y + offset;
int rw = width - 2 * offset;
int rh = height - 2 * offset;
g.fillRect( rx, ry, rw, rh );
// Return the offset from the edges of the component to the inside.
return offset;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -