Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class window extends JFrame implements ActionListener {
- JPanel North;
- JPanel Center;
- JPanel South;
- JButton draw;
- JTextField text;
- int[][] board;
- window(){
- this.setSize(600, 600);
- this.setTitle("Clicking and Stuff");
- this.setLayout(new BorderLayout());
- North = new JPanel();
- this.add(North, BorderLayout.NORTH);
- this.draw=new JButton("Draw");
- this.draw.addActionListener(this);
- North.add(draw);
- text= new JTextField("Nothing");
- North.add(text);
- Center = new JPanel();
- this.add(Center, BorderLayout.CENTER);
- Handler h = new Handler();
- Center.addMouseListener(h);
- Center.addMouseMotionListener(h);
- South = new JPanel();
- this.add(South, BorderLayout.SOUTH);
- this.setVisible(true);
- this.setResizable(false);
- board = new int[10][10];
- for (int i = 0; i<10; i++){
- for (int j = 0; j<10; j++){
- board[i][j] = (int)(Math.random()*100);
- }
- }
- }
- void draws (Graphics g, int x, int y){
- int z = (x/40);
- int f = (y/40);
- g.setColor(Color.RED);
- g.drawRect(z*40, f*40, 40,40);
- }
- void draw(Graphics g){
- for (int i = 0; i < 10; i++){
- for (int j = 0; j < 10; j++){
- g.drawRect(j*40+40, i*40+40, 40, 40);
- String s = Integer.toString(board[i][j]);
- g.drawString(s, j*40+60-(g.getFontMetrics().stringWidth(s)/2), i*40+65);
- }
- }
- }
- private class Handler implements MouseMotionListener, MouseListener{
- @Override
- public void mouseClicked(MouseEvent e) {
- Graphics g = Center.getGraphics();
- System.out.println(e.getX()+" "+e.getY());
- draws(g, e.getX(), e.getY());
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- Graphics g = Center.getGraphics();
- if (e.getSource() == draw){
- draw(g);
- }
- }
- public static void main (String args[]){
- window g = new window();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement