簡體   English   中英

Java 圖形 - 如何在繪制調用之間添加延遲以創建動畫?

[英]Java graphics - how to add delay between paint calls to create animation?

我正在用 Java 設計一個簡單的分形程序,它首先繪制一個矩形,然后遞歸地繪制每個角的 1/4 大小的矩形。 這將在所有方向上重復和跨越,直到維度收斂到 0。

到目前為止,我的程序和設計已經完全正常運行; 但是,我正在添加我不確定如何做的補充。 當我運行程序時,分形在彈出的 JFrame 中顯示已經完成。 我想要做的是讓每個矩形單獨出現,每個矩形之間都有很短的延遲。

我的 main 方法有一個類,它只是實例化一個 GUI 對象並調用它的 run() 方法。 大多數情況下,所有相關代碼都存在於這兩個文件中。

在 GUI 類中

public GUI()
 {
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      fractal = new Fractal(this, screenSize.width, screenSize.height);

      this.setTitle("Fractals");
      this.setSize(screenSize.width, screenSize.height);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.getContentPane().add(fractal);
      this.setFocusable(true);
      this.setVisible(true);
 }

 public void run()
 {
      fractal.repaint();
 }

在分形類中

public Fractal(GUI gui, int width, int height)
 {
      this.gui = gui;
      this.screenWidth = width;
      this.screenHeight = height;
 }

 @Override
 protected void paintComponent(Graphics g)
 {
      super.paintComponent(g);

      /* Calculate coordinates and dimensions */
      int x = 3 * this.screenWidth / 8;
      int y = 3 * this.screenHeight / 8;
      int width = this.screenWidth / 4;
      int height = this.screenHeight / 4;

      /* Draw the center rectangle */
      g.setColor(Color.BLACK);
      g.fillRect(x, y, width, height);

      /* Adjust dimensions for next rectangles */
      width /= 2;
      height /= 2;

      /* Draw rectangles at the corners */
      drawTopLeft(g, x - width, y - height, width, height);
      drawBottomLeft(g, x - width, y + (2 * height), width, height);
      drawTopRight(g, x + (2 * width), y - height, width, height);
      drawBottomRight(g, x + (2 * width), y + (2 * height), width, height);
 }

 public void drawTopLeft(Graphics g, int x, int y, int width, int height)
 {
      /* Ensure the boundaries lay within the screen */
      if(x > 0 && y > 0)
      {
           /* Draw itself */
           g.fillRect(x, y, width, height);

           /* Adjust dimensions for next rectangles */
           width /= 2;
           height /= 2;

           /* Draw rectangles at the corners */
           if(width > 0 && height > 0)
           {
                drawTopLeft(g, x - width, y - height, width, height);
                drawBottomLeft(g, x - width, y + (2 * height), width, height);
                drawTopRight(g, x + (2 * width), y - height, width, height);
           }
      }
 }

 public void drawBottomLeft(Graphics g, int x, int y, int width, int height)
 {
      /* Ensure the boundaries lay within the screen */
      if(x > 0 && y + height < screenHeight)
      {
           /* Draw itself */
           g.fillRect(x, y, width, height);

           /* Adjust dimensions for next rectangles */
           width /= 2;
           height /= 2;

           /* Draw rectangles at the corners */
           if(width > 0 && height > 0)
           {
                drawTopLeft(g, x - width, y - height, width, height);
                drawBottomLeft(g, x - width, y + (2 * height), width, height);
                drawBottomRight(g, x + (2 * width), y + (2 * height), width, height);
           }
      }
 }

 public void drawTopRight(Graphics g, int x, int y, int width, int height)
 {
      /* Ensure the boundaries lay within the screen */
      if(x + width < screenWidth && y > 0)
      {
           /* Draw itself */
           g.fillRect(x, y, width, height);

           /* Adjust dimensions for next rectangles */
           width /= 2;
           height /= 2;

           /* Draw rectangles at the corners */
           if(width > 0 && height > 0)
           {
                drawTopLeft(g, x - width, y - height, width, height);
                drawTopRight(g, x + (2 * width), y - height, width, height);
                drawBottomRight(g, x + (2 * width), y + (2 * height), width, height);
           }
      }
 }

 public void drawBottomRight(Graphics g, int x, int y, int width, int height)
 {
      /* Ensure the boundaries lay within the screen */
      if(x + width < screenWidth && y + height < screenHeight)
      {
           /* Draw itself */
           g.fillRect(x, y, width, height);

           /* Adjust dimensions for next rectangles */
           width /= 2;
           height /= 2;

           /* Draw rectangles at the corners */
           if(width > 0 && height > 0)
           {
                drawBottomLeft(g, x - width, y + (2 * height), width, height);
                drawTopRight(g, x + (2 * width), y - height, width, height);
                drawBottomRight(g, x + (2 * width), y + (2 * height), width, height);
           }
      }
 }

這是輸出生成的內容。

分形生成

您需要計算paintComponent 方法之外的每個矩形。 每個后續的都應存儲在數組或列表或某種數據結構中。 然后調用 repaint 並休眠 1 秒(但不要在paintComponent 方法中休眠)。

然后,您的paintComponent 方法必須遍歷矩形列表並繪制每個矩形。 因此,每次調用 repaint 時,都會先繪制舊的,然后再繪制新的。

最重要的是,不要在 paintComponent 或在 EDT 中運行的任何其他方法中進行太多處理,否則您的應用程序將變得無響應或無法按預期工作。

還有一件事。 在離開該方法之前,您在paintComponent 中所做的任何繪圖都將不可見。

我之前沒有看到你的圖片。 如果要繪制曲線或對角線,請在繪制前在paintComponent 中設置以下內容。 它將使圖形平滑。

Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM