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;
}
}
}
Maybe I missed it, but does this say anywhere that the game save bug has been squashed?
I haven't encountered it myself, but it would be nice to know I'm good to go.
Anyway, amazingly well done game. Mostly more of the same. ...but when the same is best in class with improved graphics and features, then a win.
Well when your game flops, you should expect this. If I do bad at work, I would expect a layoff.
Less than 1600 people played it on steam.
https://steamdb.info/app/1934570/charts/
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
https://www.neowin.net/forum/topic/848226-java-drawing/Share on other sites
2 answers to this question
Recommended Posts