Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Tutorial – Application Building

Similar presentations


Presentation on theme: "Java Tutorial – Application Building"— Presentation transcript:

1 Java Tutorial – Application Building
Extending our Application Daniel Bryant 4/7/2019 Java Tutorial - Daniel Bryant

2 Java Tutorial - Daniel Bryant
Introduction Today we will be extending the project from the previous Java Tutorial Lab Therefore, you must have completed all the work in Lab 1 before starting this sheet You should have NetBeans set up correctly (i.e. with the appropriate directories Mounted (specified in the first Lab) You should have version 4 of the MainFrame class (file name on the website: MainFrame4.class) compiled and working Version 4 of the class allows you to specify and load an image into your application 4/7/2019 Java Tutorial - Daniel Bryant

3 Java Tutorial - Daniel Bryant
Overview In this lab will be using our simple GUI to load an image and pass this to the “Dither” application you have previously looked at in the first week of Dr Tang’s labs This avoids having to specify the image name as an Argument to your application each time you execute it and also allows you to change the image when the application is running 4/7/2019 Java Tutorial - Daniel Bryant

4 Java Tutorial - Daniel Bryant
Adding the Dither Code You should have the directory containing the class files (directory name “Classes”) and the directory containing the Dither.java file already mounted If not, mount Classes and Apps>Chap05 into NetBeans You should also have the directory containing your “sampleGUI” package mounted - Mount >> GuiTutorial Copy the Dither.java file into your package (which should be named “samplegui”) You can do this by right-clicking on the Dither.java file in NetBeans and selecting “copy” and then right clicking on your package and selecting “paste>>copy” Because of the way the Dither code is written you will also need to specify the working directory. Right-click>>Properties>>Executor>>Working Directory 4/7/2019 Java Tutorial - Daniel Bryant

5 Java Tutorial - Daniel Bryant
Adding the Dither Code We have included the Dither class into our package so that we can easily reference the class in our code Of course you could place the Dither class into another package and “import” this into your code. Ask if you are curious We will no longer be executing the Dither class directly as you did in the first lab. Instead we will execute our MainFrame class and use this to call the Dither class When the user clicks a button on our GUI we want the Dither class to be executed Therefore we will need to copy the code in the “public static void main (String[] args)” method of the Dither class to a button in our GUI application (Why?) 4/7/2019 Java Tutorial - Daniel Bryant

6 Modifying Our Application
Open the MainFrame code in NetBeans Choose one of the top buttons to be responsible for loading the Dither application and alter the button’s text accordingly (e.g. so that it reads “Dither the image” in the GUI) 4/7/2019 Java Tutorial - Daniel Bryant

7 Modifying the Event Handler
Now we will modify the code of that buttons Event handler If you look at the code below the button you will see the following code loadApp1Button.addActionListener(new DoloadApp1()); This code adds an “ActionListener” to the button named loadApp1Button An ActionListener simply monitors the button for any events that might occur, such as the user clicking the button. When the user clicks the button an Event object is created - the ActionListener is informed and passes details of this event onto the object specified In our code we have specified that a new object named DoloadApp1 should be created, sent the details of the event and then executed 4/7/2019 Java Tutorial - Daniel Bryant

8 Modifying the Event Handler
Therefore, we need to modify the Event Handler code in the appropriate inner class (DoloadApp1() in our example) You should copy and then modify the code from the Dither class “public static void main (String[] argv)” method into the actionPerformed() method of the DoloadApp1() class You will notice the name of the image to be loaded is specified in the following code in the Dither class “main” method: JFrame frame = new Dither(argv[0]); argv[0] is the name of the argument you provided to the class at run-time using NetBeans (remember when you had to specify the filename as an argument?) 4/7/2019 Java Tutorial - Daniel Bryant

9 Modifying the Event Handler
Therefore, you will have to modify this variable to refer to the file you have loaded using our file selector window The file variable is named myPicture Because this variable is a File Object and we need to pass the name of the file into the Dither class, we will have to use the .getName() method on the file variable String fileName = myPicture.getName(); The complete code for the actionPerformed method is given overleaf, but please do try and understand why you have moved it to this class and why the modifications have been made 4/7/2019 Java Tutorial - Daniel Bryant

10 Java Tutorial - Daniel Bryant
actionPerformed code public void actionPerformed(ActionEvent e) { try { System.out.println(myPicture.getName()); JFrame frame = new Dither(myPicture.getName()); frame.pack(); frame.setVisible(true); } catch (IOException iox) { System.out.println("Error loading the image file"); catch (ImageDecoderException idx) System.out.println("Decode Error loading the image file"); 4/7/2019 Java Tutorial - Daniel Bryant

11 Java Tutorial - Daniel Bryant
Running your code Compile all the classes in your package and execute your “Main” class in order to display the GUI Click on the load picture button and select a JPEG Image from the Images directory (which you specified as the working directory for Dither) Click the “open a picture” button to load the image Click on the button you have modified labelled “Dither the Image” and the Dither window should be displayed with the image you have selected previously You can minimize the Dither window, specify another image using the above method and then open another Dither window which will display the new image 4/7/2019 Java Tutorial - Daniel Bryant

12 Java Tutorial - Daniel Bryant
And Finally… As soon as you click the close window button on any window (i.e. the main application or any Dither windows) the whole application will close down This is a little annoying but easily fixed. Can you determine why this happens and modify the code accordingly? Hint: the Dither code in your package needs to be modified 4/7/2019 Java Tutorial - Daniel Bryant


Download ppt "Java Tutorial – Application Building"

Similar presentations


Ads by Google