• 0

[JAVA] Drawing


Question

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 KillerZ123
Link to comment
https://www.neowin.net/forum/topic/848226-java-drawing/
Share on other sites

2 answers to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Well i'll look into a docking station if needed and use that.    Normally i don't usually have all the drives connected at once,  usually once a month to sync the latest files, and then they go back in there storage area   
    • memories! completely forgot about alcohol 120%!!!! man this list just makes me think of all the exciting times! everything's become so sterilized these days. 
    • Salesforce to acquire Fin, formerly Intercom, for over $3 billion by David Uzondu Image via DepositPhotos.com Salesforce today announced that it has reached an agreement with customer support software company Fin to buy the company for around $3.6 billion. Salesforce expects the transaction to close in the fourth quarter of its fiscal year 2027, and the acquisition will not change its financial guidance previously announced on May 27. Marc Benioff, CEO of Salesforce, in its press release, claimed that acquiring the startup gives Salesforce an immediate AI agent that resolves support tickets across email, WhatsApp, SMS, and Slack: You might know Fin by its former name, Intercom. If you have ever been on a website or inside a mobile app and noticed a little chat bubble widget floating in the bottom right corner of your screen, often featuring a friendly face and a message like, "Hi! How can we help you today?" you were almost certainly looking at Intercom. Intercom became Fin just last month, transforming itself into an "AI-first" platform that handles customer issues with little need for humans to intervene. The new name originates from the company's highly successful AI customer service agent, which it launched way back in 2023. This digital assistant supposedly resolves about 76% of customer service volume end-to-end on its own, so the business rebranded to match its primary software tool. Fin's new owners, Salesforce, went on an acquisition spree over the last few years, and some of them you might recognize, like its 2020 $27.7 Billion acquisition of Slack. The last few months saw the enterprise giant buy several startups, including m3ter, Momentum, Cimulate, and Contentful. Salesforce said that when the Fin deal pulls through, customers will "deploy AI agents across" various service operations, which will complement Agentforce, the platform that enables businesses to deploy customizable autonomous digital workers.
  • Recent Achievements

    • One Year In
      ThatGuyOnline earned a badge
      One Year In
    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      +Edouard
      201
    3. 3
      PsYcHoKiLLa
      127
    4. 4
      Steven P.
      82
    5. 5
      ATLien_0
      76
  • Tell a friend

    Love Neowin? Tell a friend!