looping for ascii art and scaling resizing with Java

Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

looping for ascii art and scaling resizing with Java

Post by darknkreepy3# »

Here is a tutorial I wrote while assisting a student understand the logic of scaling output in a simple command prompt ascii art of the lincoln center. understanding output characters that need escaping like the backslash (\) and taking space off of each side to match the requirements of the assignment were figured out.

Code: Select all

/*
tutorial by Kristoffe Brodeur. ©2010 All Rights Reserved
09-01-2009 The Lincoln Center ASCII Rendering
*/
class TheLinc
	{
	public int LINC_SIZE;
	//
	public String welcome;
	public String art_parkingLot;
	public String art_stadium;
	public String temp;
	//
	public String d_edgeV;
	public String d_edgeH;
	public String d_field;
	public String d_seatL;
	public String d_seatR;
	public String d_spacer;
	public String d_grass;
	//-----
	public TheLinc()
		{
		welcome="\n\n **The Lincoln Center ASCII Rendering** \n\n";
		//example of not using magic anything (numbers, strings, etc), magic meaning an value that can only be modified where it is used
		//non-magic is similar to css in html. change one value and where it is used is changed everywhere (LINC_SIZE is an example)
		LINC_SIZE=5;//the size of this value can be from 3 to 8 for a normal cmd output
		//makes drawing the art modifyable and modular
		d_edgeV="|";
		d_edgeH="_";
		d_field="*";
		d_seatL="/";
		d_seatR="\\";
		d_grass=".";
		// backslash is an escape char for special chars
		//so using (2) makes the string = the special char backslash itself
		d_spacer=" ";		
		welcome="\n\nThe Lincoln Center ASCII Rendering\nLINC_SIZE="+LINC_SIZE+"\n\n";
		System.out.print(welcome);
		}
	//-----
	String draw(String bChar, int count)
		{
		temp="";
		//
		for(int a=0;a<count;a++)
			{
			temp+=bChar;
			}
		return(temp);
		}
	//-----
	String draw_parkingLot()
		{
		art_parkingLot="\n"+draw(d_spacer,LINC_SIZE*2+1)+draw(d_edgeH,LINC_SIZE*4+1)+"\n";
		//add 1 in spacing for the edge of the parking lot
		//add 1 in top width for center of parking lot
		int vSpots=LINC_SIZE*4;
		//
		for(int a=0;a<vSpots;a++)
			{
			art_parkingLot+=draw(d_spacer,LINC_SIZE*2)+draw(d_edgeV,1)+draw(d_edgeH,LINC_SIZE*2)+draw(d_edgeV,1)+draw(d_edgeH,LINC_SIZE*2)+draw(d_edgeV,1)+"\n";
			}
		return(art_parkingLot);
		}
	//-----
	String draw_stadium()
		{
		art_stadium="\n";
		//top of field
		art_stadium+=draw(d_edgeH,LINC_SIZE*8+2+2)+"\n";//top edge is size of grass and field + edges + center
		art_stadium+=draw(d_edgeV,1)+draw(d_spacer,LINC_SIZE*4)+draw(d_edgeH,2)+draw(d_spacer,LINC_SIZE*4)+draw(d_edgeV,1)+"\n";
		int spacing=0;
		int dots=0;
		//top half of field, seats face out
		for(int a=0;a<LINC_SIZE*2;a++)
			{
			art_stadium+=draw(d_edgeV,1);
			spacing=LINC_SIZE*4-a*2-2;
			art_stadium+=draw(d_spacer,spacing);
			art_stadium+=draw(d_edgeH,1)+draw(d_seatL,1);//seat
			//
			if(a>=LINC_SIZE)
				{
				dots=LINC_SIZE*8-spacing*2-LINC_SIZE*2-2;//grass on each side is the linc size -2 for edge -the spacing calculated above divided by 2
				art_stadium+=draw(d_grass,dots/2)+draw(d_field,LINC_SIZE*2)+draw(d_grass,dots/2);				
				}
			else
				{
				//just draw grass and that is the linc size -the spacing calculated above divided by 2, negating any playing field
				dots=LINC_SIZE*8-spacing*2-2;
				art_stadium+=draw(d_grass,dots);
				}
			art_stadium+=draw(d_seatR,1)+draw(d_edgeH,1);//seat
			art_stadium+=draw(d_spacer,spacing);
			art_stadium+=draw(d_edgeV,1)+"\n";
			}
		//bottom half of field, seats face in (draw calls just reversed from above)
		for(int a=LINC_SIZE*2-1;a>-1;a--)
			{
			art_stadium+=draw(d_edgeV,1);
			spacing=LINC_SIZE*4-a*2-1;
			//use 1 instead of 2, because there is only 1 less space for each side than the top side
			//making an indent in the seats and making the bottom chairs touch
			art_stadium+=draw(d_spacer,spacing);
			art_stadium+=draw(d_seatR,1)+draw(d_edgeH,1);//seat
			//
			if(a>LINC_SIZE-1)
				{
				dots=LINC_SIZE*8-spacing*2-LINC_SIZE*2-2;//grass on each side is the linc size -2 for edge -the spacing calculated above divided by 2
				art_stadium+=draw(d_grass,dots/2)+draw(d_field,LINC_SIZE*2)+draw(d_grass,dots/2);				
				}
			else
				{
				dots=LINC_SIZE*8-spacing*2-2;
				art_stadium+=draw(d_grass,dots);
				}
			art_stadium+=draw(d_edgeH,1)+draw(d_seatL,1);//seat
			art_stadium+=draw(d_spacer,spacing);
			art_stadium+=draw(d_edgeV,1)+"\n";
			}
		art_stadium+=draw(d_edgeV,1)+draw(d_edgeH,LINC_SIZE*8+2)+draw(d_edgeV,1);//bott is size of field + grass center - sides
		return(art_stadium);
		}
	//-----
	public static void main(String[] args)
		{
		TheLinc test1=new TheLinc();//[class] [instance name]
		System.out.print(test1.draw_parkingLot());
		System.out.print(test1.draw_stadium());
		}
	}
Post Reply