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

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

如何在 Java Swing 中制作不不斷改變/閃爍顏色的簡單動畫形狀?

如何在 Java Swing 中制作不不斷改變/閃爍顏色的簡單動畫形狀?

手掌心 2023-09-13 15:27:36
我有一些為了好玩而編寫的代碼,用不同顏色繪制圓圈線。在不使用 Swing Timer 的情況下,圓圈會“一次性”繪制,并且它們的顏色會在紅色、綠色、藍色和黃色之間交替。因此,如果沒有計時器,代碼將按預(yù)期工作。然而,當(dāng)我嘗試制作動畫以便可以看到正在繪制的圓圈時,這意味著使用 Swing Timer 進行循環(huán)而不是 while/for 循環(huán),所有圓圈都具有相同的顏色,并保持四種顏色一起閃爍。我想問題是顏色與每個對象無關(guān),而是與整個框架相關(guān)聯(lián),因為我每隔幾毫秒更改一次框架的顏色,已經(jīng)繪制的圓圈不會像它們那樣保留其原始顏色while 循環(huán)。如果您看到任何解決方案,我們將不勝感激!public class Board extends JPanel {    private static final long serialVersionUID = 6676924870723702476L;    private Timer timer;    private final int DELAY = 30;    private ActionListener drawLine;    private Ellipse2D circ;    private ArrayList<Ellipse2D> circles = new ArrayList<>();    private double circDiam = 80;    private double circX = 0;    private double circY = 0;    private short circNum = 1;    public Board() {        drawLine = (ActionEvent e) -> {            repaint();            circX += circDiam;            if(circNum == 5)            {                circNum = 1;                        }        };        timer = new Timer(DELAY, drawLine);        timer.start();    }    private Color getCircColor(short circNum) {         switch(circNum) {            case 1:                return Color.blue;            case 2:                return Color.red;            case 3:                return Color.yellow;            case 4:                return Color.green;            default:                return Color.gray;        }    }    protected void paintComponent(Graphics g) {        super.paintComponent(g);        Dimension size = new Dimension(getWidth(), getHeight());        double width = size.getWidth();        double height = size.getHeight();        Graphics2D g2d = (Graphics2D) g;        RenderingHints rHints = new  RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        rHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);        g2d.setRenderingHints(rHints);
查看完整描述

2 回答

?
青春有我

TA貢獻1784條經(jīng)驗 獲得超8個贊

繪畫方法僅用于繪畫。它不應(yīng)該:

  1. 創(chuàng)建對象。也就是說,不要創(chuàng)建 Circle 對象。

  2. 停止定時器。

  3. 繪制代碼將簡單地遍歷 ArrayList 來繪制所有 Circlce 對象。

Timer 的 ActionListener 用于:

  1. 創(chuàng)建 Cirlce 對象并將它們一次一個地添加到 ArrayList 中。

  2. 確定每個對象的顏色

  3. 創(chuàng)建所需數(shù)量的對象后停止計時器。

所以基本上在你的 Board 類中你需要一個像addCircle(....).?這會將 Circle 對象添加到 ArrayList,然后repaint()對其自身進行調(diào)用。然后,您更改ActionListener代碼以創(chuàng)建 Circle 對象并調(diào)用該addCirle(...)方法。

我想問題是顏色并不與每個對象相關(guān)聯(lián)

正確的。您將需要一個自定義對象來進行繪畫。這個自定義對象將包含兩個屬性:

  1. 圓對象

  2. Circle 對象的顏色。

或者另一種選擇是直接繪制 BufferedImage,然后在面板中顯示 BufferedImage。


查看完整回答
反對 回復(fù) 2023-09-13
?
慕田峪4524236

TA貢獻1875條經(jīng)驗 獲得超5個贊

我得到了相同的結(jié)果,但有一個例外,代碼如下:


public class Board extends JPanel {


? ? private static final long serialVersionUID = 6676924870723702476L;


? ? private Timer timer;

? ? private final int DELAY = 30;


? ? private ActionListener drawLine;

? ? private RCircle circ;

? ? private ArrayList<RCircle> circles = new ArrayList<>();


? ? private double circDiam = 80;

? ? private double circX = 0;

? ? private double circY = 0;

? ? private short circNum = 1;


? ? private double height = 800;

? ? private double width = 800;


? ? public Board() {


? ? ? ? drawLine = (ActionEvent e) -> {


? ? ? ? ? ? if(circX >= width - circDiam)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? circY += circDiam;

? ? ? ? ? ? ? ? circX = 0;

? ? ? ? ? ? ? ? circDiam *= 0.865? ?;

? ? ? ? ? ? }


? ? ? ? ? ? if(circY >= height - circDiam) {

? ? ? ? ? ? ? ? timer.stop();

? ? ? ? ? ? }


? ? ? ? ? ? circ = new RCircle(circX, circY, circDiam, circNum);


? ? ? ? ? ? circNum++;


? ? ? ? ? ? addCircle();


? ? ? ? ? ? circX += circDiam;


? ? ? ? ? ? if(circNum == 5)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? circNum = 1;? ? ? ? ? ??

? ? ? ? ? ? }

? ? ? ? };


? ? ? ? timer = new Timer(DELAY, drawLine);

? ? ? ? timer.start();

? ? }


? ? private void addCircle() {

? ? ? ? circles.add(circ);


? ? ? ? repaint();

? ? }


? ? protected void paintComponent(Graphics g) {

? ? ? ? super.paintComponent(g);


? ? ? ? Graphics2D g2d = (Graphics2D) g;


? ? ? ? RenderingHints rHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

? ? ? ? rHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

? ? ? ? g2d.setRenderingHints(rHints);


? ? ? ? g2d.setColor(circ.getColor());


? ? ? ? for(RCircle circ : circles) {

? ? ? ? ? ? g2d.fill(circ.getCirc());

? ? ? ? }

? ? }


}



public class RCircle {


? ? private Ellipse2D circ;

? ? private Color color;


? ? public RCircle(double x, double y, double circDiam, short circNum) {

? ? ? ? ?this.circ = new Ellipse2D.Double(x, y, circDiam, circDiam);


? ? ? ? ?this.color = getCircColor(circNum);

? ? }


? ? private Color getCircColor(short circNum) {?

? ? ? ? switch(circNum) {

? ? ? ? ? ? case 1:

? ? ? ? ? ? ? ? return Color.blue;

? ? ? ? ? ? case 2:

? ? ? ? ? ? ? ? return Color.red;

? ? ? ? ? ? case 3:

? ? ? ? ? ? ? ? return Color.yellow;

? ? ? ? ? ? case 4:

? ? ? ? ? ? ? ? return Color.green;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? return Color.gray;

? ? ? ? }

? ? }


? ? public Ellipse2D getCirc() {

? ? ? ? return circ;

? ? }


? ? public void setCirc(Ellipse2D circ) {

? ? ? ? this.circ = circ;

? ? }


? ? public Color getColor() {

? ? ? ? return color;

? ? }


? ? public void setColor(Color color) {

? ? ? ? this.color = color;

? ? }


}



public class Execute extends JFrame {


? ? private static final long serialVersionUID = -3483390877436935589L;


? ? public Execute() {


? ? ? ? add(new Board());


? ? ? ? setSize(800, 800);

? ? ? ? setResizable(false);


? ? ? ? setLocationRelativeTo(null);

? ? ? ? setDefaultCloseOperation(EXIT_ON_CLOSE);

? ? }


? ? public static void main(String[] args) {


? ? ? ? EventQueue.invokeLater(() -> {

? ? ? ? ? ? Execute ex = new Execute();

? ? ? ? ? ? ex.setVisible(true);

? ? ? ? });

? ? }

}



查看完整回答
反對 回復(fù) 2023-09-13
  • 2 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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