亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java AWT如何延遲繪制對象

Java AWT如何延遲繪制對象

暮色呼如 2022-10-26 16:55:55
我想每 2 秒繪制一個新的隨機形狀。我已經有一個窗口,可以立即顯示一些形狀。我試圖弄亂 Timer 讓新的東西在幾秒鐘后出現在窗口中,但它不起作用,或者整個程序凍結。使用 Timer 是個好主意嗎?我應該如何實施它,讓它發揮作用?import javax.swing.*;import java.awt.*;import java.util.Random;class Window extends JFrame {    Random rand = new Random();    int x = rand.nextInt(1024);    int y = rand.nextInt(768);    int shape = rand.nextInt(2);    Window(){        setSize(1024,768);        setVisible(true);        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);    }    public void paint(Graphics g) {        super.paint(g);        g.setColor(new Color(0, 52, 255));        switch(shape) {            case 0:                g.fillOval(x, y, 50, 50);                break;            case 1:                g.fillRect(x,y,100,100);                break;        }        repaint();    }}public class Main {    public static void main(String[] args) {        Window window = new Window();    }}我還想畫一些隨機的形狀。可以嗎,為此目的使用繪畫方法中的開關?我會做一個隨機變量,如果它是 1,它會畫矩形,如果它是 2,它會畫橢圓形等等。
查看完整描述

2 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

首先,不要改變JFrame繪制的方式(換句話說,不要覆蓋paintComponent()JFrame)。創建一個擴展類JPanel并繪制它JPanel。其次,不要覆蓋paint()方法。覆蓋paintComponent()。第三,始終運行 Swing 應用程序,SwingUtilities.invokeLater()因為它們應該在自己的名為 EDT(事件調度線程)的線程中運行。最后,javax.swing.Timer就是你要找的。

看看這個例子。它每 1500 毫秒在隨機 X、Y 上繪制一個橢圓形。

預習:

https://i.stack.imgur.com/KGLff.gif

源代碼:


import java.awt.BorderLayout;

import java.awt.Graphics;


import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.Timer;


public class DrawShapes extends JFrame {

    private ShapePanel shape;


    public DrawShapes() {

        super("Random shapes");

        getContentPane().setLayout(new BorderLayout());

        getContentPane().add(shape = new ShapePanel(), BorderLayout.CENTER);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(500, 500);

        setLocationRelativeTo(null);


        initTimer();

    }


    private void initTimer() {

        Timer t = new Timer(1500, e -> {

            shape.randomizeXY();

            shape.repaint();

        });

        t.start();

    }


    public static class ShapePanel extends JPanel {

        private int x, y;


        public ShapePanel() {

            randomizeXY();

        }


        @Override

        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            g.fillOval(x, y, 10, 10);

        }


        public void randomizeXY() {

            x = (int) (Math.random() * 500);

            y = (int) (Math.random() * 500);

        }

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new DrawShapes().setVisible(true));

    }

}


查看完整回答
反對 回復 2022-10-26
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

首先,不要繼承 JFrame;而是將 JPanel 子類化,并將該面板放在 JFrame 中。其次,不要覆蓋paint() - 而是覆蓋paintComponent()。第三,創建一個 Swing Timer,并在其 actionPerformed() 方法中進行您想要的更改,然后調用 yourPanel.repaint()



查看完整回答
反對 回復 2022-10-26
  • 2 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號