⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingcomponentpeer.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* SwingComponentPeer.java -- An abstract base class for Swing based peers   Copyright (C)  2006  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package gnu.java.awt.peer.swing;import java.awt.AWTEvent;import java.awt.AWTException;import java.awt.BufferCapabilities;import java.awt.Color;import java.awt.Component;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.GraphicsConfiguration;import java.awt.Image;import java.awt.Point;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.BufferCapabilities.FlipContents;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.PaintEvent;import java.awt.image.ColorModel;import java.awt.image.ImageObserver;import java.awt.image.ImageProducer;import java.awt.image.VolatileImage;import java.awt.peer.ComponentPeer;import java.awt.peer.ContainerPeer;/** * The base class for Swing based component peers. This provides the basic * functionality needed for Swing based component peers. Many methods are * implemented to forward to the Swing component. Others however forward * to the component's parent and expect the toplevel component peer to provide * a real implementation of it. These are for example the key methods * {@link #getGraphics()} and {@link #createImage(int, int)}, as well as * {@link #getLocationOnScreen()}. * * This class also provides the necesary hooks into the Swing painting and * event handling system. In order to achieve this, it traps paint, mouse and * key events in {@link #handleEvent(AWTEvent)} and calls some special methods * ({@link #peerPaint(Graphics)}, {@link #handleKeyEvent(KeyEvent)}, * {@link #handleMouseEvent(MouseEvent)} and * {@link #handleMouseMotionEvent(MouseEvent)}) that call the corresponding * Swing methods. * * @author Roman Kennke (kennke@aicas.com) */public class SwingComponentPeer  implements ComponentPeer{  /**   * The AWT component for this peer.   */  protected Component awtComponent;  /**   * The Swing component for this peer.   */  protected SwingComponent swingComponent;  /**   * Creates a SwingComponentPeer instance. Subclasses are expected to call   * this constructor and thereafter call {@link #init(Component, JComponent)}   * in order to setup the AWT and Swing components properly.   */  protected SwingComponentPeer()  {    // Nothing to do here.  }  /**   * Initializes the AWT and Swing component for this peer. It is expected that   * subclasses call this from within their constructor.   *   * @param awtComp the AWT component for this peer   * @param swingComp the Swing component for this peer   */  protected void init(Component awtComp, SwingComponent swingComp)  {    awtComponent = awtComp;    swingComponent = swingComp;  }  /**   * Returns the construction status of the specified image. This is called   * by {@link Component#checkImage(Image, int, int, ImageObserver)}.   *   * @param img the image   * @param width the width of the image   * @param height the height of the image   * @param ob the image observer to be notified of updates of the status   *   * @return a bitwise ORed set of ImageObserver flags   */  public int checkImage(Image img, int width, int height, ImageObserver ob)  {    return Toolkit.getDefaultToolkit().checkImage(img, width, height, ob);  }  /**   * Creates an image by starting the specified image producer. This is called   * by {@link Component#createImage(ImageProducer)}.   *   * @param prod the image producer to be used to create the image   *   * @return the created image   */  public Image createImage(ImageProducer prod)  {    Image image = Toolkit.getDefaultToolkit().createImage(prod);	return image;  }  /**   * Creates an empty image with the specified <code>width</code> and   * <code>height</code>.   *   * This is implemented to let the parent component create the image. This   * eventually goes up to the top-level component peer, which is then expected   * to deliver the image.   *   * @param width the width of the image to be created   * @param height the height of the image to be created   *   * @return the created image   */  public Image createImage(int width, int height)  {    Component parent = awtComponent.getParent();    ComponentPeer parentPeer = parent.getPeer();    return parentPeer.createImage(width, height);  }  /**   * Disables the component. This is called by {@link Component#disable()}.   */  public void disable()  {    if (swingComponent != null)      swingComponent.getJComponent().setEnabled(false);  }  /**   * Disposes the component peer. This should release all resources held by the   * peer. This is called when the component is no longer in use.   */  public void dispose()  {    awtComponent = null;    swingComponent = null;  }  /**   * Enables the component. This is called by {@link Component#enable()}.   */  public void enable()  {    if (swingComponent != null)      swingComponent.getJComponent().setEnabled(true);  }  /**   * Returns the color model of the component. This is currently not used.   *   * @return the color model of the component   */  public ColorModel getColorModel()  {    // FIXME: When this peer method will be used, we need to provide an    // implementation of this, probably forwarding to the toplevel peer, like    // in the other methods.    return null;  }  /**   * Returns the font metrics for the specified font. This is called by   * {@link Component#getFontMetrics(Font)}.   *   * This is implemented to query the font metrics from the parent component.   * This will eventually call the top-level component peer, which is then   * expected to deliver a font metrics object.   *   * @param f the font for which to query the font metrics   *   * @return the font metrics for the specified font   */  public FontMetrics getFontMetrics(Font f)  {    Component parent = awtComponent.getParent();    ComponentPeer parentPeer = parent.getPeer();    return parentPeer.getFontMetrics(f);  }  /**   * Returns a {@link Graphics} object suitable for drawing on this component.   * This is called by {@link Component#getGraphics()}.   *   * This is implemented to query the graphics from the parent component and   * adjust the clip and translation to match this component.   * This will eventually call the top-level component peer, which is then   * expected to deliver a graphics object.   *   * @return a graphics object suitable for drawing on this component   */  public Graphics getGraphics()  {    Component parent = awtComponent.getParent();    ComponentPeer parentPeer = parent.getPeer();    Graphics g = parentPeer.getGraphics();    g.translate(awtComponent.getX(), awtComponent.getY());    g.setClip(0, 0, awtComponent.getWidth(), awtComponent.getHeight());    return g;  }  /**   * Returns the location of this component in screen coordinates. This is   * called by {@link Component#getLocationOnScreen()}.   *   * This is implemented to query the parent component peer for its screen   * location and adds the offset of this component to it. This will eventually   * call the top-level component's peer, which is then expected to provide   * it's screen location.   *   * @return the location of this component in screen coordinates   */  public Point getLocationOnScreen()  {    Component parent = awtComponent.getParent();    ComponentPeer parentPeer = parent.getPeer();    Point location = parentPeer.getLocationOnScreen();    location.x += awtComponent.getX();    location.y += awtComponent.getY();    return location;  }  /**   * Returns the minimum size for the component. This is called by   * {@link Component#getMinimumSize()}.   *   * This is implemented to return the Swing component's minimum size.   *   * @return the minimum size for the component   */  public Dimension getMinimumSize()  {    Dimension retVal;    if (swingComponent != null)      retVal = swingComponent.getJComponent().getMinimumSize();    else      retVal = new Dimension(0, 0);    return retVal;  }  /**   * Returns the preferred size for the component. This is called by   * {@link Component#getPreferredSize()}.   *   * This is implemented to return the Swing component's preferred size.   *   * @return the preferred size for the component   */  public Dimension getPreferredSize()  {    Dimension retVal;    if (swingComponent != null)      retVal = swingComponent.getJComponent().getPreferredSize();    else      retVal = new Dimension(0, 0);    return retVal;  }  /**   * Returns the toolkit that created this peer.   *   * @return the toolkit that created this peer   */  public Toolkit getToolkit()  {    return Toolkit.getDefaultToolkit();  }  /**   * Handles the given event. This is called from   * {@link Component#dispatchEvent(AWTEvent)} to give the peer a chance to    * react to events for the component.   *   * @param e the event   */  public void handleEvent(AWTEvent e)  {    switch (e.getID())    {      case PaintEvent.UPDATE:      case PaintEvent.PAINT:        Graphics g = getGraphics();        Rectangle clip = ((PaintEvent)e).getUpdateRect();        g.clipRect(clip.x, clip.y, clip.width, clip.height);        //if (this instanceof LightweightPeer)        //  {            if (e.getID() == PaintEvent.UPDATE)              awtComponent.update(g);            else              awtComponent.paint(g);        //  }        // We paint the 'heavyweights' at last, so that they appear on top of        // everything else.        peerPaint(g);        g.dispose();        break;      case MouseEvent.MOUSE_PRESSED:      case MouseEvent.MOUSE_RELEASED:      case MouseEvent.MOUSE_CLICKED:      case MouseEvent.MOUSE_ENTERED:      case MouseEvent.MOUSE_EXITED:        handleMouseEvent((MouseEvent) e);        break;      case MouseEvent.MOUSE_MOVED:      case MouseEvent.MOUSE_DRAGGED:        handleMouseMotionEvent((MouseEvent) e);        break;      case KeyEvent.KEY_PRESSED:      case KeyEvent.KEY_RELEASED:      case KeyEvent.KEY_TYPED:        handleKeyEvent((KeyEvent) e);        break;      default:        // Other event types are not handled here.        break;    }  }  /**   * Makes the component invisible. This is called from   * {@link Component#hide()}.   *   * This is implemented to call setVisible(false) on the Swing component.   */  public void hide()  {    if (swingComponent != null)      swingComponent.getJComponent().setVisible(false);  }  /**   * Returns <code>true</code> if the component can receive keyboard input   * focus. This is called from {@link Component#isFocusTraversable()}.   *    * This is implemented to return isFocusable() from the Swing component.   *   * @specnote Part of the earlier 1.1 API, replaced by isFocusable().   */  public boolean isFocusTraversable()  {    return swingComponent != null ?             swingComponent.getJComponent().isFocusable() : false;  }  /**   * Returns <code>true</code> if the component can receive keyboard input   * focus. This is called from {@link Component#isFocusable()}.   *   * This is implemented to return isFocusable() from the Swing component.   */  public boolean isFocusable()  {    return swingComponent != null ?             swingComponent.getJComponent().isFocusable() : false;  }  /**   * Returns the minimum size for the component. This is called by   * {@link Component#minimumSize()}.   *   * This is implemented to return the Swing component's minimum size.   *   * @return the minimum size for the component   */  public Dimension minimumSize()  {    Dimension retVal;    if (swingComponent != null)      retVal = swingComponent.getJComponent().getMinimumSize();    else      retVal = new Dimension(0, 0);    return retVal;  }  /**   * Returns the preferred size for the component. This is called by   * {@link Component#getPreferredSize()}.   *   * This is implemented to return the Swing component's preferred size.   *   * @return the preferred size for the component   */  public Dimension preferredSize()  {    Dimension retVal;    if (swingComponent != null)      retVal = swingComponent.getJComponent().getPreferredSize();    else      retVal = new Dimension(0, 0);    return retVal;  }  /**   * Prepares an image for rendering on this component. This is called by   * {@link Component#prepareImage(Image, int, int, ImageObserver)}.   *   * @param img the image to prepare   * @param width the desired width of the rendered image   * @param height the desired height of the rendered image   * @param ob the image observer to be notified of updates in the preparation   *        process   *   * @return <code>true</code> if the image has been fully prepared,   *         <code>false</code> otherwise (in which case the image observer   *         receives updates)   */  public void paint(Graphics graphics)  {    // FIXME: I don't know what this method is supposed to do.  }  /**   * Prepares an image for rendering on this component. This is called by   * {@link Component#prepareImage(Image, int, int, ImageObserver)}.   *   * @param img the image to prepare   * @param width the desired width of the rendered image   * @param height the desired height of the rendered image   * @param ob the image observer to be notified of updates in the preparation   *        process   *   * @return <code>true</code> if the image has been fully prepared,   *         <code>false</code> otherwise (in which case the image observer   *         receives updates)   */  public boolean prepareImage(Image img, int width, int height, ImageObserver ob)  {    Component parent = awtComponent.getParent();    ComponentPeer parentPeer = parent.getPeer();    return parentPeer.prepareImage(img, width, height, ob);  }  public void print(Graphics graphics)  {    // FIXME: I don't know what this method is supposed to do.  }  /**   * Repaints the specified rectangle of this component. This is called from   * {@link Component#repaint(long, int, int, int, int)}.   *   * This is implemented to call repaint() on the Swing component.   *   * @param tm number of milliseconds to wait with repainting   * @param x the X coordinate of the upper left corner of the damaged rectangle   * @param y the Y coordinate of the upper left corner of the damaged rectangle

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -