java.lang.Object
java.awt.Component
java.awt.Canvas
- 所有已实现的接口:
-
ImageObserver
,MenuContainer
,Serializable
,Accessible
一个
Canvas
组件表示屏幕上的一个空白矩形区域,应用程序可以在其中绘制或从中捕获用户的输入事件。
应用程序必须对Canvas
类进行子类化,以获得诸如创建自定义组件之类的有用功能。必须重写paint
方法以执行画布上的自定义图形。
- 自从:
- 1.0
- 参见:
-
Nested Class Summary
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Constructor Summary
ConstructorDescriptionCanvas()
构造一个新的Canvas。Canvas
(GraphicsConfiguration config) 给定一个GraphicsConfiguration对象构造一个新的Canvas。 -
Method Summary
Modifier and TypeMethodDescriptionvoid
创建画布的对等体。void
createBufferStrategy
(int numBuffers) 在此组件上创建一个新的多缓冲策略。void
createBufferStrategy
(int numBuffers, BufferCapabilities caps) 在此组件上使用所需的缓冲区功能创建一个新的多缓冲策略。获取与此Canvas关联的AccessibleContext。返回此组件使用的BufferStrategy
。void
绘制此画布。void
更新此画布。Methods declared in class java.awt.Component
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, 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, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
-
Constructor Details
-
Canvas
public Canvas()构造一个新的Canvas。 -
Canvas
给定一个GraphicsConfiguration对象构造一个新的Canvas。如果传入null,则将使用默认的GraphicsConfiguration。- 参数:
-
config
- GraphicsConfiguration对象的引用或null - 参见:
-
-
Method Details
-
addNotify
public void addNotify()创建画布的对等体。此对等体允许您更改画布的用户界面而不更改其功能。 -
paint
绘制此画布。大多数对
Canvas
进行子类化的应用程序应该重写此方法以执行一些有用的操作(通常是自定义绘制画布)。默认操作只是清除画布。重写此方法的应用程序不需要调用super.paint(g)。 -
update
更新此画布。此方法是响应调用
repaint
而调用的。首先通过填充背景颜色来清除画布,然后通过调用此画布的paint
方法完全重绘。注意:重写此方法的应用程序应该调用super.update(g)或将上述功能合并到自己的代码中。 -
createBufferStrategy
public void createBufferStrategy(int numBuffers) 在此组件上创建一个新的多缓冲策略。多缓冲对于渲染性能很有用。此方法尝试使用提供的缓冲区数量创建最佳策略。它将始终创建具有该数量缓冲区的BufferStrategy
。首先尝试页面翻转策略,然后尝试使用加速缓冲区的位块传输策略。最后,使用非加速位块传输策略。每次调用此方法时,此组件的现有缓冲策略将被丢弃。
- 参数:
-
numBuffers
- 要创建的缓冲区数量,包括前缓冲区 - 抛出:
-
IllegalArgumentException
- 如果numBuffers小于1。 -
IllegalStateException
- 如果组件不可显示 - 自从:
- 1.4
- 参见:
-
createBufferStrategy
使用所需的缓冲区功能在此组件上创建一个新的多缓冲策略。例如,如果只需要加速内存或页面翻转(由缓冲区功能指定)。每次调用此方法时,此组件的现有缓冲策略将被丢弃。
- 参数:
-
numBuffers
- 要创建的缓冲区数量 -
caps
- 创建缓冲策略所需的功能;不能为null
- 抛出:
-
AWTException
- 如果无法支持或满足提供的功能;例如,如果当前可用的加速内存不足,或者指定了页面翻转但不可能。 -
IllegalArgumentException
- 如果numBuffers小于1,或者caps为null
- 自从:
- 1.4
- 参见:
-
getBufferStrategy
返回此组件使用的BufferStrategy
。如果尚未创建BufferStrategy
或已被处理,则此方法将返回null。- 返回:
- 此组件使用的缓冲策略
- 自从:
- 1.4
- 参见:
-
getAccessibleContext
获取与此Canvas关联的AccessibleContext。对于画布,AccessibleContext采用AccessibleAWTCanvas的形式。如果需要,将创建一个新的AccessibleAWTCanvas实例。- 指定者:
-
getAccessibleContext
在接口Accessible
中 - 覆盖:
-
getAccessibleContext
在类Component
中 - 返回:
- 作为此Canvas的AccessibleContext的AccessibleAWTCanvas
- 自从:
- 1.3
-