Results 1 to 2 of 2

Thread: erase function

  1. #1
    masa is offline Member
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Default erase function

    i am able to draw a circle using java graphics 2d and using swing. but i want to erase the circle once the erase icon is clicked and the mouse is moved over the circle. i am not able to that. can any one tell how erase function works. i could not paste the code as it is a huge one. so please help me out.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    
    public class Erase extends JPanel {
        List<Shape> list = new ArrayList<Shape>();
        int selectedIndex = -1;
    
        public Erase() {
            list.add(new Rectangle(25, 25, 75, 40));
            list.add(new Rectangle(200, 50, 75, 100));
            list.add(new Rectangle(90, 175, 40, 75));
            list.add(new Rectangle(300, 240, 40, 90));
            addMouseListener(selector);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            for(int i = 0; i < list.size(); i++) {
                Color color = (i == selectedIndex) ? Color.red : Color.blue;
                g2.setPaint(color);
                g2.draw(list.get(i));
            }
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(400,400);
        }
    
        private JPanel getErasePanel() {
            JButton button = new JButton("erase selected shape");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(selectedIndex != -1) {
                        list.remove(selectedIndex);
                        selectedIndex = -1;
                        repaint();
                    }
                }
            });
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        public static void main(String[] args) {
            Erase test = new Erase();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.add(test.getErasePanel(), "Last");
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private MouseListener selector = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                Point p = e.getPoint();
                for(int i = 0; i < list.size(); i++) {
                    if(list.get(i).contains(p)) {
                        if(i == selectedIndex) {
                            selectedIndex = -1;
                        } else {
                            selectedIndex = i;
                        }
                        repaint();
                        break;
                    }
                }
            }
        };
    }

Similar Threads

  1. function
    By nanna in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 10:20 PM
  2. Need help with get function
    By calicocal in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 08:59 PM
  3. How to erase line(s) from a console?
    By Tamu in forum Advanced Java
    Replies: 3
    Last Post: 11-02-2008, 03:00 PM
  4. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  5. How do erase image drawn
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •