How to use the Java class libraries

13
How to use the Java class libraries Brief documentation of how to do this all with Java.

description

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. - PowerPoint PPT Presentation

Transcript of How to use the Java class libraries

Page 1: How to use the Java class libraries

How to use the Java class libraries

Brief documentation of how to do this all with Java.

Page 2: How to use the Java class libraries

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

Page 3: How to use the Java class libraries

Using DrJava

Page 4: How to use the Java class libraries

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, filenameC:\intro-prog-java\mediasources\catapillarClipart.jpg height 181width 360

Page 5: How to use the Java class libraries

Methods of Pictures

show() repaint() explore() – opens a Picture explorer Pixel [] getPixels() Pixel getPixel(x,y) int getWidth() int getHeight() writePictureTo(filename)

Page 6: How to use the Java class libraries

Methods of Pixels

int getGreen(), getRed(), getBlue() setGreen(value), setRed(value), setBlue(value) Color getColor() setColor(color) int getX(), getY()

Page 7: How to use the Java class libraries

Methods of Color

new Color(red, green, blue) ColorChooser.pickAColor() SimplePicture.getColorDistance(color1,color2) color.darker(), color.brighter()

Page 8: How to use the Java class libraries

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)); } }

Page 9: How to use the Java class libraries

/** * 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 columns for (int sourceX = 0, targetX=0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++)

{ // 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();

return targetPicture; }

Page 10: How to use the Java class libraries

Basic sound opening & playing

> Sound mySound = new Sound(FileChooser.pickAFile());> mySound.play()> mySoundSound file: thisisatest.wav length: 129026> int asample = mySound.getSample(1);> asample6852> mySound.setSample(1,-17);> mySound.getSample(1)-17

Page 11: How to use the Java class libraries

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.

Page 12: How to use the Java class libraries

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("SoundException occured");

}

} }

Page 13: How to use the Java class libraries

Example using decreaseVolume

> Sound mySound = new Sound(FileChooser.pickAFile());> mySoundSound file: thisisatest.wav length: 129026> mySound.getSample(1)6852> mySound.getLengthInFrames()64513> mySound.decreaseVolume();> mySound.play()> mySound.getSample(1)3426