import javax.swing.*; import java.applet.Applet; import java.awt.event.*; import java.awt.*; import java.util.Vector; public class Exec76 extends Applet { Graphics2D g2 ; Vector<Point> points = new Vector<Point>(); Polygon po = new Polygon(); Boolean eraser = false; public JButton bt; int x=0,y=0; int w = 20; public Exec76() { addMouseMotionListener(new MLis()); bt = new JButton("Draw"); bt.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e ) { if(!eraser) { bt.setText("Eraser"); eraser = true; repaint(); } else { bt.setText("Draw"); eraser = false; repaint(); } } }); } public void init(){ addMouseMotionListener(new MLis()); bt = new JButton("Draw"); bt.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e ) { if(!eraser) { bt.setText("Eraser"); eraser = true; repaint(); } else { bt.setText("Draw"); eraser = false; repaint(); } } }); add(bt); } public void paint(Graphics g) { g2 = (Graphics2D)g; if (eraser) { Rectangle r = new Rectangle(x,y,w,w); g2.setColor(Color.BLUE); g2.fill(r); for(int i=0;i<points.size();i++) { if (r.contains(points.get(i))) points.remove(i); } } g2.setColor(Color.BLACK); for(int i=0;i<points.size();i++) { g2.fillOval((int)points.get(i).getX(), (int)points.get(i).getY(), 4, 4); } } class MLis extends MouseAdapter { public void mouseDragged(MouseEvent e) { if (eraser) { x = e.getX(); y = e.getY();repaint();} else { points.add(new Point(e.getX(),e.getY())); repaint(); } } } public static void main(String[] args) { JFrame f = new JFrame("Exec76"); f.setSize(400,400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); Exec76 panel = new Exec76(); f.add(panel.bt,BorderLayout.NORTH); f.add(panel); f.setVisible(true); } }