繁体   English   中英

不知道如何调用绘画方法

[英]Dont know how to call paint method

嗨,我想知道如何调用绘画方法...

我是一个新手程序员,实际上只是在尝试绘画之类的事情。

我要制作的程序是游戏,其中有3个横档,游戏的目的是将不同大小的磁盘从左/右横档移动到右/左横档。

这是我的代码(没有完成的地方让我休息一下):

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int amount = 0;

    // get the amount of rectangles to draw to represent disks
    while (amount <= 1 && amount >= 5) {
        System.out.print("Please enter amount of discs to use(less than 7 more than one).");
        amount = scan.nextInt();            
    }
}

public void paint(Graphics g) {
    // draw a certain amount of rectangles in ascending order
    if (amount <= 1 && amount >= 5) {
        for (int i = 0; i < amount; i++) {
            g.fillRect(220 - (20 * i), 200 + (10 * i), 100 - (20 * i), 20);
        }
    }
}

创建对象后将首次调用paint方法。

要强制再次调用paint()方法,如果要传入新的Graphics对象,则可以调用update(Graphics g) ,但通常我建议使用repaint()方法,因为这样可以安排它被称为尽快。

您不需要调用它。 相反,您应该使用Java为您创建的主循环。

通常的方法是扩展JPanel (请参见以下问题: 如何使用Swing制作画布? )并覆盖paint()方法。

现在创建一个JFrame ,向其中添加新的UI组件并打开框架。 然后,Java将确保它被渲染。

我不是专家或其他可以教别人的人,但是您需要将要绘制的代码放到paintComponent(Graphics g)方法而不是paint上,然后调用repaint方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM