java awt画布_Java AWT画布

Java AWT Canvas是一个GUI组件,用于在屏幕上创建矩形区域进行绘图和接收用户输入。通过实现MouseListener接口,可以捕捉用户与Canvas的交互事件,如点击、移动和拖动。在paint方法中利用Graphics类进行绘制操作,如改变颜色和绘制椭圆。通过setBackground()和setSize()方法可以设置Canvas的背景色和尺寸。
摘要由CSDN通过智能技术生成

java awt画布

Canvas is a GUI component that creates a rectangular box on the screen. It can be used to draw shapes or print text, it acts as a canvas. It can also be used to take user inputs. It is called so because it is like a canvas on which artists draw. With the correct programming, we can use the Canvas class to design a paint-like application, where the user can freely draw any shape or object he chooses.

画布是一个GUI组件,可在屏幕上创建一个矩形框。 它可以用来绘制形状或打印文本,它可以用作画布。 它也可以用来接受用户输入。 之所以这样称呼它,是因为它就像艺术家在其上绘制的画布。 通过正确的编程,我们可以使用Canvas类来设计类似绘画的应用程序,用户可以在其中自由绘制他选择的任何形状或对象。

We can use MouseListeners to trap user input when they interact with the Canvas object. The object generates MouseEvents whenever there is a mouse click, move, drag, etc. We can use these events and implement the MouseListener interface to utilize the full power of the Canvas object. We shall learn more about event handling in later sections.

当用户输入与Canvas对象交互时,我们可以使用MouseListener捕获用户输入。 只要有鼠标单击,移动,拖动等操作,该对象就会生成MouseEvent 。我们可以使用这些事件并实现MouseListener接口,以利用Canvas对象的全部功能。 我们将在后面的部分中详细了解事件处理。

Consider the following code -

考虑以下代码-

import java.awt.*;
import javax.swing.*;

public class CreateCanvas extends Canvas {
    CreateCanvas() {
        Frame f = new Frame();
        setBackground(Color.BLUE);
        setSize(300, 200);

        f.setLayout(null);
        f.setVisible(true);
        f.setSize(500, 500);

        f.add(this);
    }

    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.drawOval(150, 100, 70, 50);
        g.setColor(Color.RED);
        g.drawString("Java is the best", 100, 180);
    }

    public static void main(String[] args) {
        CreateCanvas ob = new CreateCanvas();

    }
}

Output

输出量

Java AWT Canvas

As can be seen, we have created a canvas object of size 300 pixels by 200 pixels. Our class extends the Canvas class and overwrites its paint method. Initially, we created a blank canvas. The paint method of the canvas class gets automatically called and need not be specially called by the programmer. The default paint method just clears the canvas and it is not necessary to overwrite this, but this method should be overwritten for any fruitful application of the canvas class.

可以看出,我们创建了一个大小为300像素乘200像素的画布对象。 我们的类扩展了Canvas类并覆盖其paint方法。 最初,我们创建了一块空白画布。 canvas类的paint方法将自动调用,并且不需要程序员特别调用。 默认的paint方法仅清除画布,没有必要覆盖它,但是对于canvas类的任何卓有成效的应用程序,都应覆盖此方法。

We set the background color of the canvas by calling the setBackground() method. Since our class extends the Canvas class, we need not specially create an object of Canvas class to call a method. We can set the size of the Canvas using the setSize() method, where we pass two integers – the width and height of the Canvas object.

我们通过调用setBackground()方法设置画布的背景色。 由于我们的类扩展了Canvas类,因此我们不需要专门创建Canvas类的对象来调用方法。 我们可以使用setSize()方法设置Canvas的大小,在此我们传递两个整数-Canvas对象的宽度高度

All the drawings and texts that are displayed on the canvas have to be written inside the paint method. We utilize the Graphics class here, which is created by default by the Canvas object itself and can be used to draw on the Canvas.

画布上显示的所有绘图和文本都必须在paint方法中编写。 我们在这里利用Graphics类,该类由Canvas对象本身默认创建,可用于在Canvas上绘制。

The setColor() method of the Graphics object changes the color which is used to draw. The drawOval() method draws the outline of an oval on the screen and requires 4 parameters – The x coordinate, y coordinate, width, and height.

Graphics对象的setColor()方法更改用于绘制的颜色。 drawOval()方法在屏幕上绘制椭圆形的轮廓,并需要4个参数-x坐标, y坐标, 宽度高度

翻译自: https://www.includehelp.com/java/awt-canvas.aspx

java awt画布

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值