图形拱门不准确

发布于 2024-11-13 08:59:17 字数 91 浏览 2 评论 0原文

我必须用java画出精确的拱门。我当前正在使用 Graphics2D.fillArc()。问题是它只接受整数并且拱门不精确,我无法使拱门度平滑增加。有谁知道解决方法吗?

I have to draw precise archs in java. I am currenlty using Graphics2D.fillArc(). The problem is that it only accepts ints and the archs are not precise and i cant make the archs degree increase smoothly. Does anyone know a workaround this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过去的过去 2024-11-20 08:59:17

这是我使用 Arc2D 的 SSCCE

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Arc2D;
import javax.swing.*;

@SuppressWarnings("serial")
public class ChangingArcs extends JPanel {
   private static final Color ARC_FILL_COLOR = Color.RED;
   private static final int TIMER_DELAY = 20;
   private static final int ARC_X = 100;
   private static final int ARC_Y = 100;
   private static final int ARC_W = 500;
   private static final int ARC_H = 500;
   protected static final double DELTA_EXTEND = 0.5;
   private Arc2D arc;
   private double extend = 0;

   public ChangingArcs() {
      new Timer(TIMER_DELAY, new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            extend += DELTA_EXTEND;
            extend %= 360;
            double start = -extend/2;
            arc = new Arc2D.Double(ARC_X, ARC_Y, ARC_W, ARC_H, start, extend, Arc2D.PIE);
            repaint();
         }
      }).start();
   }

   public Dimension getPreferredSize() {
      return new Dimension(ARC_W + 2 * ARC_X, ARC_H + 2 * ARC_Y);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      if (arc != null) {
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2.setColor(ARC_FILL_COLOR);
         g2.fill(arc);
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("ChangingArcs");
      frame.getContentPane().add(new ChangingArcs());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Here's my SSCCE using Arc2D.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Arc2D;
import javax.swing.*;

@SuppressWarnings("serial")
public class ChangingArcs extends JPanel {
   private static final Color ARC_FILL_COLOR = Color.RED;
   private static final int TIMER_DELAY = 20;
   private static final int ARC_X = 100;
   private static final int ARC_Y = 100;
   private static final int ARC_W = 500;
   private static final int ARC_H = 500;
   protected static final double DELTA_EXTEND = 0.5;
   private Arc2D arc;
   private double extend = 0;

   public ChangingArcs() {
      new Timer(TIMER_DELAY, new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            extend += DELTA_EXTEND;
            extend %= 360;
            double start = -extend/2;
            arc = new Arc2D.Double(ARC_X, ARC_Y, ARC_W, ARC_H, start, extend, Arc2D.PIE);
            repaint();
         }
      }).start();
   }

   public Dimension getPreferredSize() {
      return new Dimension(ARC_W + 2 * ARC_X, ARC_H + 2 * ARC_Y);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      if (arc != null) {
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2.setColor(ARC_FILL_COLOR);
         g2.fill(arc);
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("ChangingArcs");
      frame.getContentPane().add(new ChangingArcs());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文