首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 2D编程:渲染技术

Java 2D编程:渲染技术
EN

Stack Overflow用户
提问于 2014-11-11 08:11:27
回答 1查看 753关注 0票数 0

我用java编程已经有一段时间了,我刚刚开始做2D图形(游戏开发)。在编写游戏代码时,我注意到了游戏的渲染问题。当我的播放器在屏幕上运行时,像素会出现问题。这就像多人游戏中的滞后,但它几乎不明显。在研究了这个问题之后,我还没有想出任何解决方案,所以我请求您的帮助。

我的问题:我在我的游戏中遇到了糟糕的渲染,我想知道我的渲染方式是否很糟糕。如果是这样的话,你能告诉我有什么资源吗?

我的渲染类

代码语言:javascript
复制
public class Render extends JPanel implements Runnable{
    int SCREEN_WIDTH,SCREEN_HEIGHT;
    Game game;
    Thread t;

    public Render(int w,int h,Game g){
        this.SCREEN_WIDTH = w;
        this.SCREEN_HEIGHT = h;
        this.game = g;
        this.setDoubleBuffered(true);

    }

    public void run(){
        while(true){
            repaint();
        }
    }

    public void paint(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(-this.game.getCamera().getX(), -game.getCamera().getY());


        g2d.setColor(Color.black);
        //fill the screen with black
        g2d.fillRect(0, 0, game.getLevelHandler().getLevel().getWidth() * 32, game.getLevelHandler().getLevel().getHeight() * 32);

        /**for(Entity E: game.getObjects()){
            E.draw(g2d);
        }*/
        //send the graphics object to my player...IS THIS OKAY TO DO?
        game.getPlayer().draw(g2d);

    }

    public void start(){
        t = new Thread(this);
        t.start();
    }

}




public class Player extends Entity{
    float dx,dy,moveSpeed;
    boolean jumping, canJump;

    float terminalVelocity,acceleration = 0;
    int GravityCounter,jumps,maxJumps,jumpheight = 0;


    public Player(float x, float y, float w, float h, ObjectID id, Game g) {
        super(x, y, w, h, id, g);
        //gravity
        terminalVelocity += 7;
        acceleration += 0.2;
        moveSpeed += 2.5;

        //jumping
        jumpheight = 40;        
        maxJumps = 2;
        jumps = maxJumps;
    }

    public void tick() {
        dx = 0;
        dy = 0;

        collisions();
        move();

        x += dx;
        y += dy;
    }

    public void collisions(){
    }

        public void move(){
        }
        //the drawing
        public void draw(Graphics2D g) {
            g.setColor(Color.green);
            g.drawRect((int)x, (int)y, (int)w, (int)h);
        }

    }

编辑:可下载的链接here,如果需要任何更改,我会尝试并更正它。另外,当存在内存泄漏时,会出现这种小故障吗?

我根据建议修改了我的代码,这就是我现在所拥有的。

渲染器:

代码语言:javascript
复制
public class Render extends JPanel implements Runnable{
    int SCREEN_WIDTH,SCREEN_HEIGHT;
    Game game;
    Thread t;

    public Render(int w,int h,Game g){
        this.SCREEN_WIDTH = w;
        this.SCREEN_HEIGHT = h;
        this.game = g;
        this.setDoubleBuffered(true);

    }

    public void run(){
        while(true){
            repaint();
            try {
                Thread.sleep(16);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void paint(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(-this.game.getCamera().getX(), -game.getCamera().getY());


        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, game.getLevelHandler().getLevel().getWidth() * 32,                 game.getLevelHandler().getLevel().getHeight() * 32);

        for(Entity E: game.getObjects()){
            synchronized(E){E.draw(g2d);}
        }

        synchronized(game.getPlayer()){game.getPlayer().draw(g2d);}

     }

    public void start(){
        t = new Thread(this);
        t.start();
    }

}

player类没有更改

源代码:here

EN

回答 1

Stack Overflow用户

发布于 2014-11-11 09:15:34

我知道这里发生了什么,因为我在我正在编写的游戏中遇到了完全相同的问题。这肯定是一个很难找到的东西。您的问题发生是因为您在绘制时舍入了xy变量。

代码语言:javascript
复制
g.drawRect((int)x, (int)y, (int)w, (int)h);

这会导致你的矩形跳来跳去。因为看起来您的矩形可以具有十进制坐标,所以您需要能够以十进制坐标绘制矩形。有一种非常简单的方法可以做到这一点,但是让我们使用一种无论您绘制的是什么都可以工作的方法。

你需要使用一些转换,就我个人而言,我喜欢AffineTransform的。有很多关于这些的教程,但这是谷歌搜索https://docs.oracle.com/javase/tutorial/2d/advanced/transforming.html的第一个(非文档)结果。我知道你已经在做一些转换了,但这是你的draw方法在添加它们之后的样子。

代码语言:javascript
复制
public void draw(Graphics2D g) {
        AffineTransform original = g.getTransform(); // keep a copy of the original so that we can restore it
        AffineTransform transform = (AffineTransform)original.clone();
        transform.translate((double)x, (double)y); // translate the transform to the player location, note that x and y can be non-integers here.
        g.transform(transform); // apply the transformation.
        g.setColor(Color.green);
        g.drawRect(0, 0, (int)w, (int)h);
        g.setTransform(original); // restore the original transformation
    }

对于其余的墙和东西,您可以使用完全相同的方法。请记住,对象的“屏幕”位置是对象的游戏位置减去相机的游戏位置。祝你的游戏好运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26855366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档