平台精灵不会出现在我的JFrame上

时间:2011-12-10 05:03:57

标签: java swing path

我是Java拼贴学分课程的十年级学生,我正在以平台游戏的形式做一些额外的学分工作,其中角色必须跳过敌人来杀死它们,但它不会让我显示图像在JFrame上。由于某种原因,他们根本不会加载。

头等舱:主要

package game1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class Main extends JFrame{

    public class AL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e){
            int keyCode = e.getKeyCode();

        }
        public void keyRelease(KeyEvent e){

        }
    }

第二课:屏幕

    package game1;

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

public class Screen {

    private GraphicsDevice vc;

    public Screen(){
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        vc = env.getDefaultScreenDevice();
    }

    public void setFullScreen(DisplayMode dm, JFrame window){
        window.setUndecorated(true);
        window.setResizable(false);
        vc.setFullScreenWindow(window);

        if(dm != null && vc.isDisplayChangeSupported()){
            try{
                vc.setDisplayMode(dm);
            }catch(Exception ex){}
        }
    }

    public Window getFullScreenWindow(){
        return vc.getFullScreenWindow();

    }

    public void restoreScreen(){
        Window w = vc.getFullScreenWindow();
        if(w != null){
            w.dispose();

        }
        vc.setFullScreenWindow(null);
    }
}

第三类:图像类

package game1;

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


public class ImageClass extends JFrame{
    public static void main(String[] args) {

        DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
        ImageClass i = new ImageClass();
        i.run(dm);

    }
    private Screen s;
    /*private Image bg;
    private Image pic;*/
    private boolean loaded;


        //private ImageIcon bg;
        private BufferedImage bg;
        //private JLabel label1;
        //private ImageIcon pic;
        private BufferedImage pic;
       // private JLabel label2;

        ImageClass(){
            setLayout(new FlowLayout());

            //bg = new ImageIcon(getClass().getResource("C:\\Users\\Ana Masters\\Desktop\\sprites\\char.gif"));

            try
            {
            bg = ImageIO.read(new File("char.gif"));
            pic = ImageIO.read(new File("dude.jpg"));
            }
            catch (Exception e)
            {

            }
            //label1 = new JLabel(bg);
            //add(label1);

            //pic = new ImageIcon(getClass().getResource("C:\\Users\\Ana Masters\\Desktop\\sprites\\dude.jpg"));
            //label2 = new JLabel(pic);
            //add(label2);

        }



    //run method
    public void run(DisplayMode dm){
        setBackground(Color.WHITE);
        //setForeground(Color.WHITE);
        setFont(new Font("Arial", Font.PLAIN,50));
        loaded = false;

        s = new Screen();
        try{
            s.setFullScreen(dm, this);
            loadpics();
            try{
                Thread.sleep(10000);
            }catch(Exception ex){}
        }finally{
            s.restoreScreen();
        }
    }
    //load pastures

    public void loadpics(){
        BufferedImage ba = null;
                try{
                    ba =ImageIO.read(new File("dude.jpg"));


                }catch(IOException i){

                }
                BufferedImage ab = null;
                try{
                    ab =ImageIO.read(new File("char.gif"));

                }catch(IOException b){

                }


                repaint();
        loaded = true;
    }


    @Override
    public void paint(Graphics g){
        //Image g2 = null;
       Graphics2D g2 = null;
            if(g instanceof Graphics2D){
                g2 = (Graphics2D)g;
        //g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.KEY_ANTIALIASING);

            }
        if(loaded){
            g2.drawImage(bg,570,580,null);
            g2.drawImage(pic,170,180,null);

            }
            else{
                System.out.println("not here");
            }


            }

}

如果有人能提供帮助,我们将不胜感激。如何查找/加载图像?

1 个答案:

答案 0 :(得分:3)

bg = new ImageIcon(getClass().getResource("C:\\Users\\Ana Masters\\Desktop\\sprites\\char.gif"));

对于使用getResource()找到的应用程序资源,它必须位于应用程序的运行时类路径中。使用相对于类路径的/分隔的路径指定此类资源。例如,如果类路径的根是:

C:\\Users\\Ana Masters\\Desktop\\

GIF应位于:

bg = new ImageIcon(getClass().getResource("/sprites/char.gif"));

另请注意,将应用程序资源添加到也位于应用程序运行时类路径上的Jar,而不是尝试将它们作为文件中的松散资源进行定位会更常见,也更有意义。 - 系统。