public class Canvas extends Component implements Accessible
Canvas
组件表示应用程序可以在其上绘制的屏幕的空白矩形区域,或者应用程序可以从中捕获来自用户的输入事件。
应用程序必须对Canvas
类进行子类化,以获得有用的功能,例如创建自定义组件。 必须paint
方法才能在画布上执行自定义图形。
Modifier and Type | Class and Description |
---|---|
protected class |
Canvas.AccessibleAWTCanvas
该类实现对
Canvas 类的辅助功能支持。
|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
Canvas()
构造一个新的画布。
|
Canvas(GraphicsConfiguration config)
给一个GraphicsConfiguration对象构造一个新的Canvas。
|
Modifier and Type | Method and Description |
---|---|
void |
addNotify()
创建画布的对等体。
|
void |
createBufferStrategy(int numBuffers)
为此组件创建一个新的多缓冲策略。
|
void |
createBufferStrategy(int numBuffers, BufferCapabilities caps)
在此组件上创建一个新策略,以实现所需的缓冲功能。
|
AccessibleContext |
getAccessibleContext()
获取与此Canvas关联的AccessibleContext。
|
BufferStrategy |
getBufferStrategy()
返回此
BufferStrategy 使用的BufferStrategy。
|
void |
paint(Graphics g)
画这幅画。
|
void |
update(Graphics g)
更新此画布。
|
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
public Canvas()
public Canvas(GraphicsConfiguration config)
config
- 对GraphicsConfiguration对象的引用。
GraphicsConfiguration
public void addNotify()
public void paint(Graphics g)
Canvas子类的大多数应用Canvas
都应该覆盖此方法,以便执行一些有用的操作(通常是自定义画布)。 默认操作只是清除画布。 覆盖此方法的应用程序不需要调用super.paint(g)。
paint
在类别
Component
g
- 指定的图形上下文
update(Graphics)
,
Component.paint(Graphics)
public void update(Graphics g)
响应于调用repaint
调用此方法。 画布首先通过填充背景颜色来清除,然后通过调用此画布的paint
方法完全重绘。 注意:覆盖此方法的应用程序应该调用super.update(g)或将上述功能合并到自己的代码中。
update
在类别
Component
g
- 指定的图形上下文
paint(Graphics)
,
Component.update(Graphics)
public void createBufferStrategy(int numBuffers)
BufferStrategy
与该数量的缓冲区。
首先尝试翻页策略,然后尝试使用加速缓冲区的策略策略。
最后,使用非加速的制动策略。
每次调用此方法时,将丢弃此组件的现有缓冲区策略。
numBuffers
- 要创建的缓冲区数,包括前缓冲区
IllegalArgumentException
- 如果numBuffers小于1。
IllegalStateException
- 如果组件不可显示
Component.isDisplayable()
,
getBufferStrategy()
public void createBufferStrategy(int numBuffers, BufferCapabilities caps) throws AWTException
每次调用此方法时,将丢弃此组件的现有缓冲区策略。
numBuffers
- 要创建的缓冲区数
caps
- 创建缓冲策略所需的功能;
不能null
AWTException
- 如果提供的功能不能被支持或满足;
这可能会发生,例如,如果当前没有足够的加速内存可用,或者如果指定了页面翻转,但不可能。
IllegalArgumentException
- 如果numBuffers小于1,或者如果cap是
null
getBufferStrategy()
public BufferStrategy getBufferStrategy()
BufferStrategy
使用的BufferStrategy。
如果BufferStrategy
尚未创建或已被处理,则此方法将返回null。
createBufferStrategy(int)
public AccessibleContext getAccessibleContext()
getAccessibleContext
在接口
Accessible
getAccessibleContext
在类别
Component