游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4948|回复: 4

调用createImage()方法创建一个Image对象,怎么返回 null 值?

[复制链接]

2

主题

11

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2004-9-20 12:08:00 | 显示全部楼层 |阅读模式
我是在一个panel里写的代码,请大家帮忙看看:
注释掉的部分是因为在对null对象操作,取而代之打印img对象,返回的确为 null 。

/*
* Created on 2004-9-19
*/
package game.graphics;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
* @author Administrator
*
*/
public class GamePanel extends JPanel {
    private Image bgImage;
    /**
     * This is the default constructor
     */
    public GamePanel() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private  void initialize() {
        System.out.println("Begin:Init");
        this.setSize(800,600);
        loadImages();
        Image img =  this.createImage(800,600);
        System.out.println(img);
        //Graphics g = img.getGraphics();
        //draw(g);
        //g.dispose();
    }
       
    private void loadImages(){
        bgImage = loadImage("images/background.jpg");
    }
       
    private Image loadImage(String fileName){
        return new ImageIcon(fileName).getImage();
    }
       
    public void draw(Graphics g) {
        // draw background
        g.drawImage(bgImage, 0, 0, this);
    }
}

2

主题

11

帖子

11

积分

新手上路

Rank: 1

积分
11
 楼主| 发表于 2004-9-20 12:14:00 | 显示全部楼层

Re:调用createImage(int,int)方法创建一个Image对象,怎么老是返

补上在FRAME框架里调用GamePanel的代码:

/*
* Created on 2004-9-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package game.graphics;

import java.awt.Color;
import java.awt.MenuShortcut;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import javax.swing.JMenuBar;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MainFrame extends JFrame {

        //private javax.swing.JPanel jContentPane = null;
    private GamePanel gp = null;

        private JMenuBar jJMenuBar = null;
        /**
         * This method initializes jJMenuBar       
         *        
         * @return javax.swing.JMenuBar       
         */   
        private JMenuBar getJJMenuBar() {
                if (jJMenuBar == null) {
                        jJMenuBar = new JMenuBar();
                }
                return jJMenuBar;
        }
       
    public static void main(String[] args) {
         MainFrame TFrame1 = new MainFrame();
         TFrame1.show();
    }
   
        /**
         * This is the default constructor
         */
        public MainFrame() {
                super();
                initialize();
        }
        /**
         * This method initializes this
         *
         * @return void
         */
        private void initialize() {
                //this.setContentPane(getJContentPane());
            this.add(getJContentPane());
                this.setLocation(0,0);
                this.setSize(400, 300);
                addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                });
                addMenu();
        }
        /**
         * This method initializes jContentPane
         *
         * @return javax.swing.JPanel
         */
        private JPanel getJContentPane() {
                if(gp == null) {
                        gp = new GamePanel();
                        gp.setBackground(Color.lightGray);
                }
                return gp;
        }
       
        private void addMenu(){
            //JMenuBar jJMenuBar = new JMenuBar();
            this.getJJMenuBar();
            MenuShortcut ms = new MenuShortcut(KeyEvent.VK_S);
            MenuListener menuListener = new MenuListener(this);
            
            JMenu gameMenu = new JMenu("Game");
            JMenuItem startItem = new JMenuItem("Start");
            startItem.addActionListener(menuListener);
            gameMenu.add(startItem);
            
            JMenuItem pauseItem = new JMenuItem(&quotause");
            pauseItem.addActionListener(menuListener);
            gameMenu.add(pauseItem);
            
            JMenuItem stopItem = new JMenuItem("Stop");
            stopItem.addActionListener(menuListener);
            gameMenu.add(stopItem);
            
            JMenuItem exitItem = new JMenuItem("Exit");
            exitItem.addActionListener(menuListener);
            gameMenu.addSeparator();
            gameMenu.add(exitItem);
            
            JMenu aboutMenu = new JMenu("About");
            JMenuItem aboutItem = new JMenuItem("About");
            aboutMenu.add(aboutItem);
            
            jJMenuBar.add(gameMenu);
            jJMenuBar.add(aboutMenu);
            setJMenuBar(jJMenuBar);            
        }
       
    private class MenuListener implements ActionListener
    {
        MainFrame m_tFrame;
        public MenuListener(MainFrame tFrame)
        {
            m_tFrame=tFrame;
        }
        public void actionPerformed(ActionEvent e)
        {
        //Invoked when an action occurs.
            String sCommand=e.getActionCommand();
            if(sCommand.equals("Start"))
            {
                //控制开始玩游戏的代码加到这儿
                //System.out.println("开始游戏");
            }
            else if(sCommand.equals("Stop"))
            {
                //控制结束游戏的代码加到这儿。
            }
            else if(sCommand.equals("Pause"))
            {
                //控制暂停游戏的代码加到这儿。
            }
            else if(sCommand.equals("Exit"))
            {
                //控制关闭游戏的代码加到这儿。
                dispose();
            }
            else if(sCommand.equals("About"))
            {
                //控制显示游戏的关于对话框的代码加到这儿。
            }
        }
    }
}  

42

主题

202

帖子

222

积分

中级会员

Rank: 3Rank: 3

积分
222
QQ
发表于 2004-9-20 13:04:00 | 显示全部楼层

Re: Re:调用createImage(int,int)方法创建一个Image对象,怎么老

你试试在这个Panel在屏幕上显示出来之后再调用createImage方法。

2

主题

11

帖子

11

积分

新手上路

Rank: 1

积分
11
 楼主| 发表于 2004-9-20 16:08:00 | 显示全部楼层

Re: Re: Re:调用createImage(int,int)方法创建一个Image对象,怎么

zangweiren: Re: Re:调用createImage(int,int)方法创建一个Image对象,怎么老是返回 null 值?

你试试在这个Panel在屏幕上显示出来之后再调用createImage方法。

谢谢先!
我的意图是想在PANEL里画MAP和精灵,而MAINFRAME只是一个screen的管理框架。这么以来岂不是要在FRAME框架里画图?

2

主题

11

帖子

11

积分

新手上路

Rank: 1

积分
11
 楼主| 发表于 2004-9-20 17:12:00 | 显示全部楼层

Re:调用createImage(int,int)方法创建一个Image对象,怎么老是返

其实是我太急于求成了,对java2d的API缺乏基本的了解,比如这个问题只要实现自己的 paint()  方法,然后调用 repaint() 强制AWT 执行 paint() 方法就OK了。修改后的 GamePanel.java的代码如下:
/*
* Created on 2004-9-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package game.graphics;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GamePanel extends JPanel {
    private Image bgImage;
   
        /**
         * This is the default constructor
         */
        public GamePanel() {
                super();
                initialize();
        }
        /**
         * This method initializes this
         *
         * @return void
         */
        private  void initialize() {
                this.setSize(800,600);
                loadImages();
        }
       
        private void loadImages(){
            bgImage = loadImage("images/background.jpg");
                repaint();
        }
       
        private Image loadImage(String fileName){
            return new ImageIcon(fileName).getImage();
        }

        public void paint(Graphics g){
            draw(g);
        }
       
        public void draw(Graphics g) {
        // draw background
        g.drawImage(bgImage, 0, 0, null);
        g.dispose();
    }
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-18 17:32

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表