Home arrow Java arrow Page 3 - 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 - 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) {

        }

 

 

 

    }

 

}


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