// @topic T11740 2D Graphics demo 03 -- mouse events added // @brief class <tt>D2Canvas</tt> extends <tt>JPanel</tt><br />implements <tt>MouseListener</tt>, <tt>MouseMotionListener</tt> /* * D2Canvas.java * * Created on Mar 26, 2013, 11:01:49 AM */ package shopsimulation; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Shape; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.Rectangle2D; import java.util.ArrayList; public class D2Canvas extends javax.swing.JPanel implements MouseListener, MouseMotionListener { boolean debugFlag = false; ViewDesigner mWindow; ArrayList< Shape > shapePrimitives = new ArrayList< Shape >(); /** Creates new form D2Canvas */ public D2Canvas( ) { initComponents(); } @Override public void paint( Graphics gr ) { super.paint( gr ); Graphics2D gr2d = ( Graphics2D ) gr; Rectangle rect = this.getBounds(); gr2d.draw( new Rectangle2D.Double( rect.getX() + 10, rect.getY() + 10, rect.getWidth() - 20, rect.getHeight() - 20 ) ); for ( Shape shape : shapePrimitives ) { gr2d.draw( shape ); } } // MouseListener implementation @Override public void mousePressed(MouseEvent evt) { } @Override public void mouseReleased(MouseEvent evt) { } @Override public void mouseEntered(MouseEvent evt) { } @Override public void mouseExited(MouseEvent evt) { } @Override public void mouseClicked(MouseEvent evt) { mWindow.getTxtDrawingPrimitives().append( " " + evt.getX() + " " + evt.getY() ); } // MouseMotionListener implementation @Override public void mouseMoved(MouseEvent evt) { mWindow.getTxtMouseCoord().setText( evt.getX() + " " + evt.getY() ); } @Override public void mouseDragged(MouseEvent e) { } // Getters and Setters public void setWindow( ViewDesigner window ) { mWindow = window; addMouseMotionListener(this); addMouseListener(this); } // operations public void addShape( Shape shape ) { shapePrimitives.add( shape ); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables }