当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AWT Canvas用法及代码示例


Canvas 类是Java AWT 的一部分。画布是一个空白的矩形区域,用户可以在其中绘制或捕获用户的输入。 Canvas类继承了Component类。
Canvas类的构造函数有:

  1. Canvas():创建一个新的空白画布。
  2. 画布(图形配置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



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 Java AWT | Canvas Class。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。