gpt4 book ai didi

java - 这个程序什么时候调用paint?另外,为什么它扩展 Canvas ?

转载 作者:行者123 更新时间:2023-11-29 03:35:14 25 4
gpt4 key购买 nike

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

public class GraphicsDemo1 extends Canvas
{
public void paint( Graphics g )
{
g.setColor(Color.green);
g.drawRect(50,20,100,200); // draw a rectangle
g.fillOval(160,20,100,200); // draw a filled-in oval
g.setColor(Color.blue);
g.fillRect(200,400,200,20); // a filled-in rectangle
g.drawOval(200,430,200,100);

g.setColor(Color.black);
g.drawString("Graphics are pretty neat.", 500, 100);
int x = getWidth() / 2;
int y = getHeight() / 2;
g.drawString("The first letter of this string is at (" + x + "," + y + ")", x, y);
}

public static void main( String[] args )
{
// You can change the title or size here if you want.
JFrame win = new JFrame("GraphicsDemo1");
win.setSize(800,600);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDemo1 canvas = new GraphicsDemo1();
win.add( canvas );
win.setVisible(true);
}
}

谢谢。 awt 和 swing 让我很困惑。

最佳答案

why does it extend canvas?

因为写它的人选择这样做。只有那些从 Component 扩展的类才能真正被绘制到屏幕上,并且只有当它们附加到有效的可见窗口时才可以绘制

When is paint called in this program?

绘画是 RepaintManager 的职责。它将决定何时需要重绘组件并在事件调度线程上安排重绘事件。这反过来会代表您调用(在您的情况下 update 调用)paint

您可能希望阅读 Painting in AWT and Swing有关该主题的更多详细信息

关于java - 这个程序什么时候调用paint?另外,为什么它扩展 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15940012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com