Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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
如何使用自定义绘制类在JavaSlick中绘制对象_Java_Draw_Renderer - Fatal编程技术网

如何使用自定义绘制类在JavaSlick中绘制对象

如何使用自定义绘制类在JavaSlick中绘制对象,java,draw,renderer,Java,Draw,Renderer,我正在用2d创建一个非常简单的Java Slick游戏。我可以在扩展BasicGameState的类中使用renderer方法,但我希望使用DarwMap类将其渲染到我的游戏容器中 以下是我的游戏源代码和不工作的DrawMap类: public class GamePlay extends BasicGameState{ DrawMap map; public GamePlay(int state){ } @Override public void

我正在用2d创建一个非常简单的Java Slick游戏。我可以在扩展BasicGameState的类中使用renderer方法,但我希望使用DarwMap类将其渲染到我的游戏容器中

以下是我的游戏源代码和不工作的DrawMap类:

public class GamePlay extends BasicGameState{

    DrawMap map;

    public GamePlay(int state){

    }

    @Override
    public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
        // TODO Auto-generated method stub      
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        map.render();
    }

    @Override
    public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
        // TODO Auto-generated method stub

    }

    @Override
    public int getID() {
        // TODO Auto-generated method stub
        return 1;
    }
}
下节课呢

public class DrawMap {

    //size of the map
    int x,y;
    //size of the tile
    int size;

    //tile
    Image tile;

    //creator
    public DrawMap(GameContainer gc, int x, int y, int size){
         this.x = x;
         this.y = y;
         this.size = size;
    }

    public void render() throws SlickException{

         tile = new Image("res/Tile.png");

         for(int i=0; i<(y/size); i++){
             for(int j=0; j < (x/size); j++){
                 tile.draw(j*size, i*size, 2);
            }
         }
    }

}
公共类DrawMap{
//地图的大小
int x,y;
//瓷砖的尺寸
整数大小;
//瓦片
图像块;
//创造者
公共绘图地图(游戏容器gc,整数x,整数y,整数大小){
这个.x=x;
这个。y=y;
这个。大小=大小;
}
public void render()引发异常{
tile=新图像(“res/tile.png”);

对于(inti=0;i我在构造函数中看不到它,但我假设您正在那里创建
映射
实例

现在,在屏幕上绘制(这对Slick和Java2D一般都是有效的)您需要一个
Graphics
对象,该对象表示图形上下文,该对象负责将您的数据放入屏幕。对于Slick2D,您可以通过调用
GameContainer
方法从
GameContainer
获取图形。然后,您可以调用
d将图像绘制到屏幕中在刚刚获得的
图形
对象上绘制图像
方法

下面是一个示例,将图形上下文作为
DrawMap
render
方法的参数传递:

public class GamePlay extends BasicGameState{

    DrawMap map;    
    ...

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        map.render(gc.getGraphics());
    }
    ...
}
还有
DrawMap

public class DrawMap {

    Image tile;
    ...        

    public void render(Graphics g) throws SlickException {
    // your logic to draw in the image goes here

        // then we draw the image. The second and third parameter
        // arte the coordinates where to draw the image
        g.drawImage(this.tile, 0, 0);
    }   
}

当然,您可以直接绘制到
图形
对象中。

我在您的构造函数中没有看到它,但我假设您正在那里创建
映射
实例

现在,在屏幕上绘制(这对Slick和Java2D一般都是有效的)您需要一个
Graphics
对象,该对象表示图形上下文,该对象负责将您的数据放入屏幕。对于Slick2D,您可以通过调用
GameContainer
方法从
GameContainer
获取图形。然后,您可以调用
d将图像绘制到屏幕中在刚刚获得的
图形
对象上绘制图像
方法

下面是一个示例,将图形上下文作为
DrawMap
render
方法的参数传递:

public class GamePlay extends BasicGameState{

    DrawMap map;    
    ...

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        map.render(gc.getGraphics());
    }
    ...
}
还有
DrawMap

public class DrawMap {

    Image tile;
    ...        

    public void render(Graphics g) throws SlickException {
    // your logic to draw in the image goes here

        // then we draw the image. The second and third parameter
        // arte the coordinates where to draw the image
        g.drawImage(this.tile, 0, 0);
    }   
}

当然,您可以直接绘制到
图形
对象中。

您在哪里创建
DrawMap
实例?为什么不在构造函数上使用容器?最后,您在什么时候创建图像,以及之后如何处理图像?假设您只是将图像作为一个缓冲区绘制到其中,y你最终必须将其转储到屏幕中…我在GamePlay类中创建了名为“map”的DrawMap实例。我应该将gc分配给这样一个变量吗?'this.gc=gc;'或者我应该做些其他事情吗?至于你的最后一个问题,图像是在“DrawMap”类中创建的,我希望通过使用“DrawMap”类,而不是“GamePlay”类中“renderer”方法中的一块代码。您在哪里创建
DrawMap
实例?为什么不使用构造函数上的容器?最后,您在何时创建图像以及之后如何处理图像?如果您只是将图像作为一个后缓冲区绘制到其中,则最终必须将其转储到屏幕中…我在GamePlay类中创建了名为“map”的DrawMap实例。我应该将gc分配给这样一个变量吗?'this.gc=gc;'或者我应该做些其他事情吗?至于你的最后一个问题,图像是在“DrawMap”类中创建的,我希望它被绘制到屏幕上,但使用“Dr”awMap类而不是GamePlay类中renderer方法中的代码块。谢谢,这正是我想要的:)你救了我一天:)很高兴它有帮助:3不要急于求成,但如果这是你问题的答案,你应该把它标记为接受d:谢谢,这正是我所要求的:)你救了我一天:)很高兴它有帮助:3不要急于求成,但如果这是你问题的答案,你应该把它标记为接受d: