如何根据Graphics2D中的百分比绘制圆的一部分? [英] How to draw portions of circles based on percentages in Graphics2D?

查看:297
本文介绍了如何根据Graphics2D中的百分比绘制圆的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一名自上而下的2D射手制作一个圆形健康栏,并希望只有在graphics2D中才能做到这一点的最佳方法。想知道画出许多弧线还是仅仅使用图像是最好的。 解决方案

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

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



你可以倒退



即使开始在不同的角度。



通过一点点的工作,你可以减少延长,所以你可以有一个开放的进展,例如,从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();

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

JFrame框架= 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);
}
});
}

公共类ProgressPane扩展JPanel {

私人浮动进度;
private float startAt = 270;
私有浮动方向= -1;
私人定时器定时器;

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);
}
});

$ b $ public void setProgress(float progress){
if(progress< 0.0){
progress = 0;
} else else if(progress> 1f){
progress = 1f;
}
this.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())*方向;
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();
}
}

}


I am working on a circular health bar for a top-down 2d shooter and would like some opinions of the best way to do this in only graphics2D. Wondering if drawing many arcs or just using images would be best.

解决方案

The answer depends on what you want to achieve...

This is a very basic example...

You can go backwards

Even start at a different angle.

With a little bit of work you could reduce the extended so you could have an "open" progress, for example, starting at 245 degrees with a total extent of 270 degrees if you wanted to

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();
        }
    }

}

这篇关于如何根据Graphics2D中的百分比绘制圆的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆