Java JProgressBar使用Image [英] Java JProgressBar using Image

查看:133
本文介绍了Java JProgressBar使用Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java制作游戏,而我正在使用 JProgressBar 作为健康栏。我想为 JProgressBar 使用图像(而不是颜色),但我无法做到。我已经尝试使用paint方法, paintComponent 方法,创建一个新类,但它不起作用。有人可以帮帮我吗?

I am making a game in Java, and I am using a JProgressBar for a health bar. I want to use an image (instead of colours) for the JProgressBar, but I have not been able to do it. I have tried using the paint method, paintComponent method, making a new class, but it's not working. May someone please help me?

推荐答案

无需创建自己的 ProgressBarUI 实现,您可以简单地创建自己的...

Without going to the extent to creating your own ProgressBarUI implementation, you could simply create your own...

这个例子基本上是从一个药水图像中制作一个药水棒,然后呈现一个基于当前进度的子图像。

This example essentially makes a "bar of potions" out of a single potion image and then renders a sub image based on the current progress.

只需要显示整个药水而不需要太多...

It wouldn't take much to make it show only whole potions instead...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.NumberFormat;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ProgressPane {

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

    public ProgressPane() {
        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 GridBagLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private float progress = 1f;
        private BufferedImage potion;
        private BufferedImage potionBar;

        public TestPane() {
            try {
                potion = ImageIO.read(getClass().getResource("/Potion.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            setOpaque(false);

            setForeground(Color.BLUE);
            setBackground(Color.GRAY);

            Timer timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    progress -= 0.01;
                    if (progress <= 0.001) {
                        ((Timer) e.getSource()).stop();
                    }
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        public void invalidate() {
            super.invalidate();
            potionBar = null;
        }

        @Override
        public Dimension getPreferredSize() {
            FontMetrics fm = getFontMetrics(getFont());
            return new Dimension(200, Math.max(50, fm.getHeight() + 4));
        }

        protected void createPotionBar() {

            if (potionBar == null) {
                if (getWidth() > 0 && getHeight() > 0) {
                    FontMetrics fm = getFontMetrics(getFont());
                    int height = Math.max(50, fm.getHeight() + 4);
                    potionBar = new BufferedImage(getWidth() - 4, height, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g2d = potionBar.createGraphics();
                    int x = 0;
                    int y = (height - potion.getHeight()) / 2;
                    while (x < getWidth() - 4) {
                        g2d.drawImage(potion, x, y, this);
                        x += potion.getWidth();
                    }
                    g2d.dispose();
                }
            }
        }

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

            int width = getWidth() - 4;
            int height = getHeight() - 4;
            int x = 2;
            int y = 2;

            g.setColor(getBackground());
            g.fillRect(x, y, width, height);
            g.setColor(Color.BLACK);
            g.drawRect(x, y, width, height);

            int progressWidth = (int) (width * progress);
            BufferedImage progressImage = potionBar.getSubimage(0, 0, progressWidth, potionBar.getHeight());
            g.drawImage(progressImage, x, y, this);

            FontMetrics fm = g.getFontMetrics();
            String value = NumberFormat.getPercentInstance().format(progress);
            x = x + ((width - fm.stringWidth(value)) / 2);
            y = y + ((height - fm.getHeight()) / 2);

            g.setColor(Color.WHITE);
            g.drawString(value, x, y + fm.getAscent());

        }
    }
}

这篇关于Java JProgressBar使用Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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