Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class window extends JFrame implements ActionListener {
  6.  
  7. JPanel North;
  8. JPanel Center;
  9. JPanel South;
  10. JButton draw;
  11. JTextField text;
  12.  
  13. int[][] board;
  14.  
  15. window(){
  16. this.setSize(600, 600);
  17. this.setTitle("Clicking and Stuff");
  18. this.setLayout(new BorderLayout());
  19.  
  20. North = new JPanel();
  21. this.add(North, BorderLayout.NORTH);
  22. this.draw=new JButton("Draw");
  23. this.draw.addActionListener(this);
  24. North.add(draw);
  25. text= new JTextField("Nothing");
  26. North.add(text);
  27.  
  28. Center = new JPanel();
  29. this.add(Center, BorderLayout.CENTER);
  30. Handler h = new Handler();
  31. Center.addMouseListener(h);
  32. Center.addMouseMotionListener(h);
  33.  
  34. South = new JPanel();
  35. this.add(South, BorderLayout.SOUTH);
  36.  
  37. this.setVisible(true);
  38. this.setResizable(false);
  39.  
  40. board = new int[10][10];
  41. for (int i = 0; i<10; i++){
  42. for (int j = 0; j<10; j++){
  43. board[i][j] = (int)(Math.random()*100);
  44. }
  45. }
  46.  
  47. }
  48.  
  49. void draws (Graphics g, int x, int y){
  50. int z = (x/40);
  51. int f = (y/40);
  52.  
  53. g.setColor(Color.RED);
  54. g.drawRect(z*40, f*40, 40,40);
  55.  
  56. }
  57.  
  58. void draw(Graphics g){
  59. for (int i = 0; i < 10; i++){
  60. for (int j = 0; j < 10; j++){
  61. g.drawRect(j*40+40, i*40+40, 40, 40);
  62. String s = Integer.toString(board[i][j]);
  63. g.drawString(s, j*40+60-(g.getFontMetrics().stringWidth(s)/2), i*40+65);
  64. }
  65. }
  66. }
  67.  
  68. private class Handler implements MouseMotionListener, MouseListener{
  69.  
  70. @Override
  71. public void mouseClicked(MouseEvent e) {
  72. Graphics g = Center.getGraphics();
  73. System.out.println(e.getX()+" "+e.getY());
  74. draws(g, e.getX(), e.getY());
  75. }
  76.  
  77. @Override
  78. public void mousePressed(MouseEvent e) {
  79.  
  80. }
  81.  
  82. @Override
  83. public void mouseReleased(MouseEvent e) {
  84.  
  85. }
  86.  
  87. @Override
  88. public void mouseEntered(MouseEvent e) {
  89.  
  90. }
  91.  
  92. @Override
  93. public void mouseExited(MouseEvent e) {
  94.  
  95. }
  96.  
  97. @Override
  98. public void mouseDragged(MouseEvent e) {
  99.  
  100. }
  101.  
  102. @Override
  103. public void mouseMoved(MouseEvent e) {
  104.  
  105. }
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. @Override
  113. public void actionPerformed(ActionEvent e) {
  114. Graphics g = Center.getGraphics();
  115. if (e.getSource() == draw){
  116. draw(g);
  117. }
  118. }
  119.  
  120. public static void main (String args[]){
  121. window g = new window();
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement