簡體   English   中英

Graphics.fillArc(); 工作不正常

[英]Graphics.fillArc(); is not working properly

我已經編寫了此Java代碼來繪制實心圓弧,該實心圓弧在從0到360度的每次循環迭代中端點角度均增加1,但這無法正常工作,請提供幫助。

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class A {

  public static void main(String arg[]) throws Exception {

      JFrame f = new JFrame();
      f.setExtendedState(JFrame.MAXIMIZED_BOTH);
      f.setUndecorated(true);
      f.setVisible(true);
      f.getContentPane().setBackground(Color.BLACK);
      f.setLayout(new FlowLayout());
      Circle c;

      for(int i=0; i<=360; i++) {
          c = new Circle(-i);
          f.add(c);
          Thread.sleep(6);
          f.getContentPane().remove(c);
          f.getContentPane().revalidate();
          f.getContentPane().repaint();
      }

  }
}

class Circle extends JPanel {

    int angle;

    public Circle(int angle) {
        this.angle=angle;
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillArc(50, 50, 100, 100, 90, angle);
    }
}

我不會列出您代碼中的所有錯誤。 我固定了大多數。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Scratch {

  public static void main(String arg[]) throws Exception {

      JFrame f = new JFrame();
      f.setSize(600, 600);
      f.setVisible(true);
      f.getContentPane().setBackground(Color.BLACK);
      f.setLayout(new BorderLayout());

      for(int i=0; i<=360; i++) {
          final int fi = i;
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  f.getContentPane().removeAll();
                  Circle c = new Circle(-fi);
                  f.add(c);
                  f.getContentPane().revalidate();
                  f.getContentPane().repaint();
              }
          });
          try {
              Thread.sleep(100);
          } catch (InterruptedException ie) {
          }
      }

  }
}

class Circle extends JPanel {

    int angle;

    public Circle(int angle) {
        this.angle=angle;
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(Color.RED);
        g.fillArc(50, 50, 100, 100, 0, angle);
    }
}

我建議您使用一個可以更新其圖像的組件,而不要刪除/添加其他組件。

暫無
暫無

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

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