i trying to my background move left if user hit right key. and background right if user hit left key. that way it will look like my player is moving.

the problem is that when ever i hold right key, the background waits 1 sec than moves to left. i want to make it smoother so if user hold right key, than the background should move to left right way.

note the background is 2d int array with just '2''s in it. and in paint i am drawing a green rect. so background is green.

main.java

.....
public void keyPressed(KeyEvent e)
    {
       int keys = e.getKeyCode();

        if(keys == KeyEvent.VK_RIGHT)
        {
            level_class.hitRIGHT();  //move camera left
        }
        else if(keys == KeyEvent.VK_LEFT)
        {
            level_class.hitLEFT();  //move camera right
        }
        }/*** end of key pressed method ***/


        /*** KEY RELEASED ***/
        public void keyReleased(KeyEvent e)
        {
            int keys = e.getKeyCode();

                if(keys == KeyEvent.VK_RIGHT)
                {

                }
                else if(keys == KeyEvent.VK_LEFT)
                {

                }
        }/*** end of key pressed method ***/

now in my level class i am setting variables

    public class levels 
    {
        private int map01[][] = { { 2, 2, 2 },

                              { 2, 2, 2} };

          int camera_pos_x = 0;       
          int camera_pos_y = 0;   
         int camera_speed = 5;   //camera speed

      .....
      /*** move camera left ***/
    public void hitRIGHT()
    {
        camera_pos_x += camera_speed;
    }
    public void hitLEFT()
    {
        camera_pos_x -= camera_speed;
    }
    public void paint(Graphics g) {
        for (int y = 0; y < map01.length; y++) // rows
        {
            for (int x = 0; x < map01[y].length; x++) // cols
            {
                if (map01[y][x] == 0) {
                    g.setColor(Color.green);
                    g.fillRect(x * tile_size - camera_pos_x, y * tile_size -camera_pos_y, tile_size,
                            tile_size);
                }
            }
        }// end of main for loop
    }// end of paint method
}




    ...

The user won't be able to see any change unless you draw the change. Just doing camera_pos_x += camera_speed is invisible to the user. I'm sure that the change to camera_pos_x happens immediately when the key is pressed; the delay that you are seeing is only a delay in the GUI being redrawn.

I don't know what eventually causes your GUI to be redrawn since you are clearly doing nothing to cause that when you update camera_pos_x, but the operating system will trigger a redraw whenever it is needed so it is bound to happen eventually. If you want it to happen immediately when camera_pos_x changes, the conventional way is to call repaint on whatever component needs to be redrawn.

right now in main method i repaint and paintcomponent something like this:

public class main extends JApplet ...
...

    public void actionPerformed(ActionEvent e)
    {
            repaint();
    }

...

    public class Display extends JPanel
        {
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);

                level_class.paint(g, player_class);
                player_class.paint(g,main.this);
                shoot_class.paint(g);
            }/** end of paint method ***/
        }

...
}//end of main method

i also trying adding repaint() after level_class.hitRIGHT(); and level_class.hitLEFT(); method but no change.

Calling repaint will only help if you call it on the component that actually needs to be repainted, but if you do call it on that component then it is guaranteed to solve your problem and it is the usual way of doing things.

Where do you create an instance of Display and are you calling repaint on that instance?

but if you do call it on that component then it is guaranteed to solve your problem and it is the usual way of doing things.

i am not that good with java. can u plz explain this part lil more to me?

and i have something like this for display.

public class main extends JApplet implements ActionListener, KeyListener, MouseListener
{
   Display display_class;
   ....
   public void init()
    {
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        display_class = new Display(); 
        setContentPane(display_class);  
        ...
    }
    ...
    public class Display extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);  
             ....
        }
   }
   ....
}

The panel that needs to be repainted is display_class, so when you change any values in the simulation you need to call display_class.repaint(); to see the result.

ps I find your variable names quite bizarre - I hope they're not the result os some misunderstanding? Eg you have an instance of Display which you name display_class. It's not a class, it's an instance. It would make more sense if it was named displayInstance or theDisplay or somesuch. There's also a Java convention that variable names use "camel case" (like my examples) rather then underscores (except for constants that are all upper case)

thanks, ill change the variable names right after this theard.

i tried putting display_class.repaint() in public void actionPerformed(ActionEvent e) method but still there is half a sec lag.

For smooth movement, this is how people normally do it...
in your game loop (timer actionPerformed) you execute the camera_pos_x += camera_speed; and call repaint() every time. cameraSpeed is initially zero.
When the user presses the Right key, you set camerSpeed to +5. When the user releases the key you set cameraSpeed back to zero. That way the camera will move smoothly right for as long as the key is down.
You don't say how often your timer fires, something like 25/sec (40 mSecs delay) would be a good starting value.

ah got it to working thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.