⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myclickexample.java

📁 simple click. you can use this to complete your assignment
💻 JAVA
字号:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *  MyClickExample  -- demonstrates how to implement a swing
 *                     canvas which acts as a user interface
 *                     component.
 *              Also demonstrates how to write
 *              call-backs.
 *  @author     Adam J Butcher (ug75ajb@cs.bham.ac.uk)
 *              Modified by Mark Ryan (m.d.ryan@cs.bham.ac.uk)
 *  @version    0.1, 2001.0.0
 */
public class MyClickExample
{
    public static void main( String args[] )
    {
        // construct a window for the area
        // -------------------------------
        JFrame frame = new JFrame();
        frame.setSize( new Dimension( 350, 350 ) );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        // construct a zone area with 3 x 4 zones
        // separated by 10 pixels
        // -----------------------------------
        final ZoneArea za = new ZoneArea( 3, 4, 10 );

        // Make a listener for the ZoneArea
        // note use of Adapters, and anonymous inner classes
        MouseListener ml = new MouseAdapter()
    {
              public void mouseClicked(MouseEvent e)
              {
                final int index = za.getZoneIndexAt( e.getPoint() );
                if( index != -1 )
                  System.err.println( "Clicked on zone "+index );
              }
              //    public void mouseExited(MouseEvent e)
              //    public void mouseEntered(MouseEvent e)
              //    public void mousePressed(MouseEvent e)
              //    public void mouseReleased(MouseEvent e)
          };

        Container cp = frame.getContentPane();
        
        // add the listener to the zone area, and the zone area to the frame
        // ---------------------------------------
        za.addMouseListener(ml);
        cp.setLayout( new BorderLayout( 0, 0 ) );
        cp.add(za);
        
        // show the frame and run its message thread
        // the main method will not exit until
        // all frames are disposed of
        // ----------------------------------------
        frame.show();
    };
};

/**
 *  ZoneArea -- A zone area
 *              consists of an array of equally sized
 *              rectangles spaced by a fixed number of
 *              pixels.
 *
 *              This version contains callbacks to be overloaded
 *              by the required function.
 *
 *  @author     Adam J Butcher (ug75ajb@cs.bham.ac.uk)
 *  @version    0.1, 2001.0.0
 */
class ZoneArea extends JPanel
{
    /** the number of horizontal zones */
    protected int nHoriz;
    /** the number of vertical zones */
    protected int nVert;
    /** the inter-zone gap (in pixels) */
    protected int nGap;
    
  // Constructor    
    ZoneArea( int nHoriz, int nVert, int nGap )
    {
        this.nHoriz = nHoriz;
        this.nVert = nVert;
        this.nGap = nGap;

    }

    // MEMBER FUNCTIONS

    /**
     * determine which zone a particular point lies within.
     * @param pnt the Point to check
     * @return the index of the zone at the given point
     *         or -1 if the point is not within a zone.
     */
    public int getZoneIndexAt( Point pnt )
    {
        final int x = (int)pnt.getX();
        final int y = (int)pnt.getY();

        final int w = (int)getWidth();
        final int h = (int)getHeight();

        final int nZoneWidth = (int)((w - nGap) / nHoriz) - nGap;
        final int nZoneHeight = (int)((h - nGap) / nVert) - nGap;
        
        final int col = (x - nGap) / ((w - nGap) / nHoriz);
        final int row = (y - nGap) / ((h - nGap) / nVert);
        
        /*
          see some more detail
          -------------------------------------
          System.err.println( "(x - nGap) == "+(x - nGap) );
          System.err.println( "(nZoneWidth + nGap) == "+(nZoneWidth + nGap) );
          System.err.println( "nZoneWidth == "+nZoneWidth );
          System.err.println( "(x - nGap) % (nZoneWidth + nGap) == " + ((x - nGap) % (nZoneWidth + nGap)));
          System.err.println( "nZoneWidth - ( (x - nGap) % (nZoneWidth + nGap) ) == " + (nZoneWidth - ( (x - nGap) % (nZoneWidth + nGap) )));
          System.err.println( "::::("+x+","+y+") -> ("+col+","+row+")::::" );
        */
        
        
         
        // are we in either of the initial gaps?
        // -------------------------------------
        if( x < nGap || y < nGap ) return -1;

        // in any of the ``afterzone'' gaps?
        // ---------------------------------
        if( (nZoneWidth  - ( (x - nGap) % (nZoneWidth  + nGap) ) < 0) ||
            (nZoneHeight - ( (y - nGap) % (nZoneHeight + nGap) ) < 0) )
        {
            return -1;
        };

        // sanity check on row and col
        // ---------------------------
        if( row >= nVert || col >= nHoriz ) return -1;
        
        return row*nHoriz + col;
    };
    
    public void paintComponent( Graphics g )
    {
        final Graphics2D gc = (Graphics2D)g;

        final int nZoneWidth = (int)((getWidth() - nGap) / nHoriz) - nGap;
        final int nZoneHeight = (int)((getHeight() - nGap) / nVert) - nGap;
        
        gc.clearRect(0,0,getWidth(),getHeight());
        
        for( int i=0; i<nHoriz; i++ )
        {
            for( int j=0; j<nVert; j++ )
            {
                gc.draw( new Rectangle( nGap+i*(nZoneWidth+nGap),
                                        nGap+j*(nZoneHeight+nGap),
                                        nZoneWidth,
                                        nZoneHeight ) );
            };
        };
    };
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -