I am trying to draw a brick wall in java using loops and I am having problems with my columns loop. I can get it to draw a row but not to go down to the next row.
Edit: ok I got columns to work but now I can't figure out how to stagger the columns like a real brick wall.
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
public class BrickDriver
{
//---------------------------------------
// Creates the main frame of the program
//---------------------------------------
public static void main(String[] args)
{
int x, y; // x and y dimensions of frame
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the x and y dimensions: ");
x = scan.nextInt();
y = scan.nextInt();
JFrame frame = new JFrame ("Brick Wall");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new BrickPanel(x, y));
frame.pack ();
frame.setVisible (true);
}
}
import javax.swing.*;
import java.awt.*;
public class BrickPanel extends JPanel
{
private final int LENGTH_BRICK = 50; // max length of a brick
private final int WIDTH_BRICK = 20; // max width of a brick
private int length, width; // length and width of the frame
//---------------------------
// Sets up the drawing panel
//---------------------------
public BrickPanel (int x, int y)
{
length = x; // assigning the inputed length
width = y; // assigning the inputed width
setPreferredSize (new Dimension (length, width));
setBackground (Color.darkGray);
}
//------------------------------------------------------
// draws the bricks across the frame and down the frame
//------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);
int x, y; // x and y of the brick
x = 0; // initial x of the first brick
y = 0; // initial y of the first brick
for (int countY = 0; countY <= width; countY++) // draws the columns
{
for (int countX = 0; countX <= length; countX++) // draws the rows
{
page.setColor (Color.red);
page.fillRect (x, y, LENGTH_BRICK, WIDTH_BRICK);
x = x + 52;
}
x = 0;
y = y + 22;
}
}
}
Question
M4nB3arP1g
I am trying to draw a brick wall in java using loops and I am having problems with my columns loop. I can get it to draw a row but not to go down to the next row.
Edit: ok I got columns to work but now I can't figure out how to stagger the columns like a real brick wall.
I want mine to look like this: http://www.somethingyoushouldread.com/imag...ll_tileable.jpg
but mine is just straight columns.
import javax.swing.*; import java.awt.*; import java.util.Scanner; public class BrickDriver { //--------------------------------------- // Creates the main frame of the program //--------------------------------------- public static void main(String[] args) { int x, y; // x and y dimensions of frame Scanner scan = new Scanner(System.in); System.out.print ("Enter the x and y dimensions: "); x = scan.nextInt(); y = scan.nextInt(); JFrame frame = new JFrame ("Brick Wall"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new BrickPanel(x, y)); frame.pack (); frame.setVisible (true); } }import javax.swing.*; import java.awt.*; public class BrickPanel extends JPanel { private final int LENGTH_BRICK = 50; // max length of a brick private final int WIDTH_BRICK = 20; // max width of a brick private int length, width; // length and width of the frame //--------------------------- // Sets up the drawing panel //--------------------------- public BrickPanel (int x, int y) { length = x; // assigning the inputed length width = y; // assigning the inputed width setPreferredSize (new Dimension (length, width)); setBackground (Color.darkGray); } //------------------------------------------------------ // draws the bricks across the frame and down the frame //------------------------------------------------------ public void paintComponent (Graphics page) { super.paintComponent(page); int x, y; // x and y of the brick x = 0; // initial x of the first brick y = 0; // initial y of the first brick for (int countY = 0; countY <= width; countY++) // draws the columns { for (int countX = 0; countX <= length; countX++) // draws the rows { page.setColor (Color.red); page.fillRect (x, y, LENGTH_BRICK, WIDTH_BRICK); x = x + 52; } x = 0; y = y + 22; } } }Edited by KillerZ123Link to comment
Share on other sites
2 answers to this question
Recommended Posts