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 9085409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:14:15+00:00 2026-06-16T21:14:15+00:00

I have coded a simple Java game where there are two rectangles on the

  • 0

I have coded a simple Java game where there are two rectangles on the screen, one of the rectangles should move and the other should stays still, the moving Rectangle moves with keyboard arrow input and can move either up, down, left or right. The problem I am having is that both rectangles are moving when only one of them should (rectOne) and rectTwo should stay still, I have my variables set up as shown:

    float buckyPositionX = 0;
    float buckyPositionY = 0;
    float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
    float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
//my two rectangles are shown bellow 
java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);

the coordinates variables buckyPositionX and buckyPositionY are 0,0 so they show the two left corner of my screen and the coordinate variables shiftX and shiftY show the coordinates at the centre of the screen so that the moving rectangle is always centred.

The Play class which has all of the game code inside of it is made up of a few methods, the important ones are update and render, the update deals with the rectangle movement speeds and keyboard inputs to show you an example of what is inside the update class, here is a sample of the code for when they up key is pressed:

if(input.isKeyDown(Input.KEY_UP)){buckyPositionY += 2;}

That is done for all keyboard arrow inputs. Next is the render method which deals with drawing everything on the screen and the graphics, this method includes drawing the rectangles on the screen which were previously coded with the variables.

So, I have two rectangles drawn on the screen when I try move the rectangle with the keyboard input both the rectangles move together at the same time, I think there is a problem with how I have set up my rectangle coordinates and have tried playing about with them for example removing the +buckyPositionX and +buckyPositionY from them but the same problem occurs, if you have any ideas of what may solve this problem please tell me.

Thank you in advance.

Edit1:

Here is my full code for my play class as requested:

    imports are here

    public class Play extends BasicGameState{

    Animation bucky, movingUp, movingDown, movingLeft, movingRight;
    Image worldMap;
    int[] duration = {200, 200};//how long frame stays up for
    float buckyPositionX = 0;
    float buckyPositionY = 0;
    float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
    float shiftY = buckyPositionY + 160;//the numbers are half of the screen size

    java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
    java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);

    public Play(int state){
    }   
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
          worldMap = new Image("res/world.png");
          Image[] walkUp = {new Image("res/b.png"), new Image("res/b.png")}; //these are the images to be used in the "walkUp" animation
          Image[] walkDown = {new Image("res/f.png"), new Image("res/f.png")};
          Image[] walkLeft = {new Image("res/l.png"), new Image("res/l.png")};
          Image[] walkRight = {new Image("res/r.png"), new Image("res/r.png")};

    movingUp = new Animation(walkUp, duration, false);
    movingDown = new Animation(walkDown, duration, false);  
    movingLeft = new Animation(walkLeft, duration, false);  
    movingRight = new Animation(walkRight, duration, false);
    bucky = movingDown;//facing screen initially on startup
    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
    worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0
    bucky.draw(shiftX, shiftY);//makes him appear at center of map

    g.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight());
    g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());
}

    public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
    Input input = gc.getInput();

    //up
    if(input.isKeyDown(Input.KEY_UP)){
        bucky = movingUp;//changes the image to his back
        buckyPositionY += 2;;//increase the Y coordinates of bucky (move him up)
        if(buckyPositionY>162){//if I reach the top 
            buckyPositionY -= 2;//stops any further movement in that direction
        }
    }

    //down
    if(input.isKeyDown(Input.KEY_DOWN)){
        bucky = movingDown;
        buckyPositionY -= 2;
        if(buckyPositionY<-550){
            buckyPositionY += 2;//basically change the direction if + make -
    }}
    //left
    if(input.isKeyDown(Input.KEY_LEFT)){
        bucky = movingLeft;
        buckyPositionX += 2;
        if(buckyPositionX>324){
            buckyPositionX -= 2;//delta * .1f
    }}
    //right
    if(input.isKeyDown(Input.KEY_RIGHT)){
        bucky = movingRight;
        buckyPositionX -= 2;
        if(buckyPositionX<-776){
            buckyPositionX += 2;
    }}}   


    public int getID(){
        return 1;
    }}

ANSWERING QUESTIONS:

The reason I used the variables buckyPositionX and buckyPositionY in my rectTwo variables is because when I try to draw this using the g.fillRect function everything works fine with those inputs, rectOne moves and RectTwo stays still but when I call the exact same rectangles from the variables by saying g.fillRect(rectOne.X,rectOne.Y…) I get this problem where both the rectangles are moving together which is strange because the coordinates and the sizes are the exact same as when they are drawn using g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150) and same for rectOne. I will try make it more clear with some code:

If I use this in my variables:

java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
        java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);

and this in my render method:

 g.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight());
        g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());

Both of the rectangles, rectOne and rectTwo move together.

but if I use this in my render method without having any variable set:

g.fillRect(shiftX, shiftY,90,90)
g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150)

Everything works VISUALLY although my collisions dont work with this method (which is why I want to fix it) the rectangles both appear on the screen and rectOne moves whilst rectTwo stays still.

I will make a new post explaining my problem in more detail as this one has started getting too crowded and off topic.

  • 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-16T21:14:16+00:00Added an answer on June 16, 2026 at 9:14 pm

    I guess this question to related to this?

    Anyway, but rectangles is dependent on buckyPositionY so by updating that variable you are in fact updating both rectangles.

    Try something like this instead. Preferably you would just update the coordinates to rectOne but from what I could see there is no setX method to Rectangle2D

    if (input.isKeyDown(Input.KEY_UP)){
      rectOne = new Rectangle2D.Float((float)rectOne.getX(), (float)rectOne.getY()+2, (float)90, (float)90);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have coded a simple Java game where there are two rectangles on the
I have coded a simple 2D Java game where there is a overhead view
I have coded a pretty simple Java 2D game with only two threads -
I'm writing a very simple Java Game. Let me describe it briefly: There are
After creating a simple java craps game, I decided to have it test for
I have begun to write a simple platform game in java. As a test,
I have a simple game animation made in java. It is of three planets
I have a simple java code that reads text csv file that contains sentences
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {

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.