hello,my code is below mention, bt in this my eraser button doesnt show me any cursor in drawpad.. thanxxx in adva
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
//import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
//import java.awt.event.MouseMotionListener;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
//import javax.swing.text.SimpleAttributeSet;
//import javax.swing.text.StyleConstants;
/**
*
* @author hcl
*/
public class funnypaint implements ActionListener ,ItemListener{
JFrame f;
Graphics2D graph;
final PadDraw drawPad = new PadDraw();
JButton color,clear,eraser,pencil,rectangle;
JColorChooser fcolorChooser;
JDialog foregroundDialog;
JComboBox size1;
JPanel p,d;
Toolkit toolkit=Toolkit.getDefaultToolkit();
Image image=toolkit.getImage("C:/1.PNG");
//Point point=new point(0,0);
Point hotSpot = new Point(0,0);
Cursor crsr = toolkit.createCustomCursor(image,hotSpot, "Name to Call Cursor");
int x1,y1,x2,y2;
funnypaint(){
f= new JFrame("Paint");
f.setLayout(null);
p = new JPanel(null);
d= new JPanel(null);
p.setBounds(0,0,600,45);
p.setBackground(Color.pink);
d.setBounds(0,46,600,480);
drawPad.setBounds(0, 0, 600, 480);
d.add(drawPad);
//p.setVisible(true);
rectangle = new JButton("rect");
rectangle.setBounds(360,10,50,25);
clear = new JButton("clear");
clear.setBounds(55,10,65,25);
clear.setToolTipText("clear kar");
color = new JButton();
color.setBackground(Color.black);
color.setBounds(5,10,45,25);
eraser = new JButton("Eraser");
eraser.setBounds(125,10,85,25);
size1 = new JComboBox();
size1.setBounds(240,10,45,25);
for(int i =0;i<20;i++){
size1.addItem(i);
}
pencil = new JButton("pencil");
pencil.setBounds(300,10,50,25);
p.add(pencil);
p.add(rectangle);
p.add(size1);
p.add(eraser);
p.add(color);
p.add(clear);
f.add(p);
f.add(d);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
//f.setLocationRelativeTo(null);
f.setVisible(true);
f.setSize(600,600);
clear.addActionListener(this);
color.addActionListener(this);
eraser.addActionListener(this);
size1.addItemListener(this);
pencil.addActionListener(this);
rectangle.addActionListener(this);
}
public static void main(String args[])
{
funnypaint fp= new funnypaint();
}
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
if(color == e.getSource()){
if (fcolorChooser == null) {
fcolorChooser = new JColorChooser();
}
if (foregroundDialog == null) {
foregroundDialog = JColorChooser.createDialog(drawPad, "Set color", false, fcolorChooser, new ActionListener() {
public void actionPerformed(ActionEvent evvv) {
//color.setBackground(fcolorChooser.getColor());
color.setBackground( fcolorChooser.getColor());
drawPad.changecolor(fcolorChooser.getColor());
}
}, null);
}
foregroundDialog.setVisible(true);
}
if(clear == e.getSource()){
drawPad.clear();
}
if(eraser== e.getSource()){
drawPad.eraser();
}
if(pencil == e.getSource()){
drawPad.pencil();
}
if(rectangle == e.getSource()){
drawPad.rec();
}
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED){
int size = Integer.parseInt(size1.getSelectedItem() + "");
graph.setStroke(new BasicStroke (size));
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
class PadDraw extends JComponent{
Image image;
//this is gonna be your image that you draw on
//this is what we'll be using to draw on
int currentX, currentY, oldX, oldY;
//these are gonna hold our mouse coordinates
//Now for the constructors
public PadDraw(){
setDoubleBuffered(false);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
oldX = e.getX();
oldY = e.getY();
}
});
//if the mouse is pressed it sets the oldX & oldY
//coordinates as the mouses x & y coordinates
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
currentX = e.getX();
currentY = e.getY();
if(graph != null)
graph.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
});
//while the mouse is dragged it sets currentX & currentY as the mouses x and y
//then it draws a line at the coordinates
//it repaints it and sets oldX and oldY as currentX and currentY
}
public void paintComponent(Graphics g){
if(image == null){
image = createImage(getSize().width, getSize().height);
graph = (Graphics2D)image.getGraphics();
graph.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
//this is the painting bit
//if it has nothing on it then
//it creates an image the size of the window
//sets the value of Graphics as the image
//sets the rendering
//runs the clear() method
//then it draws the image
public void rec(){
setDoubleBuffered(false);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1= e.getY();
}
//if the mouse is pressed it sets the oldX & oldY
//coordinates as the mouses x & y coordinates
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
//if(graph != null)
graph.setColor(Color.red);
graph.drawRect(x1,y1,x2-x1,y2-y1);
graph.fillRect(x1,y1,x2-x1,y2-y1);
repaint();
}
//oldX = currentX;
//oldY = currentY;
});
}
public void clear(){
graph.setPaint(Color.white);
graph.fillRect(0, 0, getSize().width, getSize().height);
graph.setPaint(Color.black);
repaint();
}
//this is the clear
//it sets the colors as white
//then it fills the window with white
//thin it sets the color back to black
public void changecolor(Color thecolor){
graph.setPaint(thecolor);
repaint();
}
//this is the red paint
public void eraser(){
// graph.setStroke(new BasicStroke (2));
graph.setPaint(Color.WHITE);
this.setCursor(crsr);
}
public void pencil(){
// graph.setStroke(new BasicStroke (1));
graph.setPaint(Color.black);
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
}
}