Sahib Saini
Sahib Saini

Reputation: 25

Draw multiple rectangles in one line

i'm trying to make a game where if the ball hits a rectangle, the value of the rectangle goes down and once it reaches 0, it would disappear. I haven't implemented the counting variable but got into a problem, i'm trying to render a list of arrays along the top, but my loop only renders one object when i'm trying to draw multiple rectangles and have them stay there so the ball could react with it. How would I do it?

package graphics;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

public class Ballz extends LASSPanel
{
//Variables for the circle (Global Variables)

int circleX, circleY, circleSize;
double ballDirection,circleDX, circleDY;


//Colors
Color circleColor;
Color backgroundColor;
Dimension size;

int turn = 1;
int click =0;
int go =0;
int move = 0;
boolean roundEnd;



//Variables for the map
int brickWH;
int[] rect = new int[12];



public Ballz()
{
    //Ball Variables
    size = new Dimension(0,0);


    circleX = 190;
    circleY = 545;
    circleDX = 0;
    circleDY=0;

    circleSize = 15;

    circleColor = new Color (245,245,245);
    backgroundColor = new Color (28,28,28);

    //Game Variables
    roundEnd = true;

    brickWH = 18;
}

public void update()
{
    //Get size of screen
    getSize(size);




    circleX += circleDX;
    circleY += circleDY;



    //Screen borders
    if (circleX >= (size.width) - circleSize)
    {
        circleDX = -circleDX;
        circleX = size.width-circleSize;

    }
    if (circleX <= 0)
    {
        circleDX = -circleDX;
        circleX = 0;
    }
    if (circleY >= (size.height -circleSize))
    {
        circleDX =0;
        circleDY = 0;
        circleX = 190;
        circleY = 545;
        roundEnd = true;
    }
    if (circleY <= 0)
    {
        circleDY = -circleDY;
        circleY = 0;
    }


    circleX += circleDX;
    circleY += circleDY;


    click = getMouseButton(0);

    if (click ==1)
    {
        circleDY = -5;
        circleDX = -3;
    }


    //Rectangle Loop


    for (int i =0; i<rect.length; i++)
    {
        rect[i] = rect[i] +50;
    }





    repaint();

}

public void paint(Graphics g)
{


    //Game Colors
    g.setColor(circleColor);
    g.fillOval(circleX, circleY, circleSize, circleSize);


    for (int i=0; i<rect.length; i++)
    {
        g.fillRect(rect[i], 2, brickWH, brickWH);

    }

    setBackground(backgroundColor); 







}

}

Upvotes: 0

Views: 649

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

Okay, so having seen a bit more of the code, lets start with...

int[] rect = new int[12];

This creates an array of ints which is initially initialised to all 0s

Next, you do...

for (int i =0; i<rect.length; i++)
{
    rect[i] = rect[i] +50;
}

All this is basically doing is adding 50 to 0 and assigning it back to the array element, so when you do...

for (int i=0; i<rect.length; i++)
{
    g.fillRect(rect[i], 2, brickWH, brickWH);

}

it's just painting each "brick" on top of the last, because they're all at the same horizontal position

What really, really, really, stands out to me is...

for (int i =0; i<rect.length; i++)
{
    rect[i] = rect[i] +50;
}

Is it your intention that each brick will be 50 pixels apart? In which case you should probably do something more like...

int xPos = 0;
for (int i =0; i<rect.length; i++)
{
    rect[i] = xPos;
    xPos += 50;
}

If, instead, they're suppose to be placed together, then you'd want something more like...

for (int i =0; i<rect.length; i++)
{
    rect[i] = brickWH * i;
}

Upvotes: 1

Related Questions