Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Help Information on the Lab Practices 
Help Information on Lab Practices 
 
 
Making use of existing package 
 
For this module, you are encouraged to make use of Nick Efford’s software on the CD 
that attached to the recommended book (Nick Efford, Digital Image Processing, A 
Practical Introduction using Java, Addison Wesley, ISBN 0201596237, May 2000). 
With the author’s agreement of using the codes for our lab sessions, we put the 
content on the CD on a secured site so that you can download the codes and practise 
some tasks based on the software. 
 
Be aware that when you download the codes and unzip the files, you may need to 
disable “read-only” property: 
 
 
 
 
 
 
1. Right click on the program folder and select 
‘properties’. 
 
 
 
 
 
 
 
2. Another window will be appeared please 
unchecked ‘Read-only’ and select ‘OK’. 
Quick start of Netbeans IDE 3.6 1
Help Information on the Lab Practices 
 
 
 
 
3. In ‘Confirm Attribute Changes’ window, 
please select ‘Apply changes to this folder, 
subfolders and files’ and press ‘OK’. 
 
 
 
 
 
4. After pressing ‘OK’ in the final window, the 
folder, its subfolders and its files will be writable. 
 
How to run an existing code 
 
ƒ Adding classes and libraries: 
ƒ  
For running source codes in the books in the Netbeans IDE 3.6, you 
can ‘mount’ libraries/classes (those classes that most programs in the 
book are using) and programs (the order of mounting can be 
important). You mount the file/directory where the classes(libraries) 
are located. 
 
 
 
 
 
1. Open NetBeans 3.6, click menu File, choose 
‘Mount Filesystem’. 
 
Quick start of Netbeans IDE 3.6 2
Help Information on the Lab Practices 
 
 
 
 
2. Choose local directory, and 
click ‘Next’.  
 
 
 
 
 
 
3.  Browse to the file directory 
you want, eg, …\Java sample by 
Nick Efford\Apps\Classes, and 
then click Finish. 
 
 
 
 
 
 
ƒ Mounting programs: 
Now, you need mount the file/directory where the programs are 
located (in this example, we use chapter 5).  
 
 
 
 
 
 
1. Open NetBeans 3.6, click menu File, choose 
Mount filesyste. 
 
 
 
 
 
2. Choose local directory, and click 
‘Next’.  
 
Quick start of Netbeans IDE 3.6 3
Help Information on the Lab Practices 
 
 
 
 
3. Browse to the file directory you want, eg, 
…\Java sample by Nick Efford\Apps\Chapter5, 
and then click Finish. 
 
 
 
ƒ Preparing for execution: 
For running, you can set up your program parameters (in this example 
is ‘ImageViewer.java’). 
 
 
 
1. Activate your program 
Click on your program (here is 
ImageViewer in chapter 5) in the left 
side of Neatbeans IDE 3.6 
 
 
 
 
 
 
 
2. Right-click on the program (ImageViewer) 
and click on ‘Properties’. 
Quick start of Netbeans IDE 3.6 4
Help Information on the Lab Practices 
 
 
 
 
In the windows of ‘Properties…’, you can 
set arguments of your main function (this is 
equal to previously when you ran your 
program in  ‘command prompt’ the 
arguments could be added  as  
“java program argument1 argument2…” 
used by ‘main’ function, which is known as 
argv[i]). 
 
Setting argument can also be done by 
choosing from menu Build, and then select 
“set arguments…”. 
 
3. Click on ‘...’ in front of ‘ Arguments’.  
 
 
 
 
 
 
4. In new window you can write your arguments 
and press ‘OK’ (which, in this example, it is 
‘mattgrey.jpg’). 
 
 
 
In the windows of ‘Properties…’, you can set the 
working directory of your program to a specific 
directory which helps you to use files of that 
folder without using ‘full-path’ of files and it 
will be used for saving program outputs by 
Netbeans IDE 3.6. In order to do such setting: 
 
5. Click on ‘...’ in front of ‘Executor’. 
 
Quick start of Netbeans IDE 3.6 5
Help Information on the Lab Practices 
 
 
 
 
 
6. In new windows, click on ‘...’ in front of 
‘Working Directory’. 
 
 
 
 
 
7. Select your favourite folder (in this case we 
select ‘…\Java sample by Nick Efford\images’ 
which can help us use all sample images). 
 
 
 
 
 
 
 
8. By right-click on your program 
(ImageViewer) and selecting ‘Execute’ (or just 
directly press F6)you can run the program. 
 
 
 
 
 
9. The result will be the window of your program 
(in this example is an image viewer window). 
 
 
Quick start of Netbeans IDE 3.6 6
Help Information on the Lab Practices 
 
o Important Practice: 
 
Try to apply same routine for Dither program in chapter 5 (‘Dither.java’), after 
successful finishing your practice, we may modify the program in next step for your 
coursework: 
 
ƒ How to modify the existing program and create you own 
application 
 
Attached here is the code that I have changed from Dither program, in order to 
achieve the task to produce a mirror image that I have shown you during one of 
the lectures. You can make a careful comparison between this code and the code 
for Dither, to see where I have changed. By doing so, I hope you will be able to 
continue to do the rest of the coursework. Although this is not an elegant way to 
write a program, at least this will give you a sense of how to manipulate java 
codes in order to achieve your own tasks. Most importantly, I hope this will help 
you get familiar and comfortable with programming in Java and perhaps gradually 
learn to write elegant object oriented software.  
 
You can make a hardcopy of Dither first, and change the new content/class name 
within NetBean environment. 
 
 
/*************************************************************************** 
 
mirrowXReflection.java  copied from Dither.java 
 
  Given an image filename as a command line argument, this program reads 
  the image stored in that file and produce the mirror image horizontally 
  The original image and the mirrored images are 
  displayed in a tabbed pane, so it is easy to switch back and forth 
  between them.  Colour images are converted to greyscale images before 
  the mirror change. 
 
  Example of use: 
 
    java mirrowXReflection greyimg.jpg 
 
 
  Dither was Written by Nick Efford.  
 mirrorXReflection is modified by LT 
 
  pyright (c) 2000, Pearson Education Ltd.  All rights reserved. 
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR 
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE 
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
Quick start of Netbeans IDE 3.6 7
Help Information on the Lab Practices 
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
***************************************************************************/ 
 
 
 
import java.io.*; 
import java.awt.image.*; 
import java.awt.color.ColorSpace; 
import javax.swing.*; 
import com.pearsoneduc.ip.io.*; 
import com.pearsoneduc.ip.gui.*; 
 
 
 
public class mirrorXReflect extends JFrame { 
 
 
  private BufferedImage sourceImage;   // image to be mirrored 
  private ImageView[] views;           // image display components 
 
 
  public mirrorXReflect(String imageFile) throws IOException, ImageDecoderException { 
 
    super("xReflect: " + imageFile); 
    readImage(imageFile); 
 
    views = new ImageView[2]; 
    views[0] = new ImageView(sourceImage); 
    views[1] = new ImageView(xReflectionInPlace(sourceImage)); 
     
    JTabbedPane tabbedPane = new JTabbedPane(); 
    tabbedPane.add(new JScrollPane(views[0]), "input"); 
    tabbedPane.add(new JScrollPane(views[1]), "xReflect"); 
     
    getContentPane().add(tabbedPane); 
    addWindowListener(new WindowMonitor()); 
 
  } 
 
 
  /* 
   * Read an image from a named file, converting 
   * to greyscale if necessary. 
   */ 
 
  public void readImage(String filename) 
   throws IOException, ImageDecoderException { 
    ImageDecoder input = ImageFile.createImageDecoder(filename); 
    sourceImage = input.decodeAsBufferedImage(); 
    if (sourceImage.getType() != BufferedImage.TYPE_BYTE_GRAY) { 
      System.err.println("Converting colour image to greyscale image..."); 
      ColorConvertOp op = new ColorConvertOp( 
       ColorSpace.getInstance(ColorSpace.CS_GRAY), null); 
      BufferedImage greyImage = op.filter(sourceImage, null); 
      sourceImage = greyImage; 
    } 
  } 
 
 
Quick start of Netbeans IDE 3.6 8
Help Information on the Lab Practices 
  /* 
   * Compute and return a dithered version of the source image, 
   * using the supplied dither matrix. 
   */ 
 
  public BufferedImage xReflectionInPlace(BufferedImage image) { 
 
    BufferedImage reflectedImage = new 
BufferedImage(image.getWidth(),image.getHeight(),image.getType()); 
    for (int y=0; y 0) { 
      
         try { 
        JFrame frame = new mirrorXReflect(argv[0]); 
         
        frame.pack(); 
        frame.setVisible(true); 
      } 
      catch (Exception e) { 
        System.err.println(e); 
        System.exit(1); 
      } 
    } 
    else { 
      System.err.println("usage: java xReflectedInPlace  "); 
      System.exit(1); 
    } 
  } 
 
} 
Quick start of Netbeans IDE 3.6 9
Help Information on the Lab Practices 
 
Possible problems and solutions: 
 
 
ƒ You should be able to see the file system in NetBeans on the left-hand side. 
You may see some red lines in the program if you choose one of the program 
(eg, Dither) and double click the program name and program will be shown on 
the right. Red colour underneath a sentence in the program means that there is 
a compiling error. In this case it is because the NetBeans cannot find those 
packages mentioned here. We need to set the classpath and mount the package 
files (or mounting libraries as we did in the beginning of this guidance). 
 
 
      
Red colour 
underneath a 
sentence in the 
program means 
that there is a 
compiling error. 
In this case it is 
because the 
NetBeans 
cannot find 
those packages 
mentioned here. 
We need to set 
the classpath 
and mount the 
package files. 
 
 
 
 
ƒ If you run the program without setting any arguments (if you do not do 
preparing for execution step in this document), just by pressing F6, you may 
find from the output window, it says:  
 
usage: java Dither  
 
This means you need to provide argument “imagefile” in order to run the 
program. So we set the argument by the following steps: Choose from menu 
Build, and then select “set arguments…”, then you will see this dialogue. You 
can type 
 
mattgrey.jpg (if your working directory is “Java sample by Nick Efford\images”; 
however, if your working directory is somewhere else, e.g., under Chap05, 
then you can type  
..\..\Images\mattgrey.jpg   
Quick start of Netbeans IDE 3.6 10
Help Information on the Lab Practices 
as the argument.) In a nutshell, you need to give the ‘full-path’ of the 
argument so that the program can find the image from the current working 
directory. 
 
You can either use the sample images from the download files (under images 
directory) or you can use your own images as input (for this Dither program, it 
has to be grey level image). If get confused, you can simply copy any image to 
be used to your working directory and just use image name as argument 
without involving using ‘full-path’ of files. And then you can run the program 
once again by pressing F6 or choose command from menu Build -> Execute 
F6. 
 
 
              
Quick start of Netbeans IDE 3.6 11