#Help me change the ball shape from Rectangle to Oval or circle

6 messages · Page 1 of 1 (latest)

terse hornet
#

I've tried this solution:
gc.fillOval( go.topX, go.topY, go.width, go.height );

But it changes all the gameobjects to oval. I want only the ball to change!

Part of the code:

  public void drawPicture()
    {
        // the game loop is running 'in the background' so we have
        // add the following line to make sure it doesn't change
        // the model in the middle of us updating the image
        synchronized ( model ) 
        {
            // get the 'paint brush' to pdraw on the canvas
            GraphicsContext gc = canvas.getGraphicsContext2D();

            // clear the whole canvas to white
            gc.setFill( Color.WHITE );
            gc.fillRect( 0, 0, width, height );
            
            // draw the bat and ball
            displayGameObj( gc, ball );   // Display the Ball
            displayGameObj( gc, bat  );   // Display the Bat

            // *[2]****************************************************[2]*
            // * Display the bricks that make up the game                 *
            // * Fill in code to display bricks from the brick array      *
            // * Remember only a visible brick is to be displayed         *
            // ************************************************************
            for (GameObj brick: bricks) {
                if (brick.visible) {
                    displayGameObj(gc, brick);
                }
            }  
            
            // update the score
            infoText.setText("BreakOut: Score = " + score);
        }
    }

    // Display a game object - it is just a rectangle on the canvas
    public void displayGameObj( GraphicsContext gc, GameObj go )
    {
        gc.setFill( go.colour );
        gc.fillRect( go.topX, go.topY, go.width, go.height );
    }
finite sierraBOT
#

This post has been reserved for your question.

Hey @terse hornet! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

lucid anvil
#

is ball a subclass of gameObj ?

terse hornet
#

@lucid anvil no, but this is how i made the ball,
ball = new GameObj(width/2, height/2, BALL_SIZE, BALL_SIZE, Color.RED );

lucid anvil
#

this won't work. Why not use an abstract class that defines the basics of a game object and extend it in subclasses which implement the respective behavior.

public abstract class GameObject {
  //Basic fields all GameObjects should have
  //Some methods that all GameObjects should have
  public abstract void displayGameObj(GraphicsContext gc);
}

public class Rectangle extends GameObject {
  //Fields special to the Rectangle class
  @Override
  public void displayGameObj(GraphicsContext gc) {
  //Implementation special to the Rectangle class.
  }
}

public class Circle extends GameObject {
  //Fields special to the Circle class
  @Override
  public void displayGameObj(GraphicsContext gc) {
  //Implementation special to the Circle class.
  }
}