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
}
}
Next: The ImageFrame >>
More Java Articles
More By A.P.Rajshekhar