Creating the Graphicizer Window


As with the previous windowed application in this book, the window the application draws is created in the application's constructor. And like the previous applications so far, this one is based on the Frame class.

Here's what Graphicizer's main method and constructor looks like; the constructor creates the window:

 public class Graphicizer extends Frame implements ActionListener {     BufferedImage bufferedImage, bufferedImageBackup;     Image image;     Menu menu;     MenuBar menubar;     MenuItem menuitem1, menuitem2, menuitem3, menuitem4;     Button button1, button2, button3, button4, button5;     FileDialog dialog;     public static void main(String[] args)     {         new Graphicizer();     }     public Graphicizer()     {         setSize(400, 360);         setTitle("The Graphicizer");         setVisible(true);         this.addWindowListener(new WindowAdapter(){             public void windowClosing(                 WindowEvent e){                     System.exit(0);                 }             }         );         .         .         . 

It adds the buttons the applications uses as drawing tools, which you can see in Figure 3.1:

 button1 = new Button("Emboss"); button1.setBounds(30, getHeight() - 50, 60, 20); add(button1); button1.addActionListener(this); button2 = new Button("Sharpen"); button2.setBounds(100, getHeight() - 50, 60, 20); add(button2); button2.addActionListener(this); button3 = new Button("Brighten"); button3.setBounds(170, getHeight() - 50, 60, 20); add(button3); button3.addActionListener(this); button4 = new Button("Blur"); button4.setBounds(240, getHeight() - 50, 60, 20); add(button4); button4.addActionListener(this); button5 = new Button("Reduce"); button5.setBounds(310, getHeight() - 50, 60, 20); add(button5); button5.addActionListener(this); . . . 

It also adds a File menu with the items Open…, Save As…, Undo, and Exit:

 menubar = new MenuBar(); menu = new Menu("File"); menuitem1 = new MenuItem("Open..."); menu.add(menuitem1); menuitem1.addActionListener(this); menuitem2 = new MenuItem("Save As..."); menu.add(menuitem2); menuitem2.addActionListener(this); menuitem3 = new MenuItem("Undo"); menu.add(menuitem3); menuitem3.addActionListener(this); menuitem4 = new MenuItem("Exit"); menu.add(menuitem4); menuitem4.addActionListener(this); menubar.add(menu); setMenuBar(menubar); . . . 

Besides the menu and button system, the constructor also creates a FileDialog object, which will be used to display a File dialog box when the user wants to open or save files:

     dialog = new FileDialog(this, "File Dialog"); } 

And that's exactly how the user startsby opening a file and displaying it in the Graphicizer.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net