Download presentation
Presentation is loading. Please wait.
1
January 2006Daniel Bryant1 Coursework Hints (part 2) : Week 4-5 Daniel Bryant Dept. of Computing University of Surrey d.bryant@surrey.ac.uk
2
January 2006Daniel Bryant2 The Purpose of My Labs Provide more information on the technical (Java) concepts that we will be looking at in the regular labs Introduce object-orientated/Java techniques you will require for your coursework I can also provide help with any problems from the regular labs or coursework These labs are designed to help you If you want information about a specific topic/concept within Java or have any comments please let me know!
3
January 2006Daniel Bryant3 Resources Please download a copy of these slides from the course website An excellent book to learn Java from the beginning is Head First Java (Second Edition) by Kathy Sierra and Bert Bates The Head First books contain a unique style (with lots of pictures and funny techniques), but they make learning a lot more enjoyable and really do help!
4
January 2006Daniel Bryant4 Resources You can download Java and NetBeans free for home use at http://java.sun.com/j2se/1.5.0/download.jsp http://java.sun.com/j2se/1.5.0/download.jsp Download Java 5.0 and NetBeans 4.1 (as used in the labs) Download Java 5.0 and NetBeans 5.0 (the newest version of NetBeans)
5
January 2006Daniel Bryant5 Today’s Lab Today we will discuss the week 4 and 5 coursework in more technical detail I will attempt to hint at the correct methodology that is needed to succeed in the coursework I will then examine the example MirrorXReflection code and attempt to identify the parts of the class you will need to modify in order to complete the coursework
6
January 2006Daniel Bryant6 Loading Multiple Images For the average and subtraction operations we need to load more than one image into our application The obvious way to do this is to specify two images in the application arguments But how do we access them in our application?
7
January 2006Daniel Bryant7 Modifying the “main” method public static void main(String[] argv) {... try { JFrame frame = new Coursework(argv[0]); frame.pack(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); System.exit(0); }... } public static void main(String[] argv) {... try { JFrame frame = new Coursework(argv); frame.pack(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); System.exit(0); }... } OriginalNew The constructor (which is going to call the average or subtract method) must have access to both filenames that were passed as arguments, not just the first one
8
January 2006Daniel Bryant8 Modifying the Constructor The constructor must be able to read an array of Strings, not just a single String. Therefore we modify the method signature of the constructor method (remember the constructor method will be named the same as your class) Old public Coursework(String image) throws IOException, ImageDecoderException New public Coursework(String[ ] arguments) throws IOException, ImageDecoderException
9
January 2006Daniel Bryant9 Modifying the Constructor We can now extract the two arguments from the array passed to the constructor (the array is named argument in this example) and load them into String variables public Coursework(String[] argument) throws IOException, ImageDecoderException { super("Coursework"); String imageFile = argument[0]; String imageFile2 = argument[1];... }
10
January 2006Daniel Bryant10 Modifying readImage The original readImage method only loads one image into the sourceImage class variable Therefore, I would recommend modifying this method to instead return a BufferedImage allowing the method to load as many images as necessary I’ve given this code to you on the next slide. Simply copy and paste this over the original readImage method.
11
January 2006Daniel Bryant11 New readImage Code public BufferedImage readImage(String filename) throws IOException, ImageDecoderException { ImageDecoder input = ImageFile.createImageDecoder(filename); BufferedImage newImage = input.decodeAsBufferedImage(); if (newImage.getType() != BufferedImage.TYPE_BYTE_GRAY) { System.err.println("Converting colour image to greyscale image..."); ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); BufferedImage greyImage = op.filter(newImage, null); newImage = greyImage; } return newImage; } You can see that the readImage method signature has changed to indicate that instead of no variable being returned after the method completes (indicated by void) a BufferedImage is returned.
12
January 2006Daniel Bryant12 Calling readImage Now instead of just calling readImage(fileName) in your constructor, you will need to load the result of this method into a BufferedImage variable. So, replace readImage(imageFile); with BufferedImage sourceImage1 = readImage(imageFile);
13
January 2006Daniel Bryant13 The Example Code public Coursework(String[] argument) throws IOException, ImageDecoderException { super("Coursework"); String imageFile = argument[0]; String imageFile2 = argument[1]; BufferedImage sourceImage1 = readImage(imageFile); BufferedImage sourceImage2 = readImage(imageFile2); BufferedImage[] imageArray = new BufferedImage[]{sourceImage1,sourceImage2};... views[0] = new ImageView(sourceImage1); views[1] = new ImageView(sourceImage2); views[3] = new ImageView(average(imageArray));... } The final thing to do is to load the two BufferedImages into an array (as shown in the code example below) and display the two images and the average result using the ImageView class
14
January 2006Daniel Bryant14 Hints for Subtraction A guided tour of average… public static BufferedImage average(BufferedImage[] imgArray) { int n = imgArray.length; int w = imgArray[0].getWidth(); //assume that they all have the same dimensions int h = imgArray[0].getHeight(); BufferedImage average = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { float sum = 0.0f; for (int i = 0; i < n; ++i) { sum += imgArray[i].getRaster().getSample(x, y, 0); } average.getRaster().setSample(x,y, 0, Math.round(sum/n)); } return average; }
15
January 2006Daniel Bryant15 Hints for Subtraction sum += imgArray[i].getRaster().getSample(x, y, 0); Is a shorthand way of writing sum = sum + imgArray[i].getRaster().getSample(x, y, 0); There is a abs() method in the Math package that returns an absolute (i.e. positive) value. This method is called like this double abValue = Math.abs(-1.25); The resulting value of abValue will be 1.25
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.