Results 1 to 2 of 2
Thread: erase function
- 04-07-2009, 02:34 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 21
- Rep Power
- 0
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.
- 04-09-2009, 03:46 AM #2
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
-
function
By nanna in forum New To JavaReplies: 1Last Post: 11-17-2008, 09:20 PM -
Need help with get function
By calicocal in forum New To JavaReplies: 10Last Post: 11-09-2008, 07:59 PM -
How to erase line(s) from a console?
By Tamu in forum Advanced JavaReplies: 3Last Post: 11-02-2008, 02:00 PM -
I want to add function
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:25 AM -
How do erase image drawn
By fernando in forum New To JavaReplies: 1Last Post: 08-06-2007, 05:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks