Home arrow Java arrow Page 2 - 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 Paint Bar
(Page 2 of 4 )

 

Next comes the class that provides shape functionalities to the application. It also allows drawing on an already loaded image. This class handles the Paint Bar clicks and sets the type of tool clicked.

 

package Kucchi;

 

import javax.swing.JToolBar;

 

import javax.swing.JButton;

import javax.swing.ImageIcon;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

  

public class Kucchi_PaintBar extends JToolBar implements ActionListener{

  

 

//Numbers associated with each tool

    final int TN_LINE = 1;

    final int TN_CIRCLE = 2;

    final int TN_RECTANGLE =3;

    final int TN_ELLIPSE = 4;

    final int TN_FILLCIRCLE = 5;

    final int TN_FILLRECTANGLE = 6;

    final int TN_FILLELLIPSE = 7;

    final int TN_POLYNOMIAL = 8;

    final int TN_POINTER = 9;

    final int TN_LASSO = 10;

    final int TN_AREASELECT = 11;

    final int TN_MAGICWAND = 12; 

    private JButton pointer,areaselect,lasso,magicwand;

    private JButton line,circ,rect,ellip;

    private JButton fillcirc,fillrect,fillellip,polynomial;

  

    //The constructor sets up control buttons with icons

    public Kucchi_PaintBar(){

        this.setFloatable(false);

        this.setOrientation(VERTICAL);

       

 

        //pointer = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/pointer.png"));

        pointer = new JButton("Pointer");

        pointer.addActionListener(this);

        this.add(pointer);

      

        areaselect = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/areaselect.png"));

        areaselect.addActionListener(this);

        this.add(areaselect);

 

 

        lasso = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/lasso.png"));

        lasso.addActionListener(this);

        this.add(lasso);   

        magicwand = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/magicwand.png"));

        magicwand.addActionListener(this);

        this.add(magicwand);

        this.addSeparator();

 

 

        //line = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/line.png"));

        line = new JButton("Line");

        line.addActionListener(this);

        this.add(line);  

        circ = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/circle.png"));

        circ.addActionListener(this);

        this.add(circ);  

        rect = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/rectangle.png"));

        rect.addActionListener(this);

        this.add(rect);  

        ellip = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/ellipse.png"));

        ellip.addActionListener(this);

        this.add(ellip);

        this.addSeparator();

 

        fillcirc = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/filledcircle.png"));

        fillcirc.addActionListener(this);

        this.add(fillcirc);

        fillrect = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/filledrectangle.png"));

        fillrect.addActionListener(this);

        this.add(fillrect);  

        fillellip = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/filledellipse.png"));

        fillellip.addActionListener(this);

        this.add(fillellip);

        polynomial = new JButton(new ImageIcon("Kucchi/Icons/ToolBar/arc.png"));

        polynomial.addActionListener(this);

        this.add(polynomial);

    }

  

    public void actionPerformed(java.awt.event.ActionEvent actionEvent) {

        if(actionEvent.getSource() == line){

            ToolNum.ToolNum = TN_LINE;

            System.out.println("Here");

        }

        if(actionEvent.getSource() == circ){

            ToolNum.ToolNum = TN_CIRCLE;

        }

        if(actionEvent.getSource() == rect){

            ToolNum.ToolNum = TN_RECTANGLE;

        }

        if(actionEvent.getSource() == ellip){

            ToolNum.ToolNum = TN_ELLIPSE;

        }

        //similar for other Paint bar items

    }    

 

 

}


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