精灵不会加载 [英] Sprites wouldnt load

查看:90
本文介绍了精灵不会加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java应该加载图像文件但不加载



代码有类文件

DrawImage.java应该加载图片

Board.java创建一个宽度和高度的板子



我试过的:



 package gms; 

import java.awt.EventQueue;
import javax.swing.JFrame;

公共类DrawImage扩展JFrame {

public DrawImage(){

initUI();
}

private void initUI(){

add(new Board());

pack();

setTitle(uc);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

public static void main(String [] args){

EventQueue.invokeLater(new Runnable(){

@覆盖
public void run(){
DrawImage c = new DrawImage();
c.setVisible(true);
}
});
}
}

Board.java

package gms;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

公共类委员会延长JPanel {

private Image uc;

public Board(){

initBoard();
}

private void initBoard(){

loadImage();

int w = uc.getWidth(this);
int h = uc.getHeight(this);
setPreferredSize(new Dimension(w,h));
}

private void loadImage(){

ImageIcon cu = new ImageIcon(jvm.png);
uc = cu.getImage();
}

@Override
public void paintComponent(Graphics g){

g.drawImage(uc,0,0,null);
}
}

解决方案

我想你已经达到了这个目标(参见在其中 [ ^ ]):当您为ImageIcon构造函数指定文件名或URL时,处理将被阻止,直到图像数据完全加载或数据为止已证明该位置无效。如果数据位置无效(但非空),则仍会成功创建ImageIcon;它只是没有大小,因此不会绘制任何内容

很酷,现在更改文件路径图像加载好了



谢谢


java is supposed to load an image file but doesn't loads

the code has the class files
DrawImage.java should load image
Board.java creates a board with width and height

What I have tried:

package gms;

import java.awt.EventQueue;
import javax.swing.JFrame;

public class DrawImage extends JFrame {

    public DrawImage() {

        initUI();
    }

    private void initUI() {

        add(new Board());

        pack();

        setTitle("uc");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                DrawImage c = new DrawImage();
                c.setVisible(true);
            }
        });
    }
}

Board.java

package gms;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Board extends JPanel {

    private Image uc;

    public Board() {

        initBoard();
    }
   
    private void initBoard() {
       
        loadImage();
       
        int w = uc.getWidth(this);
        int h =  uc.getHeight(this);
        setPreferredSize(new Dimension(w, h));       
    }
   
    private void loadImage() {
       
        ImageIcon cu = new ImageIcon("jvm.png");
        uc = cu.getImage();       
    }

    @Override
    public void paintComponent(Graphics g) {

        g.drawImage(uc, 0, 0, null);
    }
}

解决方案

I suppose you have hit this (see here[^]): "When you specify a filename or URL to an ImageIcon constructor, processing is blocked until after the image data is completely loaded or the data location has proven to be invalid. If the data location is invalid (but non-null), an ImageIcon is still successfully created; it just has no size and, therefore, paints nothing".


cool, changed the file path now the image loads fine

thanks


这篇关于精灵不会加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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