我的照片移动得太慢了

时间:2015-02-07 18:46:20

标签: java arrays

我目前正在为大学制作一种冒险2D游戏项目。 我绘制了一系列图片(墙壁,地板等),然后是可移动的英雄人物。问题是:没有阵列,人物移动得很好但是随着阵列的移动开始变得困难。是我的Pc吗?或者我的绘图方法是否太“沉重”?请看一下:

import java.awt.Graphics;
import java.awt.Rectangle;

public class World {
public static int WorldW = 16 , WorldH = 16;
public SpielElement[][] element = new SpielElement[WorldW][WorldH];
public int[][] level0 ={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0},
                        {0,0,0,1,1,1,1,1,1,1,1,6,0,0,0,0},
                        {0,0,0,1,1,1,5,1,1,1,1,1,0,0,0,0},
                        {0,0,0,1,1,6,1,1,2,1,5,1,0,0,0,0},
                        {0,0,0,1,1,1,1,5,1,1,1,1,0,0,0,0},
                        {0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0},
                        {0,0,0,0,0,1,1,3,1,1,0,0,0,0,0,0},
                        {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

public int[][] level1 ={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,0,1,0,0,0,0,1,1,1,1,2,0,1,0},
                         {0,4,0,1,0,0,5,1,1,1,1,1,1,0,1,0},
                         {0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,0},
                         {0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,0},
                         {0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0},
                         {0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,0},
                         {0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0},
                         {0,1,0,1,1,1,1,1,1,1,1,1,0,0,3,0},
                         {0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0},
                         {0,1,0,1,1,1,1,0,0,1,0,1,0,0,1,0},
                         {0,1,0,1,0,0,1,0,0,1,0,1,1,1,1,0},
                         {0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0}, 
                         {0,1,1,1,1,6,1,1,0,1,0,0,1,0,0,0},
                         {0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0},
                         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

public World(){
    create();
}

public void create(){
    for(int x = 0; x<WorldW; x++){
        for(int y = 0; y<WorldH; y++){
            element[x][y] = new SpielElement(x,y,level0[y][x]);
        }
    }
}


public void generate(Graphics g){
    for(int x = 0; x<WorldW; x++){
        for(int y = 0; y<WorldH; y++){
            element[x][y].zeichne(g);
        }
    }
}

}

int数组是要定义我需要绘制哪种图片 我打算首先创建所有面板(SpielElement),然后绘制它们 以下是我绘制它们的方法:

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.swing.*;
    public class SpielElement extends JPanel{
    int posX,posY,type ;
    BufferedImage wand, boden, tuer, schluessel;



public SpielElement(int x, int y,int z){
    this.posX = x*40;
    this.posY = y*40;
    this.type = z;

    try {
        wand = ImageIO.read(SpielElement.class.getResource("/img/wand.png"));
        boden = ImageIO.read(SpielElement.class.getResource("/img/boden.png"));
        tuer = ImageIO.read(SpielElement.class.getResource("/img/tuer.png"));
        schluessel = ImageIO.read(SpielElement.class.getResource("/img/schluessel.png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

public void zeichne(Graphics g){
    switch(type){
    case 0: g.drawImage(wand,posX,posY,boden.getWidth(),wand.getHeight(),null);
        break;
    case 1:g.drawImage(boden,posX,posY,boden.getWidth(),boden.getHeight(),null);
        break;
    case 2: g.drawImage(schluessel,posX,posY,schluessel.getWidth(),schluessel.getHeight(),null);
        break;
    case 3:     g.drawImage(tuer,posX,posY,tuer.getWidth(),tuer.getHeight(),null);
        break;
    case 4:     g.drawImage(boden,posX,posY,boden.getWidth(),boden.getHeight(),null);
        break;
    case 5:g.drawImage(boden,posX,posY,boden.getWidth(),boden.getHeight(),null);
        break;
    case 6:     g.drawImage(boden,posX,posY,boden.getWidth(),boden.getHeight(),null);
        break;

}
}

}

我希望你通过阅读代码得到我的想法,并希望看到有什么问题。没有任何语法错误,但它太慢了。最后将所有这些添加到Jarame上添加的JPanel Board

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Board extends JPanel implements KeyListener{
private final int BREITE = 700;
private final int HOEHE = 700;
Hunter Held ;
World Welt;



public Board(){
    super();
    Held = new Hunter(200,200);
    Welt = new World();

    this.setBackground(Color.BLACK);
}

public void paint(Graphics g){
    super.paint(g); 
    Welt.generate(g);
    Held.draw(g);
}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if (e.getKeyCode() == KeyEvent.VK_LEFT)
        Held.posX = Held.posX - 10;

    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        Held.posX = Held.posX + 10;

    if (e.getKeyCode() == KeyEvent.VK_UP)
        Held.posY = Held.posY - 10;

    if (e.getKeyCode() == KeyEvent.VK_DOWN)
        Held.posY = Held.posY + 10;
        this.repaint();

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

}

Hero(Held)绘制的方式与其他图像完全相同,每次按下键时都会改变坐标,因此重新绘制。 此时我将不再进行进一步的代码转储。这就是它最终的样子: http://puush.me/account?pool=1468274

(抱歉还没有声望) Scrren上的数字在KeyInput上移动,但滞后... 这是我的第一个问题,我希望它足够详细。 提前Sry,如果我的问题是愚蠢的,只在2周前开始编程一般。 提出建设性批评。 sry for code Dumping ...

1 个答案:

答案 0 :(得分:0)

在我看到你提到JPanel和JFrame之前,我认为这是与Android相关的问题......

无论如何,也许你应该在一个新的线程中,在一个看不见的画布/图像/缓存上绘制它们,无论如何......然后把它带到前面并在后台绘制另一帧。

抱歉,我不知道如何使用Swing,但我认为这是所有动画框架的工作方式。在Android中,人们在后台线程中做所有这些肮脏的工作......