import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Vector; import interactors.*; /** * A sample application that shows how to use an interactor. The application * creates 10 blue rectangles and registers them with a move interactor. The * move interactor allows the blue rectangles to be moved anywhere in the * window. */ class MoveApplication extends JPanel { JFrame window = new JFrame(); static final int RECT_HEIGHT = 50; static final int RECT_WIDTH = 100; static final int V_SPACING = 20; // list of movable rectangles Vector movableObjs = new Vector(); // RectShape objects will store their color--inefficient storage-wise but // convenient and simplifies the paintComponent code class RectShape extends Rectangle { Color color; RectShape(int left, int top, int wd, int ht, Color c) { super(left, top, wd, ht); color = c; } } public MoveApplication() { int i; // create an interactors event handler for this JPanel and attach // it to the JPanel EventHandler evtHandler = new EventHandler(this); MoveInteractor moveInter = new MoveInteractor(MouseEvent.MOUSE_PRESSED, MouseEvent.MOUSE_RELEASED); // create 10 movable rectangles and register them with the // move interactor for (i = 0; i < 10; i++) { RectShape newRect = new RectShape(10, 10+(i*(RECT_HEIGHT+V_SPACING)), RECT_WIDTH, RECT_HEIGHT, Color.BLUE); movableObjs.add(newRect); moveInter.add(newRect); } // register the move interactor with the event handler evtHandler.addInteractor(moveInter); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setContentPane(this); window.pack(); window.setVisible(true); } public Dimension getPreferredSize() { return new Dimension(400, (RECT_HEIGHT + V_SPACING) * 12); } public Dimension getMinimumSize() { return getPreferredSize(); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Color saveColor = g.getColor(); super.paintComponent(g); // draw the rectangles on the screen for (RectShape r : movableObjs) { g2.setColor(r.color); g2.fill(r); } g2.setColor(saveColor); } public static void main(String args[]) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new MoveApplication(); } }); } }