Java 如何将图像附着到对象?

Java 如何将图像附着到对象?,java,image,swing,user-interface,object,Java,Image,Swing,User Interface,Object,我是一个Java程序员初学者,正在开发一个非常简单的游戏。我已经学习了面向对象编程的基础知识,正在尝试测试和扩展我的知识 在我的游戏中,有一个投射物。游戏在JFrame中进行 如何在JFrame中直观地显示投射物对象?我想使用图像/图片。我基本上想把一个图像“附加”到投射物上 主类 public class MathGame implements ActionListener { private static JButton[] choices = new JButton[4];

我是一个Java程序员初学者,正在开发一个非常简单的游戏。我已经学习了面向对象编程的基础知识,正在尝试测试和扩展我的知识

在我的游戏中,有一个投射物。游戏在JFrame中进行

如何在JFrame中直观地显示投射物对象?我想使用图像/图片。我基本上想把一个图像“附加”到投射物上

主类

public class MathGame implements ActionListener {

    private static JButton[] choices = new JButton[4];
    private static JLabel[] options = new JLabel[4];
    private static double[] nums = new double[4];
    public static JPanel container;

    public static void main(String[] args) {

        // frame
        JFrame frame = new JFrame("Math Game");
        frame.setSize(300, 500);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // container
        container = new JPanel();
        container.setLayout(null);
        frame.setContentPane(container);

        // title
        JLabel title = new JLabel("Math Game");
        title.setBounds(113, 10, 80, 15);

        // option labels
        for (int i = 0; i < choices.length; i++) {
            options[i] = new JLabel("", SwingConstants.CENTER);
            options[i].setBounds(5 + (72 * i), 50, 68, 68);
            nums[i] = (int) (Math.random() * 100);
            options[i].setText(Double.toString(nums[i]));
            options[i].setOpaque(true);
            options[i].setBackground(Color.RED);
        }

        // buttons
        for (int i = 0; i < choices.length; i++) {
            choices[i] = new JButton();
            choices[i].setBounds(5 + (72 * i), 420, 68, 20);
            choices[i].addActionListener(new MathGame());
            container.add(choices[i]);
        }

        // progress bar
        JProgressBar progress = new JProgressBar();
        progress.setBounds(5, 450, 284, 20);
        progress.setValue(0);
        progress.setBackground(Color.LIGHT_GRAY);
        progress.setStringPainted(true);

        // adding it all
        for (int i = 0; i < choices.length; i++) {
            container.add(choices[i]);
            container.add(options[i]);
        }
        container.add(title);
        container.add(progress);

        // extras
        frame.toFront();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        Projectile bullet = new Projectile(150, 250);
        System.out.println(bullet.toString());
    }
}
public class Projectile extends Piece {

    public Projectile(int x, int y) {
        super(x, y);
    }

    public void fire() {
        for (int i = 0; i < 10; i++) {
            this.yPos = this.yPos - 5;
        }
    }

    public void draw(Graphics2D g2)
            throws IOException {

        BufferedImage image = ImageIO.read(new File("C:\\Users\\jbakos\\Desktop\\pew.png"));
        g2.drawImage(image, this.xPos, this.yPos, null);

    }
}
公共类MathGame实现ActionListener{
私有静态JButton[]选项=新JButton[4];
私有静态JLabel[]选项=新JLabel[4];
私有静态双精度[]nums=新双精度[4];
公共静态JPanel容器;
公共静态void main(字符串[]args){
//框架
JFrame=新的JFrame(“数学游戏”);
框架。设置尺寸(300500);
frame.setresizeable(false);
frame.setLocationRelativeTo(空);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//容器
container=newjpanel();
container.setLayout(null);
frame.setContentPane(容器);
//头衔
JLabel title=新JLabel(“数学游戏”);
标题.挫折(113、10、80、15);
//选项标签
for(int i=0;i
射弹类

public class MathGame implements ActionListener {

    private static JButton[] choices = new JButton[4];
    private static JLabel[] options = new JLabel[4];
    private static double[] nums = new double[4];
    public static JPanel container;

    public static void main(String[] args) {

        // frame
        JFrame frame = new JFrame("Math Game");
        frame.setSize(300, 500);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // container
        container = new JPanel();
        container.setLayout(null);
        frame.setContentPane(container);

        // title
        JLabel title = new JLabel("Math Game");
        title.setBounds(113, 10, 80, 15);

        // option labels
        for (int i = 0; i < choices.length; i++) {
            options[i] = new JLabel("", SwingConstants.CENTER);
            options[i].setBounds(5 + (72 * i), 50, 68, 68);
            nums[i] = (int) (Math.random() * 100);
            options[i].setText(Double.toString(nums[i]));
            options[i].setOpaque(true);
            options[i].setBackground(Color.RED);
        }

        // buttons
        for (int i = 0; i < choices.length; i++) {
            choices[i] = new JButton();
            choices[i].setBounds(5 + (72 * i), 420, 68, 20);
            choices[i].addActionListener(new MathGame());
            container.add(choices[i]);
        }

        // progress bar
        JProgressBar progress = new JProgressBar();
        progress.setBounds(5, 450, 284, 20);
        progress.setValue(0);
        progress.setBackground(Color.LIGHT_GRAY);
        progress.setStringPainted(true);

        // adding it all
        for (int i = 0; i < choices.length; i++) {
            container.add(choices[i]);
            container.add(options[i]);
        }
        container.add(title);
        container.add(progress);

        // extras
        frame.toFront();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        Projectile bullet = new Projectile(150, 250);
        System.out.println(bullet.toString());
    }
}
public class Projectile extends Piece {

    public Projectile(int x, int y) {
        super(x, y);
    }

    public void fire() {
        for (int i = 0; i < 10; i++) {
            this.yPos = this.yPos - 5;
        }
    }

    public void draw(Graphics2D g2)
            throws IOException {

        BufferedImage image = ImageIO.read(new File("C:\\Users\\jbakos\\Desktop\\pew.png"));
        g2.drawImage(image, this.xPos, this.yPos, null);

    }
}
公共类{
公共射弹(整数x,整数y){
super(x,y);
}
公共场所火灾(){
对于(int i=0;i<10;i++){
this.yPos=this.yPos-5;
}
}
公共空白绘图(图形2D g2)
抛出IOException{
BuffereImage image=ImageIO.read(新文件(“C:\\Users\\jbakos\\Desktop\\pew.png”);
g2.drawImage(image,this.xPos,this.yPos,null);
}
}

我是新的java程序员

视情况而定,我也在做类似的事情。我制作了一个显示网格的JPanel,因此它在8x8网格的每个正方形上显示一个“块”或对象。然后,您可以在JFrame中显示JPanel

要显示图像,draw方法()与下面类似

public void draw(Graphics2D g2, int x, int y)
            throws IOException {

        BufferedImage image = ImageIO.read(new File("location of image"));
        g2.drawImage(image, x, y, null);


    }
有关更多详细信息,请参阅:
首先,将JPanel添加到JFrame。JFrame是一个根容器,不建议使用JButtons、JLabels等元素,因此需要一个子容器

JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
将panel的LayoutManager设置为null,这允许您将子元素绝对定位在panel内部

panel.setLayout(null);
现在,添加一个带有ImageIcon的JLabel,它将把图片带到面板上,并通过setBounds将其定位

JLabel lbl = new JLabel(new ImageIcon(picture));
lbl.setBounds(x,y,width,height)

我不确定您的游戏应用程序会是什么样子,但我建议您仅在游戏内容区域使用空布局,其余部分使用其他布局管理器。

我看到了您的编辑。本质上,考虑到你的敌人对象是它自己的类,并且有一个paint方法,上面的代码也会这样做。一旦你创建了一个敌方物体,上面的方法会把它画在那个位置上,在那个位置上,图像会“代表”敌方物体。除非我遗漏了什么,如果你能对此发表评论,我可以尝试扩展我的答案。好的。我添加了所有的代码,但是,它在创建时并没有绘制图像。我可以看到你的敌人对象和你的JFrame代码吗?将它添加到你的主帖子中。Ctrl-K将代码高亮显示为代码不要读取图形代码中的图像。在构建GUI时阅读图像。从磁盘读取图像的速度比在绘图JPanel上绘制图像的速度慢1000倍。