Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:19:54+00:00 2026-06-12T00:19:54+00:00

I’m writing easy space invaders and have a problem with moving ship. Basically I

  • 0

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


    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:19:55+00:00Added an answer on June 12, 2026 at 12:19 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an array which has BIG numbers and small numbers in it. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.