微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将图形从主jframe获取到我的对象

如何解决无法将图形从主jframe获取到我的对象

所以基本上我试图使时钟出现在我的jframe上,但是图像不会弹出。没有错误消息,并且未触发我的“失败”打印测试。我很感谢您的帮助,如果我愚蠢或失明,我们将感到抱歉

所以基本上,我试图使时钟出现在战场上,但也能够将图像更改为我制作的任何精灵(所有名称都从clock0.png到clock8.png)。当前时钟根本没有显示在战屏上,这是我需要修复的主要内容

时钟类:

package BattleScreen;

import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class Clock {

    BufferedImage img;
    ArrayList<BufferedImage> imgs = new ArrayList<BufferedImage>();
    Component c;
    Integer sprite;
    int width,height,xPos,yPos;
    
    public Clock() {
        width = 50;
        height = 50;
        xPos = 100;
        //xPos = BattleScreen.panel.getWidth() - width;
        yPos = 100;
        //yPos = BattleScreen.panel.getHeight() - height;
        
        c = BattleScreen.panel;
        
        try {
            Integer count = 0;
            while (count != 9) {
                imgs.add(ImageIO.read(new File("images/clock" + count + ".png")));
                count++;
            }
        } catch (IOException e) {
            e.printstacktrace();
        }
        
        this.setSprite(0);
    }
    
    void draw () {
        if (MainMenu.MainMenu.window.getGraphics() == null) {
            System.out.println("Failure");
        } else {
            MainMenu.MainMenu.window.getGraphics().drawImage(img,yPos,width,c);
        }
    }
    
    void update(Integer time,Integer timeEnd) {
        Double dou = (((double) time) / ((double) timeEnd) * 8);
        Integer inte = dou.intValue();
        setSprite(inte);
    }
    
    void setSprite(Integer inte) {
        img = imgs.get(inte);
        this.draw();
    }
}

BattleScreen类:

package BattleScreen;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class BattleScreen {

    public static JPanel panel;
    private static BattleBox battleBox;
    private static OpponentNameBox opponentNameBox;
    private static Clock clock;
    
    @SuppressWarnings("serial")
    public static void initialise() {
        panel = new JPanel() {
            @Override
            protected void paintComponent (Graphics g) {
                super.paintComponent(g);
                clock.draw();
            }
        };
        clock = new Clock();
        
        panel.setLayout(null);
        panel.setBounds(0,MainMenu.MainMenu.window.getWidth(),MainMenu.MainMenu.window.getHeight());
        panel.setBackground(Color.black);
        
        battleBox = new BattleBox();
        opponentNameBox = new OpponentNameBox();
        
        //paintClock();
        
        panel.add(battleBox);
        panel.add(opponentNameBox);
        MainMenu.MainMenu.window.add(panel);
        panel.requestFocus();
        panel.validate();
        panel.repaint();
        panel.setVisible(true);
        
    }
    
//  public static void paintClock () {
//      clock.draw();
//  }
    
    public static void show() {
        if (panel == null) {
            initialise();
        } else {
            panel.setVisible(true);
        }
    }
    
    public static void hide() {
        if (panel != null) {
            panel.setVisible(false);
        }
    }
    
}

解决方法

此处的最佳示例:

How to Draw an BufferedImage to a JPanel

也简化并适合回答您的问题:

BattleScreen.java

import javax.swing.*;

public class BattleScreen extends JFrame {

    public static Clock clock;

    public BattleScreen() {
        super("BattleScreen TEST");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,200);
        clock = new Clock(); // use your clock here
        this.add(clock);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        BattleScreen battleScreen = new BattleScreen();

        // on a separate thread update the time.
        Thread countdownTime = new Thread((() -> {
            for (int i = 0; i < clock.getMaxTimeSlice(); i++) {
                clock.updateTime(i,8);
                sleep();
            }
        }));
        countdownTime.start();

        //control returns here almost immediately so you can carry on processing whilst the clock counts down
    }

    private static void sleep() {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Clock.java

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Clock extends JPanel {

    public static final int MAX_TIME_SLICE = 8;

    private BufferedImage sprite;
    private ArrayList<BufferedImage> images = new ArrayList<>();
    private int width,height,xPos,yPos;

    public Clock() {
        xPos = 0; // replacement
        yPos = 0; // replacement
        width = 50; // replaced
        height = 50; // replaced

        loadClockImages();

        this.setSprite(0);
        updateTime(0,8);
        repaint();
    }

    private void loadClockImages() {
        try {
            Integer index = 1; // starting at 1 rather than 0
            while (index < MAX_TIME_SLICE+1) {
                images.add(ImageIO.read(new File("./src/main/resources/images/clock_" +index+".png")));
                index++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void updateTime(Integer time,Integer timeEnd) {
        Double timeSlice = (time.doubleValue() / timeEnd.doubleValue()) * MAX_TIME_SLICE;
        Integer index = timeSlice.intValue();
        setSprite(index);
    }

    void setSprite(Integer index) {
        sprite = images.get(index);
        repaint(); // calls the methods for repainting the components at the various portions of updated screen
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (sprite != null) {
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - sprite.getWidth()) / 2;
            int y = (getHeight() - sprite.getHeight()) / 2;
            g2d.drawImage(sprite,x,y,this);
            g2d.dispose();
        }
    }

    public int getMaxTimeSlice() {
        return MAX_TIME_SLICE;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。