平滑快速移动和重绘()

时间:2012-09-26 07:54:34

标签: java swing graphics

我正在编写简单的太空入侵者并且在移动船舶时遇到问题。基本上我改变船的位置然后repaint()。如果变化很大,那就更像是跳跃而不是移动。如果变化很小,则动画更平滑但速度变慢。有没有解决方案?

我正在使用paintComponent()绘制JPanel。

@edit:我真的不明白我的电脑如何能够平滑地显示普通(新)游戏,而简单的绘图图像具有低帧速率。这就是为什么我认为它是软件问题而不是硬件。所以也许我会做一些影响帧率的事情。

@edit:好的,粘贴一些代码,但要理解 - 我开始使用java并且通常在2周前编程图形。

    package pl.spaceInvaders;
    import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;

class GamePanel extends JPanel{
SpaceInvadersMain sim;
private Image dbImage; 
private Graphics dbg; 
int moveDistHor=0;
int moveDistVer=0;
int shipMove=0;
int dir=1;

GamePanel(SpaceInvadersMain sim){
    this.sim=sim;
    setFocusable(true);
    moveMonsters();
    MyKeyListener mkl = new MyKeyListener();
    addKeyListener(mkl);
    Timer t = new Timer(500, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              if(moveDistHor<304){moveDistHor+=16*dir;}
              if(moveDistHor==304 || moveDistHor==0){dir*=-1;moveDistHor+=16*dir;moveDistVer+=16;}
              moveMonsters();
              moveProjectiles();
              detectColissions();
              repaint();
          }
       });
    t.start();
}
@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    setBackground(Color.white);
    Graphics2D g2 = (Graphics2D)g;
    Insets insets = getInsets();
    g2.translate(insets.left, insets.top);
    // drawing monsters
    for(int i=0;i<SpaceInvadersMain.M_WIDTH;i++){
        for(int j=0;j<SpaceInvadersMain.M_HIGHT;j++){
            if(sim.monsters[i][j].ifExists()){
            if((moveDistHor+moveDistVer)%32==0) 
                g2.drawImage(sim.monsters[i][j].img, sim.monsters[i][j].posX, sim.monsters[i][j].posY, 32, 32, null);
            else 
                g2.drawImage(sim.monsters[i][j].img2, sim.monsters[i][j].posX, sim.monsters[i][j].posY, 32, 32, null);
            }}
    }
    //drawing ship
    if(sim.ship.exists()) g2.drawImage(sim.ship.img, sim.ship.getPosX(), sim.ship.getPosY(),32,32,null);
}
protected void detectColissions() {
    // TODO Auto-generated method stub

}
protected void moveProjectiles() {
    // TODO Auto-generated method stub

}
protected void moveShip() {
    if(sim.ship.posX+shipMove>0 && sim.ship.posX+shipMove<750) {
        sim.ship.posX+=shipMove;
        shipMove=0;
    }

}
protected void moveMonsters(){
    for(int i=0;i<SpaceInvadersMain.M_WIDTH;i++){
        for(int j=0;j<SpaceInvadersMain.M_HIGHT;j++){
            if(sim.monsters[i][j].ifExists()){
                 sim.monsters[i][j].posX=10+i*48+moveDistHor;
                 sim.monsters[i][j].posY=10+j*48+moveDistVer;

            }}
    }
}
private class MyKeyListener implements KeyListener{

    @Override
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyCode()){
        case KeyEvent.VK_LEFT:
            //System.out.println("Arrow left caught");
            shipMove=-10;
            break;
        case KeyEvent.VK_RIGHT:
            //System.out.println("Arrow right caught");
            shipMove=10;
            break;
        }
        moveShip();
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {


    }

}

}

2 个答案:

答案 0 :(得分:3)

Space Invaders - 2D Rendering in Java教程可能会使用明确的BufferStrategy作为示例。

答案 1 :(得分:1)

请发一些代码?整个游戏是在画布内运行还是每艘船都是JComponent?对于游戏,我会使用JavaFX或像http://jmonkeyengine.com这样的游戏引擎。 要在Swing中创建流畅的动画,您可以使用动画框架: