Canvas 類是Java AWT 的一部分。畫布是一個空白的矩形區域,用戶可以在其中繪製或捕獲用戶的輸入。 Canvas類繼承了Component類。
Canvas類的構造函數有:
- Canvas():創建一個新的空白畫布。
- 畫布(圖形配置c):創建具有指定圖形配置的新畫布。
Commonly used Methods in Canvas Class
方法 | 解釋 |
---|---|
addNotify() | 創建畫布的對等點。 |
createBufferStrategy(int n) | 在此組件上為 multi-buffering 創建新策略。 |
createBufferStrategy(int n, BufferCapabilities c) | 在此組件上創建 multi-buffering 的新策略,並具有所需的緩衝區函數 |
getBufferStrategy() | 返回此組件使用的BufferStrategy。 |
paint(Graphics g) | 繪製該組件。 |
update(Graphics g) | 更新此畫布。 |
以下示例程序旨在說明Canvas類的使用:
- 程序1:創建畫布並在畫布上繪製。
Java
// Java Program to create a to create
// a canvas and paint the canvas
import java.awt.*;
import javax.swing.*;
class canvas extends JFrame {
// constructor
canvas()
{
super("canvas");
// create a empty canvas
Canvas c = new Canvas() {
// paint the canvas
public void paint(Graphics g)
{
// set color to red
g.setColor(Color.red);
// set Font
g.setFont(new Font("Bold", 1, 20));
// draw a string
g.drawString("This is a canvas", 100, 100);
}
};
// set background
c.setBackground(Color.black);
add(c);
setSize(400, 300);
show();
}
// Main Method
public static void main(String args[])
{
canvas c = new canvas();
}
}
- Output:
- 程序2:創建畫布並向畫布添加鼠標偵聽器(在畫布上單擊或拖動鼠標的位置將出現一個半徑為 5 的圓)。
Java
// Java Program to create a
// canvas and mouse listener to the
// canvas ( a circle of radius 5 will appear
// at the points where mouse are clicked or
// dragged on the canvas)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class canvas extends JFrame implements MouseListener, MouseMotionListener {
// create a canvas
Canvas c;
// constructor
canvas()
{
super("canvas");
// create a empty canvas
c = new Canvas() {
public void paint(Graphics g)
{
}
};
// set background
c.setBackground(Color.black);
// add mouse listener
c.addMouseListener(this);
c.addMouseMotionListener(this);
add(c);
setSize(400, 300);
show();
}
// mouse listener and mouse motion listener methods
public void mouseClicked(MouseEvent e)
{
Graphics g = c.getGraphics();
g.setColor(Color.red);
// get X and y position
int x, y;
x = e.getX();
y = e.getY();
// draw a Oval at the point
// where mouse is moved
g.fillOval(x, y, 5, 5);
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
Graphics g = c.getGraphics();
g.setColor(Color.red);
// get X and y position
int x, y;
x = e.getX();
y = e.getY();
// draw a Oval at the point where mouse is moved
g.fillOval(x, y, 5, 5);
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
// main class
public static void main(String args[])
{
canvas c = new canvas();
}
}
- Output:
參考:https://docs.oracle.com/javase/7/docs/api/java/awt/Canvas.html
相關用法
- Java AWT CardLayout用法及代碼示例
- Java AWT Choice用法及代碼示例
- Java AWT Color用法及代碼示例
- Java AWT Dimension用法及代碼示例
- Java AWT MenuShortcut用法及代碼示例
- Java AWT SpringLayout用法及代碼示例
- Java AWT Desktop用法及代碼示例
- Java AWT BoxLayout用法及代碼示例
- Java AWT GridBagLayout用法及代碼示例
- Java AWT GridLayout用法及代碼示例
- Java AWT BorderLayout用法及代碼示例
- Java ArrayList add()用法及代碼示例
- Java ArrayList addAll()用法及代碼示例
- Java ArrayList clear()用法及代碼示例
- Java ArrayList clone()用法及代碼示例
- Java ArrayList contains()用法及代碼示例
- Java ArrayList get()用法及代碼示例
- Java ArrayList indexOf()用法及代碼示例
- Java ArrayList removeAll()用法及代碼示例
- Java ArrayList remove()用法及代碼示例
- Java ArrayList size()用法及代碼示例
- Java ArrayList isEmpty()用法及代碼示例
- Java ArrayList subList()用法及代碼示例
- Java ArrayList set()用法及代碼示例
注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 Java AWT | Canvas Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。