import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.Graphics2D; import javax.swing.*; import chapman.graphics.JCanvas; public class DrawBox extends JCanvas implements MouseListener, MouseMotionListener, ActionListener { final static int max_index=10; protected int oldx, oldy, activeIndex=-1, p=0, last_index; static Rect rect[] = new Rect[max_index]; DrawBox() { rect[0] = new ColoredRect(0,0,200,200); rect[1] = new DrawableRect(100,100,300,300); rect[2] = new ColoredRect(200,200,400,400, Color.blue, Color.red); last_index=3; } public void addBox() { if(last_index=0) { rect[activeIndex].move(e.getX()-oldx, e.getY()-oldy); oldx=e.getX(); oldy=e.getY(); System.out.println("in mouse dragged in("+oldx+":"+oldy+")"); repaint(); } } public void mouseMoved(MouseEvent e) { } public void mousePressed(MouseEvent e) { int i; for(i=0; i=0) { rect[activeIndex].ChangeColor(Color.red, Color.blue); activeIndex=-1; repaint(); } System.out.println("in mouse released in("+oldx+":"+oldy+")"); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void actionPerformed(ActionEvent e) { if(e.getActionCommand() == "Add Box!") { System.out.println("Add Box! Action occurred"); addBox(); } else if(e.getActionCommand() == "Delete Box!") { System.out.println("Delete Box! Action occurred"); } } public static void main(String s[]) { MyWindowListener l = new MyWindowListener(); DrawBox box = new DrawBox(); JFrame f = new JFrame("Draw Box..."); JPanel p1 = new JPanel(); JButton b1 = new JButton("Add Box!"); JButton b2 = new JButton("Delete Box!"); b1.addActionListener(box); b2.addActionListener(box); box.addMouseListener(box); box.addMouseMotionListener(box); p1.add(b1); p1.add(b2); f.addWindowListener(l); f.getContentPane().add(p1, BorderLayout.SOUTH); f.getContentPane().add(box, BorderLayout.CENTER); f.pack(); f.setSize(500,500); f.setVisible(true); } } class MyWindowListener extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } }