Applets and Images - PowerPoint PPT Presentation

Loading...

PPT – Applets and Images PowerPoint presentation | free to download - id: 14683c-NmY1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Applets and Images

Description:

A GUI is made of a subclass of Container, added to with other ... Any line breaks etc. won't show up on screen. Putting an applet into a webpage APPLET ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 50
Provided by: andrew58
Learn more at: http://www.geog.leeds.ac.uk
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Applets and Images


1
Lecture 7
  • Applets and Images

2
  • Review
  • Java is in packages of pre-written Classes.
  • We can import these and use them.
  • A GUI is made of a subclass of Container, added
    to with other subclasses of Component.
  • import java.awt.
  • class PushPop
  • public PushPop ()
  • Frame f new Frame(Pop)
  • f.setSize(200,200)
  • Button b new Button(Push)
  • f.add(b)
  • f.setVisible(true)
  • public static void main (String args) new
    PushPop()

3
  • To listen to a component we must promise to have
    a method it can pass an Event.
  • We confirm this promise by saying we implement a
    Listener interface.
  • class ButtonListen implements ActionListener
  • public void actionPerformed (ActionEvent ae)
  • // Do stuff
  • We can then register a Listener with the
    Component.
  • ButtonListen bl new ButtonListen()
  • b.addActionListener(bl)

4
This week
  • This week well learn how to build web applets.
  • Applets basics
  • The URL class
  • Applets advanced
  • Making webpages with applets in them
  • The Art of Programming Part Seven webpage design
  • Images
  • Assessment

5
Applets what are they?
  • Small programs that run in webpages.
  • Are contained in a tight security sandbox.
  • Cant read or write files on the browsers
    machine.
  • Cant start local programs or access the local
    OS.
  • Can only access the machine it was sent from
    (though can get webpages from others).
  • Cant switch off and replace the JVM.
  • You can, however, sign applets. If the user
    trusts you the applets can be given more freedom.

Server or Host
Client
Request
Server Program
Web browser
Files returned
6
Basics of an Applet inheritance
  • Applets are called by a line within a webpage
    which says that an applet should be inserted and
    where to find it.
  • All applets extend java.applet.Applet.
  • All therefore need to import java.applet..
  • java.applet.Applet is a type of Panel.
  • All applets are GUI based and all but simplest
    are Event Based.
  • Therefore they also need to import java.awt. and
    java.awt.event..

7
Basics of an Applet methods
  • Dont usually have a main method.
  • init()
  • Replaces the main/constructor used to set-up
    the applet. Called when applet first shown.
  • start()
  • Used each time an applet is shown e.g. if a
    webpage is visited, left and then revisited.
    Calls paint().
  • paint()
  • Inherited by Applet from Panel / Component.
    Called at start and whenever needed, e.g. if the
    browser is covered by another window and then
    uncovered.

8
Basics of an Applet methods
  • stop()
  • Called when a browser leaves the webpage.
  • destroy()
  • Called when the browser kills the applet - stop
    is always called first. Not usually possible to
    say when this happens.
  • You dont have to implement all these methods
    each has a default.
  • Just implement those you want usually init,
    start and paint.

9
Basic Applet
  • import java.applet.
  • import java.awt.
  • import java.awt.event.
  • public class OurApplet extends Applet
  • init()
  • System.out.println(Hello World!)
  • System.out is the browsers Java Console.

10
  • Applets basics
  • The URL class
  • Applets advanced
  • Making webpages with applets in them
  • The Art of Programming Part Seven webpage design
  • Images
  • Assessment

11
The java.net.URL class
  • Encapsulates a Uniform Resource Locator (URL).
  • http//www.w3.org80/People/Berners-Lee/Overview.h
    tml
  • A URL represents another file on the network.
    Its comprised of
  • A method for locating information i.e. a
    transmission protocol, e.g. the HyperText
    Transmission Protocol (http).
  • A host machine name, e.g. www.w3.org
  • A path to a file on that server, e.g.
    /People/Berners-Lee/Overview.html
  • A port to connect to that server on, e.g. http
    connects to port 80.

12
Client Server architecture
  • A file requested using a URL will usually be
    handled by a client-server architecture.
  • Which server program gets which requests will
    depend on the port they are sent to, which is
    usually related to the transmission protocol.
  • e-mails are sent out to servers using Simple Mail
    Transfer Protocol (SMTP) which usually goes into
    Port 25. Email storage programs therefore usually
    listen for messages coming in via Port 25.
  • One server can serve several clients at once.
  • Webservers use http/port 80 to listen for
    requests, then send out webpages from a
    particular requested directory.

13
Making a URL object
  • Need to import java.net., and deal with the
    MalformedURLException that the URL constructors
    throw.
  • You can then use one of the URL class
    constructors. Some let you put in the port, or
    split off the path.
  • Easiest is
  • URL url new URL(String urlSpecifier)
  • Where urlSpecifier is an address like
  • http//www.w3.org/People/Berners-Lee/Overview.htm
    l

14
Making a URL / Scope problems
  • URL url null
  • try
  • url new URL
  • (http//www.w3.org/People/Berners-Lee/Overview.h
    tml)
  • catch (MalformedURLException mal)
  • mal.printStackTrace() // or better

15
Basic URL Methods
  • getFile()
  • Gets the file name of this URL.
  • getHost()
  • Gets the host name of this URL, if applicable.
  • getPath()
  • Gets the path part of this URL.
  • getPort()
  • Gets the port number (int) of this URL.

16
  • Applets basics
  • The URL class
  • Applets advanced
  • Making webpages with applets in them
  • The Art of Programming Part Seven webpage design
  • Images
  • Assessment

17
Advanced Applet methods
  • getImage(URL)
  • Returns an java.awt.Image object from a given URL
    object.
  • getDocumentBase()
  • Returns a URL object representing the directory
    the applet starting webpage was in.
  • Together with the URL methods, these methods
    allow us to retrieve image files from our web
    server.

18
Advanced Applet methods
  • getAppletContext()
  • Returns an AppletContext object representing the
    environment the applet is running in.
  • AppletContext ap getAppletContext()
  • The most important method in an AppletContext
    object is
  • showDocument(URL webpage).
  • This allows you to put a webpage into the browser
    window.
  • ap.showDocument(new URL(http//www.w3.org/))

19
  • Applets basics
  • The URL class
  • Applets advanced
  • Making webpages with applets in them
  • The Art of Programming Part Seven webpage design
  • Images
  • Assessment

20
Webpages
  • As weve mentioned theyre usually sent out by a
    web server to clients who ask for them, but you
    can open them directly on your harddrive.
  • They consist of text that will be displayed and
    tags that wont, which include formatting details
    or references to other files like images or
    applets.
  • The tags are referred to as the HyperText Markup
    Language (HTML).
  • Saved as text files with the suffix .html (or
    sometimes .htm)

21
A basic webpage
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtTitle for top of browserlt/TITLEgt
  • lt/HEADgt
  • ltBODYgt
  • lt!Stuff goes here--!gt
  • lt/BODYgt
  • lt/HTMLgt
  • The HEAD contains information about the page, the
    BODY contains the actual information to make the
    page.

22
  • ltBODYgt
  • The break tag breaks a lineltBRgt
  • like that.
  • ltPgt
  • The paragraph tags
  • lt/Pgt
  • leave a line.
  • This is ltBgtBoldlt/Bgt.
  • This is ltIgtItaliclt/Igt
  • ltIMG srctim.gif altPhoto Pic of Tim
    width50 height50gtlt/IMGgt
  • ltA hrefindex.htmlgtLink textlt/Agt
  • lt/BODYgt
  • The text in the file will only be shown with the
    format set out in the tags. Any line breaks etc.
    wont show up on screen.

23
Putting an applet into a webpage
  • ltAPPLET
  • code appletFile (without the .class)
  • width widthInPixels
  • height heightInPixels
  • align left or right or center
  • gt
  • ltPARAM namename1 valueattributeValue1gt
  • ltPARAM namename2 valueattributeValue2gt
  • lt/APPLETgt

24
PARAMs
  • We can pull in the parameters using the applets
    getParameter(String paramName) method, which
    returns the values associated with paramName as a
    String. For example, in our web page we might
    have
  • ltPARAM name colorTypes value 1gt
  • and in our init() we might have
  • String colorScheme getParameter(colorTypes)
  • if (colorScheme.equals(1))
  • setBackground(Color.WHITE)
  • Note that we treat 1 as a String, even though
    it is a numerical character.

25
Putting an applet into a webpage
  • ltOBJECT
  • codetype"application/java"
  • classid appletFile
  • width widthInPixels
  • height heightInPixels
  • gt
  • ltPARAM namename1 valueattributeValue1gt
  • ltPARAM namename2 valueattributeValue2gt
  • lt/OBJECTgt

26
Writing joint Applets and Applications
  • Because Applets are types of Panels, we can do
    this...
  • public class TwoFaced extends java.applet.Applet
  • public void init()
  • // All our code here.
  • public static void main(String args)
  • Frame appFrame new Frame("Two Faced")
  • TwoFaced twoFaced new TwoFaced ()
  • twoFaced.init()
  • appFrame.add(twoFaced, BorderLayout.CENTER)
  • appFrame.setSize(300, 300)
  • appFrame.setVisible(true)

27
Summary
  • Applets are programs that run in web browsers.
  • Because of this they have their own security
    limitations.
  • They have init, start, stop, and destroy methods.
  • init and destroy are called when the applet is
    first started and killed respectively.
  • start and stop are called when the webpage is
    re-entered and left.

28
Summary
  • Client server architectures are a common
    mechanism for retrieving files over the internet.
  • One java class that uses them is URL which
    encapsulates a file location and the protocol to
    get it.
  • Applets can use URL objects to get files from the
    internet and display them.
  • Applets are usually run from webpages. The
    System.out is the Java Console.

29
The Art of Programming good web design.
  • Like any GUI, good web design concentrates on
    usability.
  • There are a number of websites that can help you
    these are listed on the links page for this
    course.
  • Theres also an online tutorial details on the
    handout. Before you design any pages, read
    through the Design tips and Writing for the web
    sections.

30
Basic web design tips
  • Keep it simple and easy to read.
  • Try to keep to two screenfulls of info on any
    page if you can.
  • Dont use jargon or long paragraphs.
  • Highlight important sentences to help skim
    readers.
  • Make navigation obvious.
  • Use text links that say where they go.
  • People prefer having lots of links on one page
    more than having to click through several pages
    to get to info.
  • When in doubt, keep it plain.

31
Web accessibility
  • If you are working for a public organisation,
    accessibility for the disabled has to be a major
    design driver. In addition, not everyone can see
    applets (2 of browsers).
  • Generally you can make webpages accessible by not
    putting important information in images and
    sound.
  • Java 1.1 applets have no accessibility built in.
  • Java Foundation Class / Swing applets have
    accessibility built in will be usable with
    speaking browsers etc.

32
Web accessibility Java 1.1
  • ltAPPLET code appletFile width widthInPixels
    height heightInPixelsgt
  • Please visit this ltA hrefblind.htmlgtsitelt/Agt
    for a text list of our information.
  • lt/APPLETgt
  • Anything you write between in the APPLET / OBJECT
    tags will only be displayed if the applet doesnt
    work.

33
Images
  • Images are stored in java.awt.Image objects.
  • Weve already seen one way of getting Image
    objects Applets getImage(URL) method.
  • URL url null
  • try
  • url new URL
  • (http//www.geog.leeds.ac.uk/images/banners/log
    o.jpg)
  • catch (MalformedURLException e)
  • url null
  • Image myImage null
  • if (url ! null)
  • myImage getImage(url)

34
MemoryImageSource
  • Another is to use a java.awt.image.MemoryImageSour
    ce object.
  • A MemoryImageSource object takes in an int array
    and turns it into an image in memory
  • MemoryImageSource(int width,int height,
  • int pixel,int arrayStart,int scanLineWidth)

35
Making Images from data
  • We can create the pixel array ourselves.
  • The pixels are made up of an alpha value (the
    images transparency, then a red, green and blue
    value.
  • All are between 0 and 255 (alpha is usually 0
    transparent or 255 fully visible).
  • So how do we structure the array? Rather cleverly
    all four numbers are squeezed into a single int.

36
Making Images the hard way
  • These four values are squeezed into a single
    number. We have to do this encoding ourselves.
  • Essentially, in the room in memory set aside to
    store the biggest possible int, theres room to
    store at least four ints of size 0 to 255.
  • We can store these ints in a normal int space if
    we strip them of some of their own useless blank
    space.
  • But we can do this by altering their binary
    representations inside the computer directly.
    This is about as much fun as being continually
    kicked in the groin by a battery of mules.

37
Getting color ints
  • The Color class has methods to help.
  • int red 0
  • int green 125
  • int blue 255
  • Color color new Color(red, green, blue)
  • int r color.getRed()
  • int g color.getGreen()
  • int b color.getBlue()
  • int rgb color.getRGB()
  • Color color new Color(rgb)

38
  • int width 16
  • int height 16
  • int pixels new intwidth height
  • for (int h 0 h lt height h)
  • for (int w 0 w lt width w)
  • int value 0
  • Color pixel new Color(value,value,value)
  • pixelsw (hwidth) pixel.getRGB()

39
  • int width 16
  • int height 16
  • int pixels new intwidth height
  • for (int h 0 h lt height h)
  • for (int w 0 w lt width w)
  • int value (w h gt 255) ? 255 w h
  • Color pixel new Color(value,value,value)
  • pixelsw (hwidth) pixel.getRGB()

40
Using MemoryImageSource
  • Once we have our array of pixels we can build the
    MemoryImageSource.
  • Encapsulates an image in the computers memory.
  • Computers can have multiple output devices on
    which an image is displayed, so this doesnt
    represent a displayed image. To do this, we need
    to use a Component to convert it. They know about
    the display.
  • MemoryImageSource memImage
  • new MemoryImageSource(16,16,pixels,0,16)
  • Panel panel new Panel()
  • Image image panel.createImage(memImage)

41
Displaying an Image
  • We can then use a Graphics objects drawImage
    method to draw the image to the screen.
  • drawImage(Image im, int left, int top,
    ImageObserver imob)
  • g panel.getGraphics()
  • g.drawImage(image,0,0,this)
  • All components implement ImageObserver. This
    interface has one method imageUpdate, which
    returns whether an image has finished loading.

42
Sequence
  • Get an image array.
  • Create a MemoryImageSource object.
  • Create an Image object.
  • Draw the image using a graphics object.

43
Making Images PixelGrabbers
  • To get our int array of pixels we can get them
    from another Image or write them ourselves.
  • The PixelGrabber object does the exact opposite
    of MemoryImageSource.
  • PixelGrabber(Image img, int left, int top, int
    width, int height, int pixels, int startArray,
    int scanLineWidth)
  • int pic new int 100
  • PixelGrabber pg
  • new PixelGrabber(image,0,0, 16,16, pic,0,16)
  • pg.grabPixels()

44
Putting it all together
  • We now have all the methods we need to build a
    small image processor.
  • First we get an image from a file using
    getImage(URL).
  • Then we use a pixelGrabber to pull the pixels
    into an array.
  • We then expand the pixels into their alpha, red,
    green and blue values.
  • We manipulate the values for example replacing
    certain grayscale colours with false colours.

45
Putting it all together
  • We then recode the values into the int array.
  • We use a MemoryImageSource to put the pixels back
    in an Image.
  • Finally we use a Graphics object to display the
    new image on the screen.
  • We then run to the pub and drink ourselves
    unconscious to celebrate being geek gods
    unfettered by the mere trifling difficulties of
    everyday human beings.

46
Summary
  • Applets are programs that run in web browsers.
  • Because of this they have their own security
    limitations.
  • They have init, start, stop, and destroy methods.
  • init and destroy are called when the applet is
    first started and last killed.
  • start and stop are called when the webpage is
    re-entered and left.

47
Summary
  • Client server architectures are a common
    mechanism for retrieving files over the internet.
  • One java class that uses them is URL which
    encapsulates a file location and which protocol
    to use.
  • Applets can use URL objects to get files from the
    internet and display them.
  • Applets are usually run from webpages.
  • We must be aware of good website accessibility
    and usability.

48
Summary
  • We can get Images from the internet or build them
    ourselves.
  • We can manipulate the images by getting them with
    a PixelGrabber and extracting the original alpha,
    red, green and blue values.
  • We can put arrays of encoded ints into an Image
    using a MemoryImageSource object.
  • We can display images using a Graphics objects
    drawImage method.

49
  • Next lecture
  • Input and output to files.
  • Communicating over the internet continued.
  • Practical
  • Making an applet
About PowerShow.com