140 likes | 157 Views
Learn how to load, save, and transform images, implement filters with JavaFX, and create a secondary window for filter parameters. Practice functional programming concepts and address feedback in this Lab 8 continuation.
E N D
SE1021 Software Development II Lab 8 (Continued)/New JavaFX Containers and Controls
Overview • Lab 8 Questions? • When is this due? • What about the demo? • Functional Programming
Lab 8: This Week (9) • Load and save images in the .bmp file format • Apply the following transforms to the current image: • Make red-channel version of the image (removes green and blue channels) • Make red-gray version of the image (alternate lines) • Create a second window (an additional stage) for entering filter parameters Provide handlers for the Blur, Sharpen, and Edge buttons. • The appropriate graphical user interface to support the operations listed above. • Implementing logging to a log file • Correct any issues identified in your previous submission, if your instructor has provided feedback to you in a timely manner. (that’s me)
Lambda Expressionhttps://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood • Better to use a lambda expression than say, an inner class • The compiler will generate a new class file for each anonymous inner class • If you use a lot of anonymous inner classes, this is many class files that need to be loaded and verified before being used which can impact startup performance • Java/JVM handles lambdas by “invokedynamic” bytecode instruction • Generate an invokedynamic call site (lambda factor) • When this is invoked returns an instance of the Functional interface to which the lambda is being converted • Convert the body of the lambda expression into a method that will be invoked through the invokedynamic instruction
Functional Interface/Lambda Expressions • A functional interface is one that only contains one method • Help compiler by specifying: @FunctionalInterface • Lambda Expression syntax is (argument) -> body • Lambda expressions allow us to treat code as data • Pass code to methods for execution
Lambda Syntax • ( formal-parameter-list ) -> { expression-or-statements } • For the formal parameter list, if the list consists of a single parameter, the parentheses can be omitted • If it is empty, need empty parenthesis • x • () • For the expression or statements, if you place the expression between the opening and closing braces {}, need to use a return statement to return value, otherwise, don’t need a return • (int x, int y) -> x+y • (x, y) -> { returnx+y; } • (int x, int y) -> { System.out.println(x+y); returnx+y; }
Lambda Examplehttp://www.informit.com/articles/article.aspx?p=2191423&seqNum=2 import java.awt.EventQueue; public class LambdaDemo { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { System.out.println("Running"); } }; EventQueue.invokeLater(r); EventQueue.invokeLater(() -> System.out.println("Running")); }} @FunctionalInterface public interface Runnable { public abstract void run(); }
Another Lambda Example @FunctionalInterface interface Converter{ double convert(double input); } public class LambdaDemo{ public static void main(String[] args) { // Convert Fahrenheit to Celsius System.out.println(convert(input -> (input-32)*5.0/9.0, 98.6)); // Convert Kilometers to Miles System.out.println(convert(input -> input/1.609344, 8)); } static double convert(Converter converter, double input) { return converter.convert(input); } }
Functional Programminghttp://msoe.taylorial.com/se1021/Functional • Can (over) simplify software into two components • Data • Operations • Our approach thus far has been utilizing object oriented concepts to group operations with data • Functional programming treats operations as data • This is what we have been looking at with the lambda expression
Object Oriented Programming • Use of classes to group data with related operations • Objects are created with related data and operations specific to that data and class purpose • Encapsulation • Inheritance • Code re-use • Functionality and behavior are tied together
Functional Programming Definition • The ever-popular Wikipedia definition: https://en.wikipedia.org/wiki/Functional_programming) • “In computer science, functional programming is a programming paradigm – a style of building the structure and elements of computer programs – that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.”
Functional Programming (simplified) • Operations are treated as data • As such, can be passed to methods as parameters • Applied to data • Popular for event-driven programming (again – lambda expression) and concurrency tasks where we execute the same portion of code simultaneously
Java and Functional Programming • Originally Java was difficult to use for functional programming because designed to just be object oriented • Strongly typed language, limitations • With Java 8, functional programming has become easier • Lambda Expressions • Introduction of new types (interfaces): Predicate, Function and Consumer used for functional pipelines • Lazy Evaluation
Functional Programming Characteristics • Functional programming like a mathematical function that for a given set of inputs, the function will return the same value each time it’s called • Need to have the following constraints: • The function call can be replaced by the result. • Functions are not allowed to have side effects. • The compiler is allowed to rearrange the function invocations or run them on different threads without changing the result. • Program flow is driven by data dependencies rather than the sequence of instructions. • Functional programming requires data to be immutable. If the value must change, a new piece of data is created with the new value.