Download presentation
Presentation is loading. Please wait.
Published byDerrick Long Modified over 8 years ago
1
Lecture 4 FrameEditorDemo Packages System Object Runtime Object File Systems
2
AbstractButtons (JVM) Files quitButton Edit tools interacts with runs creates User FrameEditorTester GameFrame FrameEditor User reads/writes
3
AbstractButtonsEdit tools FrameEditorTester GameFrame FrameEditor quitButton Flow of ActionEvents ? ?
4
AbstractButtonsEdit tools quitButton initial calls to addActionListener() relayed calls to addActionListener() FrameEditorTester GameFrame FrameEditor Effect of wrapper addActionListener methods
5
Packages Hierarchy of classes, independent of inheritance. Packages group classes related by function. A package is a namespace protected/package access specifiersprotected/package
6
Creating Packages First line of each file is: package packagename; To access a class: import packagename.classname; To access all classes from a package: import packagename.*; Each package has its own directory, named packagename.
7
Using Packages By default, files are in the “unnamed package.” This is only ok for very small projects. Can have hierarchies of packages. Ex: com.company.region.package. This corresponds to the directory $CLASSPATH/com/company/region/package. There are a few naming conventions.naming conventions Separate import statements for each package, even subpackages.
8
Example Package Layout frameeditor apptranspres
9
File Layout frameeditor app trans pres FrameEditor.java EditorFrame EditorPanel EditorMenuBar IconSelector HelpFrame EditorToolbar TargetHandler EditorPanelHandler EditorMenuHandler FileSaver EditUndoer (unnamed) ActionFrame.java
10
Package code frameeditor.FrameEditor: package frameeditor; import ActionFrame; import frameeditor.pres.*; import frameeditor.app.*; import frameeditor.trans.*; frameeditor.pres.EditorFrame: package frameeditor.pres; import ActionFrame; import frameeditor.FrameEditor; frameeditor.trans.EditorMenuHandler: package frameeditor.trans; import ActionFrame; import frameeditor.FrameEditor; import frameeditor.app.*; import frameeditor.pres.EditorMenuBar;
11
Package Details Matters where you compile from. Java and Javac look in 2 places for packages: $CLASSPATH, and the current directory. Java, Javac can find classes and directories stored in ZIP and JAR archives. May have to disambiguate names, e.g. javax.swing.Timer, java.util.Timer and com.thomasphayes.silly.Timer
12
File Systems A standardized, high-level mechanism to allow a user or users to access organized data on a medium or media. Usually a rooted tree at heart (Unix), sometimes a rooted forest (Win*), may be a directed graph (both fake this). Files, directories, paths. Permissions, owners, locks.
13
Paths Steps from one node to another. May be absolute (starts at root), canonical (absolute, plus no redundancies or shortcuts), or relative (starts somewhere else).. = current,.. = parent, / or \ = file- separator, : or ; = path-separator. Root: / in Unix. L:\ in Win*
14
Warning: File systems Notably, the pathnames for Win* and Unix are different. Windows: C:\home\hayest\fname Unix: /home/hayest/fname File location conventions also differ. JRE seems to support Unix symlinks but not Windows Shortcuts.
15
Solutions java.io.File.separator Do not hard-code pathnames. Environment variables. $HOME, $PATH, $CLASSPATH, $FRAMEEDITORSAVEPATH, etc. Your installer can easily set up more. Make user locate files (JFileChooser)
16
Example: Fixing filenames “images/starters/icon1.gif” or “images\\starters\\icon1.gif” should be replaced by “images” + file.separator + “starters” + file.separator + “icon1.gif” Avoid symbolic links and absolute paths. e.g. “images” needs to be replaced by “..” + file.sep + “..” + file.sep + “images” + file.sep + “starters” in a number of the demos.
17
Environment variables Can get a few of these with System.getProperties() Can get all from shell, but code will be highly system dependent. Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(“set”); //…code to grab output from process… Demo code
18
RunTime and System System provides a system-independent way of accessing system resources, but is very limited.System Runtime lets you execute OS commands, and fiddle with the Java Interpreter and Java Virtual Machine. You should minimize and encapsulate this code to retain some portability.Runtime
19
The Runtime Object
20
“System calls” Get the Runtime instance (there can be only one). Runtime rt = Runtime.getRuntime(); Call an exec method. This returns a Process object and may throw an IOException.exec Monitor the returned Process.Process
21
Runtime Example Runtime.getRuntime() gets the Runtime object. exec() creates a Process object. Wrap the InputStream produced by the Process object, then use read*() methods to read the input. waitFor(), destroy() methods available.
22
Shells A tool for low-level OS control. High-level conveniences, such as variables, job control, automatic completion, scripting. Downside: steep learning curve, ugly. Perfect candidate for a GUI!
23
Shell --> WinDohs 101 Goal: Ability to open and close directories, and to view files and their properties. Shell normally lets you into only one directory at a time; this doesn’t make sense for our GUI. Each window can display the contents of a directory. When to refresh?
24
Oh no! We must be experts at using the File, System, and Runtime objects. We must be experts on the shell. We must be experts on the OS. We must be experts on GUI’s. We must be crazy!
25
Don’t Panic! Basically, our job is translation. The shell will do all the really hard work. Plenty of help out there for the asking. We have already learned most of what we need.
26
AbstractButtons (JVM) Runtime interacts with runs creates User WinDohs DirectoryFrame User creates invokes Process creates data
27
FolderSelector Done Quit Cancel click on refresh button or refresh timer event. check permissions exec(“ls”) display output if ok create desktop bring up FolderSelector QuitDialog Start click on directory name check permissions open and refresh a new window if ok
28
Summary Work on FrameEditor! Study the Demos Course Project Specs TBA this week
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.