Java
  Home arrow Java arrow Page 3 - 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  
Mobile Linux 
App Generation ROI 
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 - The ImageFrame


    (Page 3 of 4 )

    This class uses the following class to transfer the selected tool to the ImageFrame class:

    package Kucchi;

     

    public class ToolNum{

     

     

        static int ToolNum;

    }

     

    Next comes the ImageFrame.java

     

     

     

    package Kucchi;

      

     

    import java.awt.image.BufferedImage;

     

     

     

    import java.awt.Graphics;

     

     

     

    import java.awt.geom.Point2D;

     

     

     

    import javax.swing.JFrame;

     

     

     

    import javax.swing.JPanel;

    import java.awt.BorderLayout;

    import java.awt.event.MouseListener;

    import java.awt.event.MouseMotionListener;

    import java.awt.event.MouseEvent;

    import java.awt.BasicStroke;

     

    import java.awt.Graphics2D;

    import Kucchi.ToolNum;

      

    public class ImageFrame extends JFrame{

        public BufferedImage image;

        public Graphics g,g1;

        public Graphics2D g2;

        private BasicStroke dashStroke;

        int height,width;

        public Point2D pointstart,pointend;

        private boolean flag = true;

        int startx,starty,endx,endy;

        /** Creates a new instance of ImageFrame */

        public ImageFrame(BufferedImage img,String f_name) {

            super(f_name);

            image = img;

     

            g = image.createGraphics();

            float []dash = {1.5f};

            dashStroke = new BasicStroke(2.0f,BasicStroke.CAP_BUTT,BasicStroke.
    JOIN_MITER,10.0f,dash,0.0f);

            this.getContentPane().add(new ImageFrame.ImagePanel(),new BorderLayout().CENTER);

        }  

        public class ImagePanel extends JPanel implements MouseListener,MouseMotionListener{

            public ImagePanel(){

                addMouseListener(this);

                addMouseMotionListener(this);

            }

            /**

             * Paints the container. This forwards the paint to any lightweight

             * components that are children of this container. If this method is

             * reimplemented, super.paint(g) should be called so that lightweight

             * components are properly rendered. If a child component is entirely

             * clipped by the current clipping setting in g, paint() will not be

             * forwarded to that child.

             *

             * @param g the specified Graphics window

             * @see   Component#update(Graphics)

             *

             */

            public void paint(Graphics g) {

                g.drawImage(image,0,0,null);  

                if (g != null && pointstart != null && pointend != null){

                    startx = (int)pointstart.getX();

                    starty = (int)pointstart.getY();

                    endx = (int)pointend.getX();

                    endy = (int)pointend.getY();  

                    if(ToolNum.ToolNum != 1){

                        if(startx > endx){

                            int swap = endx;

                            endx = startx;

                            startx = swap;

                        }

                        if(starty > endy){

                            int swap = endy;

     

     

     

                            endy = starty;

                            starty = swap;

                        }

                    }

                    switch(ToolNum.ToolNum){

                        case 1:

                            g.drawLine(startx,starty,endx,endy);

                            break;

                        case 2:

     

     

     

                            g.drawOval(startx,starty,endx-startx,endy-starty);

                            break;

                        case 3:

                            g.drawRect(startx,starty,endx-startx,endy-starty);

                            break;

                        case 11:

                            g2 = (Graphics2D)g.create();

                            g2.setStroke(dashStroke);

                            g2.drawRect(startx,starty,endx-startx,endy-starty);

                            g2.dispose();

                            break;

                     //similarly for other shapes

                    }

                }

            }

            public void mouseClicked(java.awt.event.MouseEvent mouseEvent) { 

            }

     

     

     

            public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {

            }

     

     

     

            public void mouseExited(java.awt.event.MouseEvent mouseEvent) {

            }

           public void mousePressed(java.awt.event.MouseEvent mouseEvent) {

     

                pointstart = mouseEvent.getPoint();

     

            }

     

     

     

            public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {

                pointend = mouseEvent.getPoint();  

                if(ToolNum.ToolNum != 11){

                    update(g);

                }

                repaint();

            }

     

     

     

            public void mouseDragged(java.awt.event.MouseEvent mouseEvent) {

            }

     

     

     

            public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {

            }

     

     

     

        }

     

    }

    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 4 hosted by Hostway
    Stay green...Green IT