【问题标题】:How to draw portions of circles based on percentages in Graphics2D?如何根据 Graphics2D 中的百分比绘制圆的一部分?
【发布时间】:2013-10-10 19:57:29
【问题描述】:

我正在为一个自上而下的 2D 射击游戏制作一个圆形健康栏,并希望对仅在 graphics2D 中执行此操作的最佳方式提出一些意见。想知道是画多条弧线还是只使用图像最好。

【问题讨论】:

  • 你的意思是像饼形/楔形?尝试使用闭包类型为 PIEArc2D
  • @Geobits - 如果您将其发布为答案,我会投赞成票。
  • 如果他确认他想要一个馅饼,我会的。 “圆的部分”有点模糊。
  • 我很佩服你的克制。除了饼图之外,我真的不明白“基于百分比的圆圈部分”可能意味着什么。
  • 我首先想到的是一个厚“环”的弧形截面。可能是因为我在手机上使用了类似于电池电量计的东西。不过,我想您仍然可以使用 Arc2D 来执行此操作并更改笔划宽度。

标签: java graphics graphics2d


【解决方案1】:

答案取决于你想要达到的目标......

这是一个非常基本的例子……

你可以倒退

甚至从不同的角度开始。

通过一些工作,您可以减少扩展,因此您可以获得“开放”进度,例如,如果您愿意,可以从 245 度开始,总范围为 270 度

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CircleProgress {

    public static void main(String[] args) {
        new CircleProgress();
    }

    public CircleProgress() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ProgressPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ProgressPane extends JPanel {

        private float progress;
        private float startAt = 270;
        private float direction = -1;
        private Timer timer;

        public ProgressPane() {
            timer = new Timer(80, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    float value = getProgress();
                    if (value >= 1f) {
                        timer.stop();
                    }
                    setProgress(value += 0.01);
                }
            });
        }

        public void setProgress(float progress) {
            if (progress < 0.0) {
                progress = 0;
            } else if (progress > 1f) {
                progress = 1f;
            }
            this.progress = progress;
            repaint();
        }

        public float getProgress() {
            return progress;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            float extent = (360f * getProgress()) * direction;
            int width = getWidth();
            int height = getHeight();
            int radius = Math.min(width, height);

            int x = (width - radius) / 2;
            int y = (height - radius) / 2;

            g2d.setColor(Color.YELLOW);
            Arc2D arc = new Arc2D.Double(x, y, radius, radius, startAt, extent, Arc2D.PIE);
            g2d.fill(arc);

            g2d.dispose();
        }
    }

}

【讨论】:

  • @DavidWallace 如果您喜欢,请查看this answer。 ;)
  • @MadProgrammer 无需道歉。等等..我应该为已经支持 your 的答案道歉吗?并不是说我可能会道歉,只是真的很好奇.. ;)
  • @MadProgrammer BTW - 如果我意识到这些图像将被“打碎”为只有几种颜色,我会使用纯色填充。它们(现在包括我自己的个人资料照片。)在“色块”中看起来很可怕.. :(
猜你喜欢
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多