Presentation is loading. Please wait.

Presentation is loading. Please wait.

Field Trip #27 Using Java to Download Images and Sounds By Keith Lynn.

Similar presentations


Presentation on theme: "Field Trip #27 Using Java to Download Images and Sounds By Keith Lynn."— Presentation transcript:

1 Field Trip #27 Using Java to Download Images and Sounds By Keith Lynn

2 JPanel A JPanel is a lightweight component that can be placed inside other containers It can have its own layout If we want to draw Graphics on the panel, we override the paintComponent method

3 JFrame A JPanel can be placed inside a Jframe A Jframe is a top-level container in Java We can set the layout of the Jframe to FlowLayout to have components laid out in a row If we want to specify the size of a component, we use the method setPreferredSize which accepts a Dimension object

4 JButton A Jbutton is a graphical component that can be added to a container The Jbutton contains a label which can either be text of an image If we want to detect and act on the button click, then we must attach an ActionListener to the Jbutton ActionListener is an interface which contains the method actionPerformed which accepts an ActionEvent object

5 Events An event like a button click can be detected When we have an ActionListener attached to a Jbutton and the button is clicked, actionPerformed is called We can find the source of the event with getSource() and we can find the action command of the source with getActionCommand() Unless we specify otherwise, the action command of a Jbutton is its label

6 URL URL is found in the java.net package We can use it to open a file on the web as long as we can access it in a browser When we create a URL, we need to make sure that we deal with the potential of a MalformedURLException We can open the stream of the URL with the method openStream

7 Streams A stream is a continuous flow When we connect to a URL and open its stream, then we create a stream of bytes We can read all of the bytes of the URL As long as the remote server is setup to properly send the bytes of the file and read all of the bytes, then we can save the file on our hard drive and open it

8 Arrays We can make use of arrays in downloading the information To make the reading efficient, we attach a BufferedInputStream to the stream of the URL BufferedInputStream allows us to read in large chunks of the URL at once For each chunk of the URL we read, we accumulate these in an array of bytes Once we have read all bytes of the URL, then we can save the array

9 FileOutputStream To save the array of bytes, we can create a FileOutputStream which will open a byte connection to a file For efficient writing, we attach a BufferedOutputStream to the FileOutputStream We write the entire array to the BufferedOutputStream We make sure we close the stream

10 Downloading an image or sound In order to download an image or sound, locate one on the web Then place the URL in the JtextField When you click the download button, the file will be downloaded Since we are downloading the bytes of the file, we will get an exact copy


Download ppt "Field Trip #27 Using Java to Download Images and Sounds By Keith Lynn."

Similar presentations


Ads by Google