MODIFYING PICTURES USING LOOPS
A monitor with 640 by 480 has 307200 pixels
640 X 480 = 307200 pixels
(H) (V)
A pixel has three colors r, g, and b
|
|
R |
G |
B |
|
BLACK |
0 |
0 |
0 |
|
White |
255 |
255 |
255 |
|
RED |
255 |
0 |
0 |
|
BLUE |
0 |
0 |
255 |
Java Color class
import java.awt.Color; //need to import this library abstract Windows ToolKit
Color newColor = new Color ( 255, 0, 0)
System.out.println(newColor); // Red = 255 , Green =0 Blue =0
Picture class
Image file extensions are bmp, jpg or gif.
Example display a picture or an image
>String filename= FileChooser.pickAFile( );
>Picture picObject = new Picture ( filename);
> picObject show( );
Object methods of the Picture class
show( )
explore ( )
getWidth( )
getHeight ( )
getPixel ( )
getPixels ( )
Pixel class
Picture picObj = new Picture( “c:/A/happy.gif”);
Pixel pixelObj = picObj.getPixel (0, 0) ;
System.out.println( pixelObj); //Pixel Red= 140 Green= 80 Blue = 250
int red;
red = pixelObj.getRed ( ) ;
System.out.println ( red) ; // 140
red= (int) ( red * 0.5) ;
pixelObject.setRed (red);
System.out.println(pixelObject.getRed ( )); //70
Object methods of class Pixel
getRed ( )
getBlue ( )
getGreen ( )
setRed ( )
setBlue ( )
setGreen ( )
setColor( )
Example
Picture picObject = new Picture ( “C:/A/happy.gif”);
Pixel [ ] pixelArray = picObject.getPixels ( );
System.out.println( pixelArray [0].getBlue ( ));
Example:
Creating a method called decreaseRed in the Picture class
public void decreaseRed ( )
{ Pixel [ ] pixelArray = this.getPixels ( );
Pixel pixelObject = null;
int redvalue;
for ( int i= 0; i <pixelArray.length ; i = i +1 )
{pixelObject = pixelArray [i] ;
redvalue = pixelObject.getRed();
redvalue= (int) (redvalue *0.6) ;
pixelObject.setRed ( redvalue) ;
}
}
Calling the method decreaseRed
Ø Picture picObject = new Picture ( “C: / happy.gif”);
Ø picObject.show();
Ø picObject.decreaseRed ( ) ;
Ø picObject.repaint ( ) ;
Using loop to solve pixel problems
In the Picture class, create a method named decreaseRed.
Pass an amount of decreasing red to the method.
Call the method decreaseRed in the main METHOD .
String filename = “C:/A/happy.gif”;
Picture picObj = new Picture (filename);
picObj.show( );
picObj.decreaseRed ( 0.7);
picObj.repaint ( );
In the Picture class create the Method decreaseRed
- use a for- each loop
public void decreaseRed (double redpct)
{ int value = 0;
Pixel [] pixelArray = this.getPixels ( );
for( Pixel pixelObj : pixelArray)
{ value = pixelObj.getRed ( );
value = (int )(value * redpct);
pixelObj.setRed( value);
} // Close for loop
} // Close Method
- use a while loop
public void decreaseRed ( double redpct)
{ Pixel [] pixelArray = this.getPixels( ); |
Pixel pixelObj = null;
int value = 0;
int i = 0 ;
while ( i <pixelArray.length)
{ pixelObj = pixelArray [i];
value = pixelObj.getRed ( );
value = ( int ) ( value * redpct);
pixelObj.setRed(value);
i = i + 1;
}//Close loop
} // Close Method
- use a for loop
public void decreaseRed ( double redpct )
{ Pixel [ ] pixelArray = this.getPixels ( );
int value = 0;
Pixel pixelObj = null ;
for( i = 0; i = < pixelArray.length; i = i + 1)
{ pixelObj = pixelArray [i];
value = pixelObj.getRed ( );
value = ( int ) ( value * redpct) ;
pixelObj.setRed ( value ) ;
} //close loop
} //close method