Advertisement
Guest User

java

a guest
Feb 24th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.MouseEvent;
  3. import java.awt.event.MouseListener;
  4. import java.awt.event.MouseMotionListener;
  5. import java.awt.geom.*;
  6. import javax.swing.*;
  7. import java.awt.event.InputEvent;
  8.  
  9. class GridPanel extends JPanel implements MouseListener, MouseMotionListener
  10. {
  11.  
  12. int numCols;
  13. int numRows;
  14. int getButton;
  15.  
  16. int colindex=-1;
  17. int rowindex=-1 ;
  18.  
  19.  
  20. int getColumn(int x){
  21. colindex =(numCols * x )/ getWidth();
  22.  
  23. return colindex;
  24. }
  25.  
  26. int getRow(int y){
  27.  
  28. rowindex = (numRows * y) / getHeight();
  29. return rowindex ;
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. public GridPanel(int nc, int nr)
  42. {
  43. numCols = nc;
  44. numRows = nr;
  45. addMouseListener(this);
  46. addMouseMotionListener(this);
  47.  
  48. }
  49.  
  50. Rectangle getRect(int thisCol, int thisRow)
  51. {
  52. // if input is out of range, return "null"
  53. if(thisCol <0 || thisRow < 0)
  54. return null;
  55. if(thisCol >= numCols || thisRow>=numRows)
  56. return null;
  57.  
  58. // otherwise, make and return the Rectangle
  59. int w = getWidth()/numCols;
  60. int h = getHeight()/numRows;
  61.  
  62. int x = thisCol*w;
  63. int y = thisRow*h;
  64.  
  65.  
  66. Rectangle myRect = new Rectangle(x,y,w,h);
  67. return myRect;
  68. }
  69.  
  70. public void paint(Graphics g)
  71. {
  72. g.setColor(Color.gray);
  73. g.fillRect(0,0,getWidth(), getHeight());
  74. g.setColor(Color.black);
  75.  
  76.  
  77. Graphics2D g2 = (Graphics2D)g;
  78. // we'll use Graphics2D for it's "draw" method -
  79. // neater than the Graphics "drawRect" suppled
  80. // (which you could also use)
  81.  
  82. for (int i = 0;i<numCols;i++)
  83. {
  84. for(int j = 0;j<numRows;j++)
  85. {
  86. Rectangle r = getRect(i,j);
  87. g2.draw(r);
  88. }
  89. }
  90.  
  91. Rectangle r1 = getRect(colindex,rowindex);
  92.  
  93. if (getButton ==1 ){
  94.  
  95. g2.setColor(Color.red);
  96.  
  97.  
  98. }else{
  99.  
  100. g2.setColor(Color.yellow);
  101.  
  102. }
  103.  
  104.  
  105. if(colindex >-1 && rowindex >-1){
  106.  
  107. g2.fillOval(r1.x,r1.y, r1.width, r1.height);
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. }
  119.  
  120.  
  121. public void mouseMoved(MouseEvent event){
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. }
  129.  
  130. public void mouseDragged(MouseEvent event){
  131. int x = event.getX();
  132. int y = event.getY();
  133.  
  134.  
  135.  
  136.  
  137.  
  138. getButton = event.getButton();
  139.  
  140.  
  141. int cc =event.getClickCount();
  142.  
  143. boolean dd = event.isControlDown();
  144.  
  145. getColumn(x);
  146. getRow(y);
  147.  
  148.  
  149. System.out.println("the x position is " + x);
  150. System.out.println("the y position is " + y);
  151. System.out.println("Mouse click " + rowindex+","+colindex);
  152. System.out.println("Mouse click " + cc + dd);
  153.  
  154.  
  155.  
  156. repaint();
  157.  
  158.  
  159.  
  160.  
  161.  
  162. }
  163.  
  164.  
  165. public void mouseClicked(MouseEvent event) {
  166. // TODO Auto-generated method stub
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. }
  175.  
  176. @Override
  177. public void mouseEntered(MouseEvent event) {
  178. // TODO Auto-generated method stub
  179. System.out.println("mouse enter :)))");
  180. }
  181.  
  182. @Override
  183. public void mouseExited(MouseEvent event) {
  184. // TODO Auto-generated method stub
  185. System.out.println("Exit :)))");
  186. }
  187.  
  188. @Override
  189. public void mousePressed(MouseEvent event) {
  190. // TODO Auto-generated method stub
  191. int x = event.getX();
  192. int y = event.getY();
  193.  
  194.  
  195. getButton = event.getButton();
  196. colindex =(7 * x )/ getWidth();
  197. rowindex = (5 * y) / getHeight();
  198. int cc =event.getClickCount();
  199.  
  200. boolean dd = event.isControlDown();
  201.  
  202.  
  203. System.out.println("the x position is " + x);
  204. System.out.println("the y position is " + y);
  205. System.out.println("Mouse click " + rowindex+","+colindex);
  206. System.out.println("Mouse click " + cc + dd);
  207.  
  208.  
  209.  
  210. repaint();
  211.  
  212.  
  213. }
  214.  
  215. @Override
  216. public void mouseReleased(MouseEvent event) {
  217. // TODO Auto-generated method stub
  218.  
  219.  
  220.  
  221.  
  222. }
  223.  
  224.  
  225. // copied from the W2MouseEvents for convenience
  226. // (so we can run the program from GridPanel class too!)
  227. public static void main(String[] args)
  228. {
  229. W2MouseEvents w = new W2MouseEvents();
  230. w.setVisible(true);
  231.  
  232.  
  233. }
  234.  
  235.  
  236.  
  237.  
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement