in this program , you will see very simple drawing tool , with few colors on it ,
...
i hope it attract you , ..
and if any comment , i will be happy to read it ..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GUITest extends JFrame implements MouseListener, MouseMotionListener {
private Color colors[]= {Color.black , Color.GREEN , Color.red , Color.yellow
,Color.white , Color.blue , Color.orange , Color.PINK};
private String ColorName[] = {"BlacK" , "GREEN" , "RED" , "yellow"
,"white", "blue" , "orange" ,"PINK" };
private JButton Button1, Button2, Button3 , Button4 , Button5, Button6, Button7 , Button8;
private JButton Incras , Decreas;
private JLabel label;
private Icon image = new ImageIcon("BackGroundInage.jpg"); // <- put any image, but it should be big size
private Point point , lastPoint;
int PaintSize=3;
int ColorIndex=0;
boolean set;
public GUITest()
{
super("Put the lines");
// Color buttons
buttonSetting(Button1 , 0);
buttonSetting(Button2 , 1);
buttonSetting(Button3 , 2);
buttonSetting(Button4 , 3);
buttonSetting(Button5 , 4);
buttonSetting(Button6 , 5);
buttonSetting(Button7 , 6);
buttonSetting(Button8 , 7);
//Line Button
lineButtonSetting(Incras,1);
lineButtonSetting(Decreas,-1);
getContentPane().setLayout(new FlowLayout());
// the image background
label = new JLabel();
label.setIcon(image);
getContentPane().add(label);
//the point
point = new Point(0,0);
lastPoint = new Point(0,0);
//add listener
addMouseListener(this);
addMouseMotionListener(this);
// the fram size
setSize(image.getIconWidth(),image.getIconHeight()+75);
setVisible(true);
}// end constructor *
// set the color, name, and eventListener of the Color buttons ------------
public void buttonSetting(JButton Button , int Index)
{
Button = new JButton(" ");
Button.setBackground(colors[Index]);
Button.setActionCommand(ColorName[Index]);
Button.addActionListener(//S1
new ActionListener(){//S2
public void actionPerformed (ActionEvent event )
{
selectColor(event.getActionCommand());
}
}//E2
);//E1
getContentPane().add(Button,BorderLayout.SOUTH);
}// end method buttonSetting *
// set the color, name, and eventListener of the size buttons -------------
public void lineButtonSetting(JButton Button , int sign)
{
Button = new JButton("");
//Button.setBackground(Color.cyan);
if(sign >= 0){
Button.setActionCommand("+ve");
Button.setText("Increas Brush size");
}// end encrease
else{
Button.setActionCommand("-ve");
Button.setText("Decreas Brush size");
}
Button.addActionListener(//S1
new ActionListener(){//S2
public void actionPerformed (ActionEvent event )
{
lineWidth(event.getActionCommand());
}
}//E2
);//E1
getContentPane().add(Button,BorderLayout.SOUTH);
}// end method lineButtonSetting*
// select the color to paint-----------------------------------------------
public void selectColor (String Color)
{
int i;
for( i=0; i < ColorName.length; i++)
{
if(ColorName.matches(Color)){break;}
} //end for loop
//set the color
if (i<ColorName.length){ColorIndex = i;}
}// end method selectColor
// change teh thechnis of the line-----------------------------------------
public void lineWidth (String sign)
{
if (sign.equals("+ve")) {PaintSize++;}
else
if (PaintSize >= 1) //minimum size = 1
{PaintSize--;}
}
//the Mouse Listen Values **
//mouseClicked--------------------------------------------------------------
public void mouseClicked (MouseEvent event)
{
}
//mousePressed--------------------------------------------------------------
public void mousePressed(MouseEvent event)
{
point.setLocation(event.getPoint());
lastPoint.setLocation(point);
repaint(super.getGraphics());
}
//mouseReleased-------------------------------------------------------------
public void mouseReleased(MouseEvent event)
{
}
//mouseEntered--------------------------------------------------------------
public void mouseEntered(MouseEvent event)
{
}
//mouseExited---------------------------------------------------------------
public void mouseExited(MouseEvent event)
{
}
//mouseDragged--------------------------------------------------------------
public void mouseDragged(MouseEvent event)
{
repaint(super.getGraphics());
point.setLocation(event.getPoint());
}
//mouseMoved----------------------------------------------------------------
public void mouseMoved(MouseEvent event )
{
}
//Pint the line-------------------------------------------------------------
public void repaint (Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setColor(colors[ColorIndex]);
g2.setStroke(new BasicStroke(PaintSize));
g2.drawLine(lastPoint.x,lastPoint.y,point.x,point.y);
g2.fillOval(point.x,point.y,1,1);
lastPoint.setLocation(point);
}// end paint method
public static void main(String[] args) {
System.out.println("\n\n\tStart the main Method:\n");
// the call for the classes start from hear ---VV--V--Vv ----vV-------------
//GUITest test = new GUITest();
GUITest test = new GUITest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// the call for the classes end hear ------------^^-^^^---^-^^^^^-----------
}//end the main method
}// end class