Trying to edit a picture in java program

Simple java question.. code provided

Page 1 of 1

4 Replies - 1908 Views - Last Post: 07 October 2008 - 11:02 AM Rate Topic: -----

#1 bonis317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 08

Trying to edit a picture in java program

Post icon  Posted 06 October 2008 - 10:59 AM

Here is the program I have... it prints a car with # signs in the terminal window. I am just a little confused on how this works and how I would go about replacing the car with a new picture (this is the overall goal of the assignment.. get the program to run and replace the original image with something new). It has been 2 years since I took a Java course, we have no text book, and I can't understand my professor when I go to him for help (seeing as programming is quite difficult to teach). I know I have to edit the code inside of the try catch block, but every time I try to create a new image, the program has an error. I guess maybe I don't understand how this coordinate system is working. Any help / resources would be appreciated... Thanks to all.

class Art{
  
	public static void main( String [] args ){
  
	   System.out.println( "Type q or nothing + Enter" );
  
	   while( true ){
  
		 try{
  
			int c = System.in.read();
  
			if( c == q ){ break; }
  
			clear();
  
			moveTo( (int)  Math.round( Math.random()*50 )
  
					 ,
  
					(int) Math.round( Math.random()*50 )
  
				  );
  
			move(1,1);   paintRect(8,8);
  
			move(4,8);   paintRect(4,4);
  
			move(4,0);   paintRect(2,2);
  
			move(0,-6);  paintRect(2,2);
  
			move( 10,0);
  
			paintText( "Type q or nothing + Enter" );
  
		 } catch ( java.io.IOException e ){ System.err.println( e ); }
  
	}  }
  
 
  
	static char escape = (char) 27;
  
	static char pen = #;
  
	static int row = 0, col = 0;
  
 
  
	static void clear(){
  
	  System.out.print( escape + "[2J" );
  
	  row = 0;
  
	  col = 0;
  
	}
  
	static void setPen(char p){
  
	   pen = p;
  
	}
  
	static void moveTo(int r, int c){
  
	   row = r;
  
	   col = c;
  
	}
  
	static void move(int r, int c){
  
	   row += r;
  
	   col += c;
  
	}
  
	static void paintRect( int  width, int  height )
  
	{
  
	  for( int i=0; i<height; i++) {
  
		for( int j=0; j<width; j++ ){
  
		   System.out.print( escape + "["
  
				+ (row+i) + ";" + (col+j) + "H" );
  
		   System.out.print( pen );
  
	} } }
  
	static void paintText( String text ){
  
		System.out.print( escape + "[" + row + ";" + col + "H" );
  
		System.out.print( text );
  
	}
  
 }



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to edit a picture in java program

#2 devonator  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-September 08

Re: Trying to edit a picture in java program

Posted 07 October 2008 - 07:44 AM

you did
if( c == ’q’ ){ break; }


and
static char pen = ’#’;



which cause the compiler unable to compile.

cuz it should be

if( c == 'q' ){ break; }


and
static char pen = '#';



see the difference on the symbols? (’q’ and 'q' big difference - ' this key is located above the / and on the left side of the \ key do not use the ` key on the left side of 1 cuz IT'S WRONG! lol joking - but seriously its wrong) There are many similar mistakes in your programing, I fixed enough now it compiles and displays your text image.

now have fun with those code, oh and your welcome. Hope it helps!


class Art{
  
	public static void main( String [] args ){
  
	   System.out.println( "Type 'q' or nothing + Enter" );
  
	   while( true ){
  
		 try{
  
			int c = System.in.read();
  
			if( c == 'q' ){ break; }
  
			clear();
  
			moveTo( (int)  Math.round( Math.random()*50 )
  
					 ,
  
					(int) Math.round( Math.random()*50 )
  
				  );
  
			move(1,1);   paintRect(8,8);
  
			move(4,8);   paintRect(4,4);
  
			move(4,0);   paintRect(2,2);
  
			move(0,-6);  paintRect(2,2);
  
			move( 10,0);
  
			paintText( "Type 'q' or nothing + Enter" );
  
		 } catch ( java.io.IOException e ){ System.err.println( e ); }
  
	}  }
  

  
	static char escape = (char) 27;
  
	static char pen = '#';
  
	static int row = 0, col = 0;
  

  
	static void clear(){
  
	  System.out.print( escape + "[2J" );
  
	  row = 0;
  
	  col = 0;
  
	}
  
	static void setPen(char p){
  
	   pen = p;
  
	}
  
	static void moveTo(int r, int c){
  
	   row = r;
  
	   col = c;
  
	}
  
	static void move(int r, int c){
  
	   row += r;
  
	   col += c;
  
	}
  
	static void paintRect( int  width, int  height )
  
	{
  
	  for( int i=0; i<height; i++) {
  
		for( int j=0; j<width; j++ ){
  
		   System.out.print( escape + "["
  
				+ (row+i) + ";" + (col+j) + "H" );
  
		   System.out.print( pen );
  
	} } }
  
	static void paintText( String text ){
  
		System.out.print( escape + "[" + row + ";" + col + "H" );
  
		System.out.print( text );
  
	}
  
}


This post has been edited by devonator: 07 October 2008 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Trying to edit a picture in java program

Posted 07 October 2008 - 08:18 AM

This:
--> if( c == 'q' ){ break; }
Can also be written as:
--> if(c == 'q') break;
Without the { } braces. The { } braces are only required if more than 1 statement is following the if statement
Was This Post Helpful? 0
  • +
  • -

#4 bonis317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 08

Re: Trying to edit a picture in java program

Posted 07 October 2008 - 09:43 AM

Actually, the code worked alreeady to print the car.. I just have to replace the car with something else (a different image)... And you must not have read, I don't understand how it is printing the car.. this is pretty much my first Java class... and I feel so lost already
Was This Post Helpful? 0
  • +
  • -

#5 devonator  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-September 08

Re: Trying to edit a picture in java program

Posted 07 October 2008 - 11:02 AM

View Postbonis317, on 7 Oct, 2008 - 09:43 AM, said:

Actually, the code worked alreeady to print the car.. I just have to replace the car with something else (a different image)... And you must not have read, I don't understand how it is printing the car.. this is pretty much my first Java class... and I feel so lost already



well if you wrote the code that u posted above, you should know that the picture above was printed because you painted it with text letters. Like the java point function, you were able to draw rectangle and other shapes at specific co-ordinate.

eg.
move(1,1); paintRect(8,8);


also u sat variable to different letters using the math function, therefore different letter comes out, and u did some small for loops to fill out the lines and empty spaces with certain characters.
and if you wanna another image, you'll have to redraw it co-ordinate by co-ordinate (if you actually did write this program) which requires to remove all the co-ordinate and most of the codes off from the program.

heres the program in english (a quick not so wordy version):
if q is press than quite otherwise when enter is pressed
draw an object as certain co-ordinate;
fill out the space;
print;

neitherway the typing mistake error ` and ' i described above was corret, cuz if u dont fix it the compiler wont compile, cuz thats the way java is.\\hope thing work out for you, good luck in ur java class

This post has been edited by devonator: 07 October 2008 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1