Download JavaAPI

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Nonogram wikipedia , lookup

Color wikipedia , lookup

Autostereogram wikipedia , lookup

Color vision wikipedia , lookup

Subpixel rendering wikipedia , lookup

Framebuffer wikipedia , lookup

Spatial anti-aliasing wikipedia , lookup

Apple II graphics wikipedia , lookup

Original Chip Set wikipedia , lookup

List of 8-bit computer hardware palettes wikipedia , lookup

BSAVE (bitmap format) wikipedia , lookup

Indexed color wikipedia , lookup

Hold-And-Modify wikipedia , lookup

Transcript
How to use the
Java class libraries
Brief documentation of how to do
this all with Java.
Java materials


We’re developing Java versions of our Media
Computation materials for the teachers workshops
that we’re developing.
Original classes by Guzdial for Jython, revised by
students, and now made readable by Barb Ericson
Using DrJava
Basic picture opening & showing
> String fileName = FileChooser.pickAFile();
> System.out.println(fileName);
C:\intro-prog-java\mediasources\catapillarClipart.jpg
> Picture picture=new Picture(fileName);
> picture.show();
> System.out.println(picture);
Picture, filename
C:\intro-prog-java\mediasources\catapillarClipart.jpg height 181
width 360
Methods of Pictures








show()
repaint()
explore() – opens a Picture explorer
Pixel [] getPixels()
Pixel getPixel(x,y)
int getWidth()
int getHeight()
writePictureTo(filename)
Methods of Pixels





int getGreen(), getRed(), getBlue()
setGreen(value), setRed(value), setBlue(value)
Color getColor()
setColor(color)
int getX(), getY()
Methods of Color




new Color(red, green, blue)
ColorChooser.pickAColor()
SimplePicture.getColorDistance(color1,color2)
color.darker(), color.brighter()
decreaseRed in Java
/**
* Method to decrease the red by
half in the current picture
*/
public void decreaseRed()
{
Pixel[] pixels = this.getPixels();
Pixel p = null;
int value = 0;
// loop through all the pixels
for (int i = 0; i < pixels.length; i++)
{
// get the current pixel
p = pixels[i];
// get the value
value = p.getRed();
// set the red value to half what it
was
p.setRed((int) (value * 0.5));
}
}
/**
* Method to copy Katie rotated to the left 90
degrees
* @return the picture after Katie has been
copied and rotated to the left 90
*/
public static Picture copyKatieSideways()
{
String sourceFile =
Picture.getMediaPath("KatieFancy.jpg");
Picture sourcePicture = new
Picture(sourceFile);
String targetFile =
Picture.getMediaPath("7inx95in.jpg");
Picture targetPicture = new
Picture(targetFile);
Pixel sourcePixel = null;
Pixel targetPixel = null;
{
// loop through the rows
for (int sourceY = 0, targetY =0;
sourceY < sourcePicture.getHeight();
sourceY++, targetY++)
{
// set the target pixel color to the source
pixel color
sourcePixel =
sourcePicture.getPixel(sourceX,sourceY);
targetPixel =
targetPicture.getPixel(targetY,targetX);
targetPixel.setColor(sourcePixel.getColor(
));
}
}
// show the source and target pictures
sourcePicture.show();
targetPicture.show();
// loop through the columns
for (int sourceX = 0, targetX=0;
sourceX < sourcePicture.getWidth();
sourceX++, targetX++)
return targetPicture;
}
Basic sound opening & playing
> Sound mySound = new Sound(FileChooser.pickAFile());
> mySound.play()
> mySound
Sound file: thisisatest.wav length: 129026
> int asample = mySound.getSample(1);
> asample
6852
> mySound.setSample(1,-17);
> mySound.getSample(1)
-17
Sound methods aren’t as clean yet

They’re still in the form we’ve used them in
Jython, not as Barb has been making them for
Java.
decreaseVolume
/** Method to decrease volume by
50%
*
*/
public void decreaseVolume()
{
int mySample = 0;
for (int index = 0;
index < getLengthInFrames()-1;
// getLengthInFrames currently
one too long
index++)
{
try {
mySample = getSample(index);
// getSample 0-based
mySample *= 0.5;
setSample(index,
(int) mySample);
} catch (SoundException ex) {
System.out.println("SoundExcepti
on occured");
}
}
}
Example using decreaseVolume
> Sound mySound = new Sound(FileChooser.pickAFile());
> mySound
Sound file: thisisatest.wav length: 129026
> mySound.getSample(1)
6852
> mySound.getLengthInFrames()
64513
> mySound.decreaseVolume();
> mySound.play()
> mySound.getSample(1)
3426