Home arrow Java arrow Java 2D in the Real World
JAVA

Java 2D in the Real World


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.

Author Info:
By: A.P.Rajshekhar
Rating: 3 stars3 stars3 stars3 stars3 stars / 4
May 09, 2007
TABLE OF CONTENTS:
  1. · Java 2D in the Real World
  2. · The Paint Bar
  3. · The ImageFrame
  4. · The Entry Point

print this article
SEARCH DEVARTICLES

Java 2D in the Real World
(Page 1 of 4 )

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());

        }

    }

}

 


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials