java tutorial application building
Download
Skip this Video
Download Presentation
Java Tutorial – Application Building

Loading in 2 Seconds...

play fullscreen
1 / 12

Java Tutorial – Application Building - PowerPoint PPT Presentation


  • 76 Views
  • Uploaded on

Java Tutorial – Application Building. Extending our Application Daniel Bryant [email protected] 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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Java Tutorial – Application Building' - gwennan-parker


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
java tutorial application building

Java Tutorial – Application Building

Extending our Application

Daniel Bryant

[email protected]

Java Tutorial - Daniel Bryant

introduction
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

Java Tutorial - Daniel Bryant

overview
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

Java Tutorial - Daniel Bryant

adding the dither code
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

Java Tutorial - Daniel Bryant

adding the dither code1
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?)

Java Tutorial - Daniel Bryant

modifying our application
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)

Java Tutorial - Daniel Bryant

modifying the event handler
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

Java Tutorial - Daniel Bryant

modifying the event handler1
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?)

Java Tutorial - Daniel Bryant

modifying the event handler2
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

Java Tutorial - Daniel Bryant

actionperformed code
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");

}

}

Java Tutorial - Daniel Bryant

running your code
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

Java Tutorial - Daniel Bryant

and finally
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

Java Tutorial - Daniel Bryant

ad