CIS-257 Home: http://www.c-jump.com/bcc/c257c/c257syllabus.html
In previous tutorial we discussed Custom 2D rendering animation controlled by a background thread. In this tutorial we add mouse events to the application.
Open DrawingBox2D.java
Add MouseListener and MouseMotionListener interfaces to the DrawingBox2D class:
public class DrawingBox2D extends javax.swing.JPanel
implements MouseListener, MouseMotionListener {
Modify DrawingBox2D class constructor to add mouse listeners:
public DrawingBox2D() {
initComponents();
addMouseMotionListener( this );
addMouseListener( this );
}
Add mWindow object reference to the attributes of the DrawingBox2D class:
public class DrawingBox2D extends javax.swing.JPanel {
private int degrees = 0;
private FrameDemo2D mWindow;
//...
The reason for this is that mouse events often need to tell the rest of the window about user clicks and mouse coordinates. In such case the methods of DrawingBox2D will be aable to call public methods of the mWindow object as necessary.
Add implementation of the mouse event handlers to the DrawingBox2D class:
public class DrawingBox2D extends javax.swing.JPanel implements MouseListener, MouseMotionListener { //... // 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) { // the mouse coordinates are // evt.getX() // evt.getY() } // MouseMotionListener implementation @Override public void mouseMoved(MouseEvent evt) { // the mouse coordinates are // evt.getX() // evt.getY() } @Override public void mouseDragged(MouseEvent e) { }
Add setWindow() method to the DrawingBox2D class:
// operations public void setWindow( FrameDemo2D aWindow ) { mWindow = aWindow; }
Open FrameDemo2D.java
Modify FrameDemo2D constructor to call setWindow() method of the drawing box:
public FrameDemo2D() {
initComponents();
setTitle( "2D Graphics Demo" );
DrawingBox2D drawingBox = (DrawingBox2D)pnlDrawingBoard;
drawingBox.setWindow( this );
}
//...
To test the mouse listener, try mouse clicking that changes the background color of the drawing box. First, add backgroundColor attribute to the DrawingBox2D class:
public class DrawingBox2D extends javax.swing.JPanel
implements MouseListener, MouseMotionListener {
//...
private Color backgroundColor;
Then, modify your mouse event handlers as follows:
public class DrawingBox2D extends javax.swing.JPanel implements MouseListener, MouseMotionListener { //... // MouseListener implementation @Override public void mousePressed(MouseEvent evt) { backgroundColor = getBackground(); setBackground( Color.GREEN ); repaint(); } @Override public void mouseReleased(MouseEvent evt) { setBackground( backgroundColor ); repaint(); }
Compie and run the application. You should see the background color change while you press the left mouse button.
After all steps of this tutorial are complete, the DrawingBox2D.java looks like this:
package drawingDemo; import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import static java.lang.Math.*; public class DrawingBox2D extends javax.swing.JPanel implements MouseListener, MouseMotionListener { private int degrees = 0; private FrameDemo2D mWindow; private Color backgroundColor; /** * Creates new form DrawingBox2D */ public DrawingBox2D() { initComponents(); addMouseMotionListener(this); addMouseListener(this); } // MouseListener implementation @Override public void mousePressed(MouseEvent evt) { backgroundColor = getBackground(); setBackground(Color.GREEN); repaint(); } @Override public void mouseReleased(MouseEvent evt) { setBackground(backgroundColor); repaint(); } @Override public void mouseEntered(MouseEvent evt) { } @Override public void mouseExited(MouseEvent evt) { } @Override public void mouseClicked(MouseEvent evt) { // the mouse coordinates are // evt.getX() // evt.getY() } // MouseMotionListener implementation @Override public void mouseMoved(MouseEvent evt) { // the mouse coordinates are // evt.getX() // evt.getY() } @Override public void mouseDragged(MouseEvent e) { } // operations public void setWindow(FrameDemo2D aWindow) { mWindow = aWindow; } public void update() { degrees = (++degrees % 360 == 0) ? 0 : degrees; } @Override public void paintComponent(Graphics graph) { super.paintComponent(graph); // Our own 2D rendering starts here: graph.setColor(Color.RED); graph.fillRect(10, 20, 20, 40); graph.setColor(Color.BLUE); graph.drawString("degrees=" + degrees, 35, 45); graph.drawLine(100, 100, 80, 80); // draw x axis graph.setColor(Color.GREEN); graph.drawLine(0, 100, 1000, 100); // draw sine graph.setColor(Color.BLUE); int oldx = 0; int oldy = 0; for (int x = 0; x < 360; ++x) { int y = (int) (-sin(Math.toRadians(x + degrees)) * 50) + 100; //int y = (int)( -cos( Math.toRadians(x) ) * 50 ) + 100; //int y = (int)( -tan( Math.toRadians(x) ) * 50 ) + 100; //int y = (int) (-1 / tan(Math.toRadians(x + degrees)) * 50) + 100; graph.drawLine(oldx, oldy, x, y); oldx = x; oldy = y; } }//paintComponent /** * 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"> 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> // Variables declaration - do not modify // End of variables declaration }
The copy of the NetBeans project for this tutorial is posted here: demo2D.zip