2

I'm writing easy space invaders and have a problem with moving ship. Basically I change position of ship and then repaint(). If the change is big it is more like jumping than moving. If the change is small animation is smoother but getting slower. Is there any solution?

I'm drawing on JPanel and using paintComponent().

@edit: I don't really understand how my pc can display normal(new) games smoothly and with simple drawing image has low frame rate. It is why i thought it was software problem rather than hardware. So maybe i do something what affects frame rate.

@edit: Ok, pasting some code, but be understanding - I started with java and generally programming graphics about 2 weeks ago.

    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) {


    }

}

}

Krever
  • 1,371
  • 1
  • 13
  • 32
  • You are limited by your update/frame rate, if you can increase your frame rate things will look smoother. – Goz Sep 26 '12 at 07:56
  • Your question is rather broad. For example an answer to " Is there any solution?" might be "Use a faster computer" but I'm somewhat sure that's not what you're actually looking for as an answer. Try to improve your question, show which ways of solving the problem you see, which one of these you've tried or not tried and for what reason and so on and so forth. Always keep in mind when writing a question if any person reading it can understand it the way you want. – hakre Sep 26 '12 at 08:01
  • You could try using one of the animation libraries like [TimingFramework](http://java.net/projects/timingframework/pages/Home) or [Trident](http://kenai.com/projects/trident/pages/Home) – MadProgrammer Sep 26 '12 at 08:13
  • Without seeing some code, it is rather hard to tell. Maybe you keep on repainting your all frame which can be quite time-consuming and unefficient. Maybe you have left some un-optimized code somewhere and that code keeps on getting called during painting-operations. – Guillaume Polet Sep 26 '12 at 08:19
  • 1
    Low frame rate might be due to the use of a `SwingWorker` (see [this question](http://stackoverflow.com/questions/11837749/gui-running-at-30-fps)) – Robin Sep 26 '12 at 08:23
  • COuld you try to define clip to repaint only changed piece of panel. SO try to call reaint(... your changes rect) – StanislavL Sep 26 '12 at 08:28
  • You could also have a look at a really bad example I did ;) [Tank](https://dl-web.dropbox.com/get/Public/Tank.zip?w=7b378c40) – MadProgrammer Sep 26 '12 at 08:37

2 Answers2

2

The Space Invaders - 2D Rendering in Java tutorial may be helpful as an example using an explicit BufferStrategy.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thanks for the link, going to read it just out of curiosity :-) – moeTi Sep 26 '12 at 11:29
  • @moeTi: On review, I notice that I changed `Canvas` to `JPanel` and used generic `java.util.List` in `Game`, but the code is instructive. – trashgod Sep 26 '12 at 11:41
1

Please post some code? Is the whole game running inside a canvas or is every ship a JComponent? For games I would use JavaFX or a game engine like http://jmonkeyengine.com. To create smooth animations in Swing you can use a animation framework:

Hendrik Ebbers
  • 2,570
  • 19
  • 34