Java ScreenShot
Screenshot      

Screenshot Core Java 2: Volume I - Fundamentals

Table of Contents
 2.  The Java Programming Environment


Graphical apps

The Welcome program was not terribly exciting. Next, let us run a graphical app. This program is a very simple GIF file viewer. It simply loads and displays a GIF file. Again, let us first compile and run it from the command line.

  1. Open a shell window.

  2. Change to the directory CoreJavaBook/v1ch2/ImageViewer.

  3. Enter:

    javac ImageViewer.java java ImageViewer
    


A new program window pops up with our ImageViewer app. (See Screenshot-11.)

Screenshot-11. Running the ImageViewer app

Java graphics 02fig11.gif


Now select File -> Open and look for a GIF file to open. (We supplied a couple of sample files in the same directory.) To close the program, click on the Close box in the title bar or pull down the system menu and close the program. (To compile and run this program inside a text editor or an integrated development environment, do the same as before. For example, for Emacs, choose JDE -> Compile, then choose JDE -> Run App.) We hope that you find this program interesting and useful. Have a quick look at the source code. The program is substantially longer than the first program, but it is not terribly complex if you consider how much code it would take in C or C++ to write a similar app. In Visual Basic, of course, it is easy to write or, rather, drag and drop, such a program—you need only add a couple of lines of code to make it functional. The Java SDK does not have a visual interface builder, so you need to write code for everything, as shown in Example 2-2. You will learn how to write graphical programs like this in s 7-9.

Java graphics caution_icon.gif

If you run this program with a version of the Java SDK prior to 1.4, then you will get a compile-time error at the line

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


In that case, comment out the line and recompile. Then the program won't exit when you close the frame. Instead, choose the File -> Exit menu option. See for more information on this issue.

Example 2-2 ImageViewer.java
 1. import java.awt.*;
 2. import java.awt.event.*;
 3. import java.io.*;
 4. import javax.swing.*;
 5.
 6. /**
 7. A program for viewing images.
 8. */
 9. public class ImageViewer
10. {
11. public static void main(String[] args)
12. {
13. JFrame frame = new ImageViewerFrame();
14. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15. frame.show();
16. }
17. }
18.
19. /**
20. A frame with a label to show an image.
21. */
22. class ImageViewerFrame extends JFrame
23. {
24. public ImageViewerFrame()
25. {
26. setTitle("ImageViewer");
27. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
28.
29. // use a label to display the images
30. label = new JLabel();
31. Container contentPane = getContentPane();
32. contentPane.add(label);
33.
34. // set up the file chooser
35. chooser = new JFileChooser();
36. chooser.setCurrentDirectory(new File("."));
37.
38. // set up the menu bar
39. JMenuBar menuBar = new JMenuBar();
40. setJMenuBar(menuBar);
41.
42. JMenu menu = new JMenu("File");
43. menuBar.add(menu);
44.
45. JMenuItem openItem = new JMenuItem("Open");
46. menu.add(openItem);
47. openItem.addActionListener(new
48. ActionListener()
49. {
50. public void actionPerformed(ActionEvent evt)
51. {
52. // show file chooser dialog
53. int r = chooser.showOpenDialog(null);
54.
55. // if file selected, set it as icon of the label
56. if(r == JFileChooser.APPROVE_OPTION)
57. {
58. String name
59. = chooser.getSelectedFile().getPath();
60. label.setIcon(new ImageIcon(name));
61. }
62. }
63. });
64.
65. JMenuItem exitItem = new JMenuItem("Exit");
66. menu.add(exitItem);
67. exitItem.addActionListener(new
68. ActionListener()
69. {
70. public void actionPerformed(ActionEvent event)
71. {
72. System.exit(0);
73. }
74. });
75. }
76. 77. private JLabel label;
78. private JFileChooser chooser;
79. private static final int DEFAULT_WIDTH = 300;
80. private static final int DEFAULT_HEIGHT = 400;
81. }

Java ScreenShot
Screenshot      
Top
 

Comments