Java
  Home arrow Java arrow Java 2D in the Real World
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java 2D in the Real World
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2007-05-09

    Table of Contents:
  • Java 2D in the Real World
  • The Paint Bar
  • The ImageFrame
  • The Entry Point

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java 2D in the Real World


    (Page 1 of 4 )

    Yesterday we discussed the Java 2D API: how it works and how to use it. Today we're going to get our hands dirty with code as we study a real world application.

    The application being developed exposes many of the advanced features of Java 2D API. It contains the following functions:

    Kucci.java - This file is the starting point of the application.

    Kucchi_PaintBar.java - This item draws and handles the shape and drawing functions.

    Kucchi_MenuBar.java - For the menu bar and the associated functionalities.


     

    ImageFrame.java - Displays the image onto the screen.

    Here is the Kucchi_MenuBar.java. It contains only File functions at present:

    package Kucchi;

      

    import javax.swing.JDialog;

    import java.awt.Graphics;

    import java.awt.Graphics2D;

    import javax.swing.JFrame;

    import javax.swing.JPanel;

    import javax.swing.JMenuBar;

    import javax.swing.JMenu;

    import javax.swing.JMenuItem;

    import java.awt.event.ActionListener;

    import java.awt.event.ActionEvent;

    import java.io.IOException;

    import javax.imageio.ImageIO;

    import javax.swing.JFileChooser;

    import java.awt.image.BufferedImage;

    import java.io.File;

     

     

    public class Kucchi_MenuBar extends JMenuBar {

        public File f;

        public BufferedImage image;

        public Graphics g;

        public Graphics2D g2d;

        public ImageFrame IFrame;

        /** Creates a new instance of MenuBar */

        public Kucchi_MenuBar() {

            K_FileMenu();

        }  

        public void K_FileMenu()  {  

            JMenu K_File;

            JMenuItem K_New,K_Open,K_Save,K_SaveAs,
    K_SaveAll,K_Print,K_PgSetup,K_Exit;

            K_File = new JMenu("File");  

            K_New = new JMenuItem("New");

            K_File.add(K_New);

            K_New.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    File_New();

                }

            }

            );

            K_Open = new JMenuItem("Open");

            K_File.add(K_Open);

            K_Open.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    try{File_Open();}

                    catch(IOException i){}

                }

            }

            );

            K_File.addSeparator();

            K_Save = new JMenuItem("Save");

            K_File.add(K_Save);

            K_Save.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    try{File_Save(f,image);}

                    catch(IOException i){}

                }

            }

            );

            //similarly set up other menu items

            K_File.addSeparator();

            K_Exit = new JMenuItem("Exit");

            K_File.add(K_Exit);

            K_Exit.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

     

      

                   if (e.getActionCommand() == "Exit") {

                        System.exit(0);

                    }

                }

            }

            );

            this.add(K_File);

        }

        

    public void File_New() {

            image = null;

            JFrame Jf = new JFrame();

             JPanel Jp = new JPanel();

            NewImage jd = new NewImage(Jf,"New Image",true);

            //image.setRGB(0,1,15);

            jd.show();

            System.out.println(jd.d_image.width +" "+ jd.d_image.height);

            Jf.setTitle(jd.t_title.getText());

            Jf.getContentPane().add(Jp);

            Jf.setSize(jd.d_image);

            Jf.show();

        }

        public void File_Open() throws IOException{

            NewFileChooser choose = new NewFileChooser();

            int state = choose.chooser.showOpenDialog(this);

            if(state == JFileChooser.APPROVE_OPTION){

                f = choose.chooser.getSelectedFile();

                image = ImageIO.read(f);

                String f_name = f.getName();

                IFrame = new ImageFrame(image,f_name);

                System.out.print(image.getWidth() + "         " + image.getHeight());

                IFrame.setSize(image.getWidth(),image.getHeight());

                IFrame.show();

            }

        }

        //It gets the file choosen through the JFileChooser and saves it using the

        //ImageIO class's write method

        public void File_Save(File f,BufferedImage image) throws IOException{

            if(f != null){

                if (f.exists()){

                    ImageIO.write(image," ",f);

                    System.out.println(f.toString());

                }}

            else{

                JFileChooser chooser = new JFileChooser();

                int result = chooser.showSaveDialog(null);

                if (result == JFileChooser.APPROVE_OPTION){

                    ImageIO.write(image,"jpg ",chooser.getSelectedFile());

                }

            }

        }

    //It gets the file choosen through the JFileChooser
    //and saves it using the

    //ImageIO class's write method with a different name

      

        public void File_SaveAs(File f,BufferedImage image) throws IOException{

            JFileChooser chooser = new JFileChooser();

            int result = chooser.showSaveDialog(null);

            if (result == JFileChooser.APPROVE_OPTION){

                ImageIO.write(image,"jpg ",chooser.getSelectedFile());

            }

        }

    }

     

    More Java Articles
    More By A.P.Rajshekhar


     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway