Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.geom.*;
- import javax.swing.*;
- import java.awt.event.InputEvent;
- class GridPanel extends JPanel implements MouseListener, MouseMotionListener
- {
- int numCols;
- int numRows;
- int getButton;
- int colindex=-1;
- int rowindex=-1 ;
- int getColumn(int x){
- colindex =(numCols * x )/ getWidth();
- return colindex;
- }
- int getRow(int y){
- rowindex = (numRows * y) / getHeight();
- return rowindex ;
- }
- public GridPanel(int nc, int nr)
- {
- numCols = nc;
- numRows = nr;
- addMouseListener(this);
- addMouseMotionListener(this);
- }
- Rectangle getRect(int thisCol, int thisRow)
- {
- // if input is out of range, return "null"
- if(thisCol <0 || thisRow < 0)
- return null;
- if(thisCol >= numCols || thisRow>=numRows)
- return null;
- // otherwise, make and return the Rectangle
- int w = getWidth()/numCols;
- int h = getHeight()/numRows;
- int x = thisCol*w;
- int y = thisRow*h;
- Rectangle myRect = new Rectangle(x,y,w,h);
- return myRect;
- }
- public void paint(Graphics g)
- {
- g.setColor(Color.gray);
- g.fillRect(0,0,getWidth(), getHeight());
- g.setColor(Color.black);
- Graphics2D g2 = (Graphics2D)g;
- // we'll use Graphics2D for it's "draw" method -
- // neater than the Graphics "drawRect" suppled
- // (which you could also use)
- for (int i = 0;i<numCols;i++)
- {
- for(int j = 0;j<numRows;j++)
- {
- Rectangle r = getRect(i,j);
- g2.draw(r);
- }
- }
- Rectangle r1 = getRect(colindex,rowindex);
- if (getButton ==1 ){
- g2.setColor(Color.red);
- }else{
- g2.setColor(Color.yellow);
- }
- if(colindex >-1 && rowindex >-1){
- g2.fillOval(r1.x,r1.y, r1.width, r1.height);
- }
- }
- public void mouseMoved(MouseEvent event){
- }
- public void mouseDragged(MouseEvent event){
- int x = event.getX();
- int y = event.getY();
- getButton = event.getButton();
- int cc =event.getClickCount();
- boolean dd = event.isControlDown();
- getColumn(x);
- getRow(y);
- System.out.println("the x position is " + x);
- System.out.println("the y position is " + y);
- System.out.println("Mouse click " + rowindex+","+colindex);
- System.out.println("Mouse click " + cc + dd);
- repaint();
- }
- public void mouseClicked(MouseEvent event) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseEntered(MouseEvent event) {
- // TODO Auto-generated method stub
- System.out.println("mouse enter :)))");
- }
- @Override
- public void mouseExited(MouseEvent event) {
- // TODO Auto-generated method stub
- System.out.println("Exit :)))");
- }
- @Override
- public void mousePressed(MouseEvent event) {
- // TODO Auto-generated method stub
- int x = event.getX();
- int y = event.getY();
- getButton = event.getButton();
- colindex =(7 * x )/ getWidth();
- rowindex = (5 * y) / getHeight();
- int cc =event.getClickCount();
- boolean dd = event.isControlDown();
- System.out.println("the x position is " + x);
- System.out.println("the y position is " + y);
- System.out.println("Mouse click " + rowindex+","+colindex);
- System.out.println("Mouse click " + cc + dd);
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent event) {
- // TODO Auto-generated method stub
- }
- // copied from the W2MouseEvents for convenience
- // (so we can run the program from GridPanel class too!)
- public static void main(String[] args)
- {
- W2MouseEvents w = new W2MouseEvents();
- w.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement