Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用图像的Java JProgressBar_Java_Image_Swing_Jprogressbar_Game Development - Fatal编程技术网

使用图像的Java JProgressBar

使用图像的Java JProgressBar,java,image,swing,jprogressbar,game-development,Java,Image,Swing,Jprogressbar,Game Development,我正在用Java制作一个游戏,我正在使用JProgressBar作为健康栏。我想为JProgressBar使用图像(而不是颜色),但我一直无法做到这一点。我尝试过使用paint方法,paintComponent方法创建一个新类,但它不起作用。有人能帮我吗?不必创建自己的ProgressBarUI实现,您只需创建自己的 本例实质上是从单个药剂图像中生成“药剂条”,然后根据当前进度渲染子图像 它不会花太多的时间让它只显示整个药剂而不是 import java.awt.Color; import ja

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

不必创建自己的
ProgressBarUI
实现,您只需创建自己的

本例实质上是从单个药剂图像中生成“药剂条”,然后根据当前进度渲染子图像

它不会花太多的时间让它只显示整个药剂而不是

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.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.FontMetrics;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入java.io.IOException;
导入java.text.NumberFormat;
导入javax.imageio.imageio;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类进度窗格{
公共静态void main(字符串[]args){
新的ProgressPane();
}
公共进度窗格(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的GridBagLayout());
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私人浮动进度=1f;
私人缓冲药水;
专用缓冲区映像药剂棒;
公共测试窗格(){
试一试{
potion=ImageIO.read(getClass().getResource(“/potion.png”);
}捕获(IOEX异常){
例如printStackTrace();
}
设置不透明(假);
设置前景(颜色:蓝色);
挫折背景(颜色:灰色);
计时器计时器=新计时器(500,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
进度-=0.01;
如果(进度0&&getHeight()>0){
FontMetrics fm=getFontMetrics(getFont());
int height=Math.max(50,fm.getHeight()+4);
potionBar=新的BuffereImage(getWidth()-4,高度,BuffereImage.TYPE_INT_ARGB);
Graphics2D g2d=potionBar.createGraphics();
int x=0;
int y=(height-potion.getHeight())/2;
而(x
如果您只是在介绍它,为什么不直接使用JLabel?我是否可以设置JLabel的大小,使其看起来像一个健康栏?为了更快地获得更好的帮助,请发布一条。+1 ooh我喜欢…:)很适合玩游戏,更好的是,我不必编写它,只需将其撕开:P有可能使前景成为另一幅图像吗?因此,红色药剂图像会被蓝色药剂图像或类似图像重叠?我意识到前景已经是一幅
缓冲图像
,但它被构造为一幅空白图像(或者看起来是这样)。