📄 component.java
字号:
if (peer instanceof java.awt.peer.LightweightPeer) {
return parent.prepareImage(image, width, height, observer);
} else {
return (peer != null)
? peer.prepareImage(image, width, height, observer)
: getToolkit().prepareImage(image, width, height, observer);
}
}
/**
* Returns the status of the construction of a screen representation
* of the specified image.
* <p>
* This method does not cause the image to begin loading. An
* application must use the <code>prepareImage</code> method
* to force the loading of an image.
* <p>
* Information on the flags returned by this method can be found
* with the discussion of the <code>ImageObserver</code> interface.
* @param image the <code>Image</code> object whose status
* is being checked.
* @param observer the <code>ImageObserver</code>
* object to be notified as the image is being prepared.
* @return the bitwise inclusive <b>OR</b> of
* <code>ImageObserver</code> flags indicating what
* information about the image is currently available.
* @see java.awt.Component#prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
* @see java.awt.Toolkit#checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
* @see java.awt.image.ImageObserver
* @since JDK1.0
*/
public int checkImage(Image image, ImageObserver observer) {
return checkImage(image, -1, -1, observer);
}
/**
* Returns the status of the construction of a screen representation
* of the specified image.
* <p>
* This method does not cause the image to begin loading. An
* application must use the <code>prepareImage</code> method
* to force the loading of an image.
* <p>
* The <code>checkImage</code> method of <code>Component</code>
* calls its peer's <code>checkImage</code> method to calculate
* the flags. If this component does not yet have a peer, the
* component's toolkit's <code>checkImage</code> method is called
* instead.
* <p>
* Information on the flags returned by this method can be found
* with the discussion of the <code>ImageObserver</code> interface.
* @param image the <code>Image</code> object whose status
* is being checked.
* @param width the width of the scaled version
* whose status is to be checked.
* @param height the height of the scaled version
* whose status is to be checked.
* @param observer the <code>ImageObserver</code> object
* to be notified as the image is being prepared.
* @return the bitwise inclusive <b>OR</b> of
* <code>ImageObserver</code> flags indicating what
* information about the image is currently available.
* @see java.awt.Component#prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
* @see java.awt.Toolkit#checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
* @see java.awt.image.ImageObserver#_top_
* @since JDK1.0
*/
public int checkImage(Image image, int width, int height,
ImageObserver observer) {
ComponentPeer peer = this.peer;
if (peer instanceof java.awt.peer.LightweightPeer) {
return parent.checkImage(image, width, height, observer);
} else {
return (peer != null)
? peer.checkImage(image, width, height, observer)
: getToolkit().checkImage(image, width, height, observer);
}
}
/**
* Checks whether this component "contains" the specified point,
* where <code>x</code> and <code>y</code> are defined to be
* relative to the coordinate system of this component.
* @param x the <i>x</i> coordinate of the point.
* @param y the <i>y</i> coordinate of the point.
* @see java.awt.Component#getComponentAt(int, int)
* @since JDK1.1
*/
public boolean contains(int x, int y) {
return inside(x, y);
}
/**
* @deprecated As of JDK version 1.1,
* replaced by contains(int, int).
*/
public boolean inside(int x, int y) {
return (x >= 0) && (x < width) && (y >= 0) && (y < height);
}
/**
* Checks whether this component "contains" the specified point,
* where the point's <i>x</i> and <i>y</i> coordinates are defined
* to be relative to the coordinate system of this component.
* @param p the point.
* @see java.awt.Component#getComponentAt(java.awt.Point)
* @since JDK1.1
*/
public boolean contains(Point p) {
return contains(p.x, p.y);
}
/**
* Determines if this component or one of its immediate
* subcomponents contains the (<i>x</i>, <i>y</i>) location,
* and if so, returns the containing component. This method only
* looks one level deep. If the point (<i>x</i>, <i>y</i>) is
* inside a subcomponent that itself has subcomponents, it does not
* go looking down the subcomponent tree.
* <p>
* The <code>locate</code> method of <code>Component</code> simply
* returns the component itself if the (<i>x</i>, <i>y</i>)
* coordinate location is inside its bounding box, and <code>null</code>
* otherwise.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @return the component or subcomponent that contains the
* (<i>x</i>, <i>y</i>) location;
* <code>null</code> if the location
* is outside this component.
* @see java.awt.Component#contains(int, int)
* @since JDK1.0
*/
public Component getComponentAt(int x, int y) {
return locate(x, y);
}
/**
* @deprecated As of JDK version 1.1,
* replaced by getComponentAt(int, int).
*/
public Component locate(int x, int y) {
return contains(x, y) ? this : null;
}
/**
* Returns the component or subcomponent that contains the
* specified point.
* @param p the point.
* @see java.awt.Component#contains
* @since JDK1.1
*/
public Component getComponentAt(Point p) {
return getComponentAt(p.x, p.y);
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>dispatchEvent(AWTEvent e)</code>.
*/
public void deliverEvent(Event e) {
postEvent(e);
}
/**
* Dispatches an event to this component or one of its sub components.
* @param e the event
*/
public final void dispatchEvent(AWTEvent e) {
dispatchEventImpl(e);
}
void dispatchEventImpl(AWTEvent e) {
int id = e.getID();
/*
* 1. Allow input methods to process the event
*/
if (areInputMethodsEnabled()
&& (
// Otherwise, we only pass on low-level events, because
// a) input methods shouldn't know about semantic events
// b) passing on the events takes time
// c) isConsumed() is always true for semantic events.
// We exclude paint events since they may be numerous and shouldn't matter.
(e instanceof ComponentEvent) && !(e instanceof PaintEvent))) {
InputContext inputContext = getInputContext();
if (inputContext != null) {
inputContext.dispatchEvent(e);
if (e.isConsumed()) {
return;
}
}
}
/*
* 2. Pre-process any special events before delivery
*/
switch(id) {
// Handling of the PAINT and UPDATE events is now done in the
// peer's handleEvent() method so the background can be cleared
// selectively for non-native components on Windows only.
// - Fred.Ecks@Eng.sun.com, 1-8-98
case FocusEvent.FOCUS_GAINED:
if (parent != null && !(this instanceof Window)) {
parent.setFocusOwner(this);
}
break;
case KeyEvent.KEY_PRESSED:
case KeyEvent.KEY_RELEASED:
Container p = (Container)((this instanceof Container) ? this : parent);
if (p != null) {
p.preProcessKeyEvent((KeyEvent)e);
if (e.isConsumed()) {
return;
}
}
break;
case MouseEvent.MOUSE_PRESSED:
// requestFocus();
break;
default:
break;
}
/*
* 3. Deliver event for normal processing
*/
if (newEventsOnly) {
// Filtering needs to really be moved to happen at a lower
// level in order to get maximum performance gain; it is
// here temporarily to ensure the API spec is honored.
//
if (eventEnabled(e)) {
processEvent(e);
}
} else if (!(e instanceof MouseEvent && !postsOldMouseEvents())) {
//
// backward compatibility
//
Event olde = e.convertToOld();
if (olde != null) {
int key = olde.key;
int modifiers = olde.modifiers;
postEvent(olde);
if (olde.isConsumed()) {
e.consume();
}
// if target changed key or modifier values, copy them
// back to original event
//
switch(olde.id) {
case Event.KEY_PRESS:
case Event.KEY_RELEASE:
case Event.KEY_ACTION:
case Event.KEY_ACTION_RELEASE:
if (olde.key != key) {
((KeyEvent)e).setKeyChar(olde.getKeyEventChar());
}
if (olde.modifiers != modifiers) {
((KeyEvent)e).setModifiers(olde.modifiers);
}
break;
default:
break;
}
}
}
/*
* 4. If no one has consumed a key event, propagate it
* up the containment hierarchy to ensure that menu shortcuts
* and keyboard traversal will work properly.
*/
if (!e.isConsumed() && e instanceof java.awt.event.KeyEvent) {
Container p = (Container)((this instanceof Container) ? this : parent);
if (p != null) {
p.postProcessKeyEvent((KeyEvent)e);
}
}
/*
* 5. Allow the peer to process the event
*/
if (peer != null) {
peer.handleEvent(e);
}
}
boolean areInputMethodsEnabled() {
// in 1.1.x, we assume input method support is required for all
// components that handle key events. There's no way to tell
// whether they're really interested in character input or just
// in keystrokes.
return (eventMask & AWTEvent.KEY_EVENT_MASK) != 0 || keyListener != null;
}
// REMIND: remove when filtering is handled at lower level
boolean eventEnabled(AWTEvent e) {
switch(e.id) {
case ComponentEvent.COMPONENT_MOVED:
case ComponentEvent.COMPONENT_RESIZED:
case ComponentEvent.COMPONENT_SHOWN:
case ComponentEvent.COMPONENT_HIDDEN:
if ((eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
componentListener != null) {
return true;
}
break;
case FocusEvent.FOCUS_GAINED:
case FocusEvent.FOCUS_LOST:
if ((eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0 ||
focusListener != null) {
return true;
}
break;
case KeyEvent.KEY_PRESSED:
case KeyEvent.KEY_RELEASED:
case KeyEvent.KEY_TYPED:
if ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 ||
keyListener != null) {
return true;
}
break;
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_RELEASED:
case MouseEvent.MOUSE_ENTERED:
case MouseEvent.MOUSE_EXITED:
case MouseEvent.MOUSE_CLICKED:
if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 ||
mouseListener != null) {
return true;
}
break;
case MouseEvent.MOUSE_MOVED:
case MouseEvent.MOUSE_DRAGGED:
if ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 ||
mouseMotionListener != null) {
return true;
}
break;
default:
break;
}
//
// Always pass on events defined by external programs.
//
if (e.id > AWTEvent.RESERVED_ID_MAX) {
return true;
}
return false;
}
/**
* @deprecated As of JDK version 1.1,
* replaced by dispatchEvent(AWTEvent).
*/
public boolean postEvent(Event e) {
ComponentPeer peer = this.peer;
if (handleEvent(e)) {
e.consume();
return true;
}
Component parent = this.parent;
int eventx = e.x;
int eventy = e.y;
if (parent != null) {
e.translate(x, y);
if (parent.postEvent(e)) {
e.consume();
return true;
}
// restore coords
e.x = eventx;
e.y = eventy;
}
return false;
}
// Event source interfaces
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -