ebook img

Essential Java Fast : How to write object oriented software for the Internet PDF

196 Pages·1997·12.859 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Essential Java Fast : How to write object oriented software for the Internet

Essential Java Fast How to write object oriented software for the Internet Springer-Verlag London Ltd. John Cowell Essential Java Fast How to write object oriented software for the Internet with 64 figures Springer John Cowell, BSc(Hons), MPhil.,PhD. Department of Computer and Information Sciences, De Montfort University Kents Hill Campus, Hammerwood Gate, Kents Hill Milton Keynes. MK7 6HP, UK ISBN 978-3-540-76052-8 British Library Cataloguing in Publication Data Cowell, John, 1957- Essential Java fast: how to write object oriented software for the Internet in Java l.Java (Computer program language) 2.0bject-oriented programming (Computer science) 3.Internet (Computer network) I.Titie 005.1' 33 ISBN 978-3-540-76052-8 ISBN 978-1-4471-0629-6 (eBook) DOI 10.1007/978-1-4471-0629-6 Library of Congress Cataloging-in-Publication Data A Catalog record for this book is available from the Library of Congress Apart from any fair dealing for the purpose of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1989, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms of licences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. © Springer-Verlag London 1997 Originally published by Springer-Verlag Berlin Heidelberg New York in 1997 Microsoft, MS, Visual Basic, Windows and Windows 95 are trademarks of Microsoft Corporation Ltd. Delphi is a trademark of Borland Corporation Ltd. Java and Hot Java are trademarks of Sun Microsystems Corporation Ltd. The use of registered names, trademarks etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use. The publishers make no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for anyerrors or omissions that may be made. Typesetting: camera-ready by author 34/3830-543210. Printed on acid-free paper Contents 1 WHY USE JAVA? ......................................................................................... 1 Introduction .............................................................................................. 1 What is Java? ........................................................................................... 2 Is this book for you? ................................................................................. 2 What you need to run Java ....................................................................... 3 How to use this book ................................................................................ 3 Conventions .............................................................................................. 4 2 OBJECT ORIENTATION PRIMER ............................................................... 5 Introduction .............................................................................................. 5 What are objects? ..................................................................................... 5 Classes ...................................................................................................... 6 Attributes and behaviour .......................................................................... 7 Inheritance ................................................................................................ 8 Why use inheritance? ............................................................................... 8 3 JAVA APPLETS ......................................................................................... 10 Introduction ............................................................................................ 10 Applets and applications ......................................................................... 10 Why applets are different ....................................................................... 11 A simple applet. ...................................................................................... 13 Initialising an applet ............................................................................... 13 Starting an applet .................................................................................... 14 Stopping an applet .................................................................................. 14 Destroying an applet. .............................................................................. 14 Displaying text and graphics .................................................................. 14 Java and HTML ...................................................................................... 15 HTML tags ............................................................................................. 16 The applet tag ......................................................................................... 16 Creating an applet. .................................................................................. 17 Controlling the applet size and position ................................................. 18 Compiling applications ........................................................................... 21 Command line arguments for applications ............................................. 21 Command line arguments for applets ..................................................... 21 v vi Contents 4 THE JAVA LANGUAGE ..............................................................................2 4 Introduction ............................................................................................. 24 Applications and applets ......................................................................... 24 Features of Java programs ....................................................................... 25 Names in Java ......................................................................................... 25 Running a Java application ..................................................................... 25 Key elements of a Java program ............................................................. 26 Classes ..................................................................................................... 26 New objects ............................................................................................. 27 Data types ................................................................................................ 27 Arrays ...................................................................................................... 28 Methods ................................................................................................... 29 Calling methods ...................................................................................... 30 Creating a class and an object ................................................................. 31 Instance variables .................................................................................... 32 Class variables ......................................................................................... 33 Constants ................................................................................................. 33 Using instance variables .......................................................................... 33 Using class variables ............................................................................... 34 The keyword this ..................................................................................... 35 Arithmetic in Java ................................................................................... 35 Assigning number variables .................................................................... 35 Casting .................................................................................................... 36 Assigning characters, strings and boolean variables ............................... 36 Assignment operators .............................................................................. 37 Assigning objects .................................................................................... 39 Comparison operators ............................................................................. 39 Comparing objects .................................................................................. 40 Overloaded methods ................................................................................ 40 Constructors ............................................................................................ 41 The finalize method ................................................................................ .4 2 5 BRANCHING AND LOOPiNG ..................................................................... 43 Introduction ............................................................................................. 43 If .. else statements .................................................................................. .43 Block statements ..................................................................................... 44 The conditional operator ........................................................................ .45 Switch statements .................................................................................... 45 Looping ................................................................................................... 46 While loops .............................................................................................. 46 Do .. while loops ....................................................................................... 47 For loops ................................................................................................. 47 Quitting loops .......................................................................................... 48 Java operators ........................................................................................... 49 Bitwise operators ..................................................................................... 49 Contents vii Logical operators .................................................................................... 51 Operator precedence ............................................................................... 52 Using strings in Java ............................................................................... 53 6 GRAPHiCS ................................................................................................. 55 Introduction ............................................................................................ 55 Graphics basics ....................................................................................... 55 Graphics drawing methods ..................................................................... 57 Lines and points ...................................................................................... 57 Rectangles .............................................................................................. 57 Plain rectangles ....................................................................................... 58 Rounded rectangles ................................................................................ 58 3D-effect rectangles ................................................................................ 59 Polygons ................................................................................................. 60 Ovals ...................................................................................................... 61 Arcs ........................................................................................................ 62 Colour ..................................................................................................... 63 Pre-defined colour .................................................................................. 63 Defining colours ..................................................................................... 64 Changing object colours ......................................................................... 64 Text and fonts ......................................................................................... 66 Using images .......................................................................................... 67 The draw/mage method .......................................................................... 68 The keyword this .................................................................................... 69 7 ANIMATION AND MULTITHREADING ...................................................... 70 Introduction ............................................................................................ 70 Multithreading ........................................................................................ 70 Creating new threads .............................................................................. 71 Pre-emptive scheduling .......................................................................... 72 Problems with multiple threads .............................................................. 74 The synchronized keyword ..................................................................... 75 Using Runnable ...................................................................................... 76 Thread priorities ..................................................................................... 76 Animation ............................................................................................... 78 The bouncing ball applet ........................................................................ 78 Using threads .......................................................................................... 80 Reducing flicker ..................................................................................... 83 Overriding methods ................................................................................ 83 Overriding update .................................................................................. 84 Using double buffering ........................................................................... 86 viii Contents 8 HANDLING EXCEPTIONS .......................................................................... 92 Introduction ............................................................................................. 92 What are exceptions? .............................................................................. 92 Java exceptions ....................................................................................... 92 The java.lang .Exception class ................................................................. 93 Trying and catching ................................................................................. 94 Throwing and catching ............................................................................ 96 Usingfinally ............................................................................................ 96 9 THE JAVA.AWl PACKAGE ........................................................................ 97 Introduction ............................................................................................. 97 Event driven programming ...................................................................... 97 What the AWT does ................................................................................ 98 The Button component ............................................................................ 98 The Label component. ........................................................................... l 00 The Scrollbar component. ..................................................................... l 00 The Checkbox component ..................................................................... 10 2 The List component. .............................................................................. l 03 The TextField component ..................................................................... 104 Entering passwords ............................................................................... l 05 The TextArea component ...................................................................... 105 Controlling layout ................................................................................. 106 The FlowLayout class ........................................................................... 106 The GridLayout class ............................................................................ 107 The GridBagLayout cIass ...................................................................... 10 8 The BorderLayout class ........................................................................ 110 The CardLayout class ........................................................................... 111 Layout methods ..................................................................................... 112 Using the layout managers .................................................................... 112 10 HANDLING EVENTS .............................................................................. 116 Introduction ........................................................................................... 116 . Mouse events ......................................................................................... 116 The mouseDown and mouse Up events .................................................. 117 Boolean methods ................................................................................... 118 The line drawing applet.. ....................................................................... 118 The scribble applet ................................................................................ 120 Keyboard events .................................................................................... 122 The handleEvent method ...................................................................... 124 The action method ................................................................................ 125 11 WINDOWS, DIALOGS AND MENUS ...................................................... 128 Introduction ........................................................................................... 128 Contents ix Creating and displaying windows ......................................................... 128 Adding objects to pages ....................................................................... 129 Menus ................................................................................................... 130 Types of menu items ....................................................................... : ..... 132 Enabling and disabling menu items ...................................................... 134 The Dialog subclass ............................................................................. 134 Windows methods ................................................................................ 135 12 INPUT AND OUTPUT STREAMS .......................................................... 137 Introduction .......................................................................................... 137 The java.io package .............................................................................. 137 The FilelnputStream class .................................................................... 138 Reading from streams ........................................................................... 140 The skip and mark methods .................................................................. 140 The ByteArraylnputStream class .......................................................... 141 The StringBuJferlnputStream class ....................................................... 141 The FilterlnputStream class ................................................................. 142 The BuJferedlnputStream class ............................................................ 142 The DatalnputStream class .................................................................. 142 The LineNumberlnputStream class ...................................................... 143 The PushbacklnputStream class ........................................................... 144 The PipedlnputStream class ................................................................. 144 The SequencelnputStream class ........................................................... 144 The OutputStream class ........................................................................ 145 The FileOutputStream class ................................................................. 145 Writing to streams ................................................................................ 146 The ByteArrayOutputStream class ....................................................... 147 The FilterOutputStream class ............................................................... 147 The BuJferedOutputStream class .......................................................... 148 The PrintStream class .......................................................................... 148 The DataOutputStream class ................................................................ 149 The PipedOutputStream class .............................................................. 150 13 MODIFIERS AND PACKAGES .............................................................. 151 Introduction .......................................................................................... 151 Access control ...................................................................................... 151 The public access modifier ................................................................... 152 The private access modifier .................................................................. 152 The package access modifier.. .............................................................. 153 The protected access modifier .............................................................. 153 Accessor methods ................................................................................. 153 Thefinal modifier ................................................................................. 154 Using packages ..................................................................................... 155 Why use packages? .............................................................................. 155 x Contents Importing packages ............................................................................... 157 The java. lang package .......................................................................... 158 14 JAVA AND C++ ....................................................................................... 159 Introduction ........................................................................................... 159 Data types .............................................................................................. 159 Using strings .......................................................................................... 160 Command line arguments ...................................................................... 160 Exception handling ................................................................................ 160 Miscellaneous differences ..................................................................... 161 APPENDIX AJ AVA PACKAGES ................................................................. 162 Introduction ........................................................................................... 162 Reserved words ..................................................................................... 162 Reserved characters ............................................................. :. ................ 162 Java operators ........................................................................................ 163 Java data types ...................................................................................... 163 The Java class library ............................................................................ 164 The java.awt package ............................................................................ 164 Exception index for java.awt ................................................................ 166 The java.awt.image package ................................................................. 166 Exception index for java.awt.image ...................................................... 166 The java.awt.peer package .................................................................... 167 Exception index for java.awt.peer ........................................................ 167 The java.io package .............................................................................. 167 Exception index for java.io ................................................................... 169 The java. lang package .......................................................................... 169 Exception index for java. lang ............................................................... 170 The java.util package ............................................................................ 171 Exception index for java.util ................................................................. 172 The java. net package ............................................................................. 172 Exception index for java.net ................................................................. 173 The java.applet package ....................................................................... 173 Exception index for java.applet ............................................................ 173 APPENDIX B CLASS HIERARCHIES .......................................................... 174 Introduction ........................................................................................... 174 Class hierarchy ...................................................................................... 174 INDEX .......................................................................................................... 183

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.