1 / 13

How to use the Java class libraries

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.

Download Presentation

How to use the Java class libraries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. Using DrJava

  4. 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

  5. Methods of Pictures • show() • repaint() • explore() – opens a Picture explorer • Pixel [] getPixels() • Pixel getPixel(x,y) • int getWidth() • int getHeight() • writePictureTo(filename)

  6. Methods of Pixels • int getGreen(), getRed(), getBlue() • setGreen(value), setRed(value), setBlue(value) • Color getColor() • setColor(color) • int getX(), getY()

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

  8. /** * 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)); } } decreaseRed in Java

  9. /** * 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; }

  10. 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

  11. 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.

  12. /** 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"); } } } decreaseVolume

  13. 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

More Related