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;
}
}
}
MSN defined our generation in some ways, kind of like Snapchat and TikTok have done for future generations.
I have great memories of the MSN era in the late 90s / early 2000s. In the UK everyone seemed to come home from School and go on MSN for the evening. We didn't really have mobile phones then, so other than going and knocking on your friends door it was a totally new way of interacting with people. I also loved how I could talk to people I’d met playing online games from around the world.
Inviting people to NetMeeting and messing about with the shared white board and webcams was pretty fun, even if webcams only ran at a couple of fps over dial-up.
All the random things you could do with MsgPlus! were really fun - I suspect that made a few people jump with /shello randomly blasting Mr Hankey out their speakers!
Maybe I’m just nostalgic, however I do feel the internet and computers were more fun back then.
I actually agree with "Good Bot". "millions" - I wonder how they made that assertion. Did they guess? If so, not good enough. Hence "clickbait". If they said "five people" it'll still be clickbait. It's clickbait unless there are actual proven figures to substantiate that claim
I think every American should have a course in a 'dry sense of humour' at school; and perhaps 'using sarcasm in jest' oh, and also 'the use or irony in humour'.
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