Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中获取图形对象?_Java - Fatal编程技术网

如何在Java中获取图形对象?

如何在Java中获取图形对象?,java,Java,我正在做一个基本的太空入侵者游戏。我从LWJGL.zip文件中获得了所有的资源(我没有使用LWJGL库来创建我的游戏,只是从中获得了图片等)。无论如何,每当我在键盘上按“空格”键时,我的键盘监听器就会创建一个新的子弹,我的飞船就会发射。但是,我不知道如何绘制子弹图像,因为我的KeyListener没有传递图形对象,您需要一个来绘制图像。导致问题的代码是“Shot”构造函数中的“drawImage”方法。这是我的密码: public class KeyTyped{ public

我正在做一个基本的太空入侵者游戏。我从LWJGL.zip文件中获得了所有的资源(我没有使用LWJGL库来创建我的游戏,只是从中获得了图片等)。无论如何,每当我在键盘上按“空格”键时,我的键盘监听器就会创建一个新的子弹,我的飞船就会发射。但是,我不知道如何绘制子弹图像,因为我的KeyListener没有传递图形对象,您需要一个来绘制图像。导致问题的代码是“Shot”构造函数中的“drawImage”方法。这是我的密码:

    public class KeyTyped{

    public void keyESC(){
        Screen.isRunning = false;
    }

    public void keyLEFT() {
        Screen.shipPosX -= 10;

    }

    public void keyRIGHT() {
        Screen.shipPosX += 10;

    }
    //This is automatically called from my KeyListener whenever i 
    //press the spacebar
    public void keySPACE(){
        if(!spacePressed){
            ShotHandler.scheduleNewShot();
        }else{
            return;
        }
    }


}
公共类散弹手{

public static int shotX = Screen.shipPosX;
public static int shotY = Screen.shipPosY + 25;




public static void scheduleNewShot() {
    //All this does is set a boolean to 'false', not allowing you to fire any more shots until a second has passed.
    new ShotScheduler(1);


    new Shot(25);
}
}

公共类快照扩展了ShotHandler{

public Shot(int par1){

    //This is my own method to draw a image. The first parameter is the graphics object that i need to draw.
    GUI.drawImage(*????????*, "res/spaceinvaders/shot.gif", ShotHandler.shotX, ShotHandler.shotY);

}
            //Dont worry about this, i was just testing something
    for(int i = 0; i <= par1; i++){
        ShotHandler.shotY++;
    }
}
公共快照(int par1){
//这是我自己绘制图像的方法。第一个参数是我需要绘制的图形对象。
GUI.drawImage(*?????*,“res/spaceinvaders/shot.gif”,ShotHandler.shotX,ShotHandler.shotY);
}
//别担心,我只是在测试一些东西

对于(int i=0;i的方法是,监听器应该改变游戏的状态(即创建一个子弹,或者改变子弹的位置,或者其他),然后调用
repaint()
。然后Swing将调用
paintComponent(图g)
方法,该方法将使用作为参数传递的图形绘制更新的游戏状态。

其思想是侦听器应更改游戏状态(即创建一个项目符号,或更改项目符号的位置,或其他),然后调用
重新绘制()
。然后Swing将调用
绘制组件(图形g)
方法,该方法将使用作为参数传递的图形绘制更新的游戏状态。

您需要将“子弹”的位置存储在某个位置,然后在
绘制组件(图形g)中访问该状态
method。实际上,这应该考虑很多因素。让你的
Shot
类看起来像这样:

public class Shot {
    private Point location; // Could be Point2D or whatever class you need

    public Shot(Point initLocation) {
        this.location = initLocation;
    }

    // Add getter and setter for location

    public void draw(Graphics g) {
        // put your drawing code here, based on location
    }
 }
然后用你的按键法,

public void keySPACE(){
    // Add a new shot to a list or variable somewhere
    // WARNING: You're getting into multithreading territory.
    // You may want to use a synchronized list.
    yourJPanelVar.repaint();
}
您将扩展
JPanel
并覆盖
paintComponent

public class GameScreen extends JPanel {
    public paintComponent(Graphics g) {
        for (shot : yourListOfShots) {
             shot.draw(g);
        }
        // And draw your ship and whatever else you need
    }
 }
这是基本的想法。希望它现在有意义。我想你可以把
快照的绘图代码移到别处,但为了简单起见,我只停留在
快照
类本身


我会注意到上面的代码相当混乱。看看MVC模式。它有助于更清晰的状态抽象(将状态与显示代码分离)。

您需要将“项目符号”的位置存储在某个位置,然后在
绘图组件(图g)中访问该状态
method。实际上,这应该考虑很多因素。让你的
Shot
类看起来像这样:

public class Shot {
    private Point location; // Could be Point2D or whatever class you need

    public Shot(Point initLocation) {
        this.location = initLocation;
    }

    // Add getter and setter for location

    public void draw(Graphics g) {
        // put your drawing code here, based on location
    }
 }
然后用你的按键法,

public void keySPACE(){
    // Add a new shot to a list or variable somewhere
    // WARNING: You're getting into multithreading territory.
    // You may want to use a synchronized list.
    yourJPanelVar.repaint();
}
您将扩展
JPanel
并覆盖
paintComponent

public class GameScreen extends JPanel {
    public paintComponent(Graphics g) {
        for (shot : yourListOfShots) {
             shot.draw(g);
        }
        // And draw your ship and whatever else you need
    }
 }
这是基本的想法。希望它现在有意义。我想你可以把
快照的绘图代码移到别处,但为了简单起见,我只停留在
快照
类本身


我会注意到,我上面的代码非常混乱。看看MVC模式。它有助于更清晰的状态抽象(将状态与显示代码分离)。

你能再解释一下吗?我知道paintComponent()、repaint()、states等,但我似乎无法将其组合在一起。(对不起,我刚开始看java游戏教程。我了解java至少两年了,但我不是最高级的游戏程序员xD。)如果你正在做一些自定义绘制,请尝试覆盖
JPanel
上的
paint(Graphics g)
方法(一定要调用
super.paint(g)
),并将渲染代码放在其中。使用传入的
Graphics
对象。然后调用
repaint()时
,您的自定义渲染将被触发。@akf Swing类建议不要重写
paint
,我记得。
paint
包含处理背景等的代码。@akf jpmc26是对的,您应该重写
paintComponent
方法,而不是
paint
方法ses paintComponent将是您需要覆盖它的唯一方法-如果可以的话,我会编辑我的评论-但是海报应该得到要点-…尝试覆盖
paintComponent(Graphics g)
方法…您能再解释一下吗?我知道paintComponent(),repaint(),状态等,但我似乎不能把它放在一起。(对不起,我刚开始看java游戏教程。我了解java至少两年了,但我不是最高级的游戏程序员xD。)如果你正在做一些自定义绘制,请尝试覆盖
JPanel上的
paint(Graphics g)
方法(确保调用
super.paint(g)
),并将渲染代码放在其中。使用传入的
Graphics
对象。然后调用
repaint()时
,您的自定义渲染将被触发。@akf Swing类建议不要重写
paint
,我记得。
paint
包含处理背景等的代码。@akf jpmc26是对的,您应该重写
paintComponent
方法,而不是
paint
方法ses paintComponent将是您需要覆盖它的唯一方法-如果可以,我会编辑我的评论-但是海报应该得到要点-…尝试覆盖
paintComponent(图g)
method…
foreach
在Java中不存在,它只是
的反斜杠谢谢。在C上已经有一段时间了。
foreach
在Java中不存在,它只是
的反斜杠谢谢。在C上已经有一段时间了。