第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Java AWT如何延遲繪制對(duì)象

Java AWT如何延遲繪制對(duì)象

暮色呼如 2022-10-26 16:55:55
我想每 2 秒繪制一個(gè)新的隨機(jī)形狀。我已經(jīng)有一個(gè)窗口,可以立即顯示一些形狀。我試圖弄亂 Timer 讓新的東西在幾秒鐘后出現(xiàn)在窗口中,但它不起作用,或者整個(gè)程序凍結(jié)。使用 Timer 是個(gè)好主意嗎?我應(yīng)該如何實(shí)施它,讓它發(fā)揮作用?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();    }}我還想畫(huà)一些隨機(jī)的形狀??梢詥幔瑸榇四康氖褂美L畫(huà)方法中的開(kāi)關(guān)?我會(huì)做一個(gè)隨機(jī)變量,如果它是 1,它會(huì)畫(huà)矩形,如果它是 2,它會(huì)畫(huà)橢圓形等等。
查看完整描述

2 回答

?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

首先,不要改變JFrame繪制的方式(換句話說(shuō),不要覆蓋paintComponent()JFrame)。創(chuàng)建一個(gè)擴(kuò)展類(lèi)JPanel并繪制它JPanel。其次,不要覆蓋paint()方法。覆蓋paintComponent()。第三,始終運(yùn)行 Swing 應(yīng)用程序,SwingUtilities.invokeLater()因?yàn)樗鼈儜?yīng)該在自己的名為 EDT(事件調(diào)度線程)的線程中運(yùn)行。最后,javax.swing.Timer就是你要找的。

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

預(yù)習(xí):

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));

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-10-26
?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

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



查看完整回答
反對(duì) 回復(fù) 2022-10-26
  • 2 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)