Don't like ads? PRO users don't see any ads ;-)
Guest

draw square on click

By: artificialexit on Apr 25th, 2012  |  syntax: Java  |  size: 1.96 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.BorderLayout;
  2.  
  3.  
  4. public class Main extends JFrame {
  5.  
  6.         /**
  7.          *
  8.          */
  9.         private static final long serialVersionUID = 1L;
  10.         private JPanel contentPane;
  11.  
  12.         /**
  13.          * Launch the application.
  14.          */
  15.         public static void main(String[] args) {
  16.                 EventQueue.invokeLater(new Runnable() {
  17.                         public void run() {
  18.                                 try {
  19.                                         Main frame = new Main();
  20.                                         frame.setVisible(true);
  21.                                 } catch (Exception e) {
  22.                                         e.printStackTrace();
  23.                                 }
  24.                         }
  25.                 });
  26.         }
  27.  
  28.         /**
  29.          * Create the frame.
  30.          */
  31.         public Main() {
  32.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.                 setBounds(100, 100, 450, 300);
  34.                 contentPane = new JPanel();
  35.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  36.                 contentPane.setLayout(new BorderLayout(0, 0));
  37.                 setContentPane(contentPane);
  38.                
  39.                 //JPanel obj = new Square();
  40.                 //contentPane.add(obj);
  41.                
  42.                 addMouseListener(new MouseListener() {
  43.                        
  44.                         @Override
  45.                         public void mouseReleased(MouseEvent e) {
  46.                                 // TODO Auto-generated method stub
  47.                                
  48.                         }
  49.                        
  50.                         @Override
  51.                         public void mousePressed(MouseEvent e) {
  52.                                 // TODO Auto-generated method stub
  53.                                
  54.                         }
  55.                        
  56.                         @Override
  57.                         public void mouseExited(MouseEvent e) {
  58.                                 // TODO Auto-generated method stub
  59.                                
  60.                         }
  61.                        
  62.                         @Override
  63.                         public void mouseEntered(MouseEvent e) {
  64.                                 // TODO Auto-generated method stub
  65.                                
  66.                         }
  67.                        
  68.                         @Override
  69.                         public void mouseClicked(MouseEvent e) {
  70.                                 // TODO Auto-generated method stub
  71.                                 JPanel obj = new Square(e.getX(), e.getY(), 10);
  72.                                 contentPane.add(obj);
  73.                                 revalidate();
  74.                         }
  75.                 });
  76.         }
  77.        
  78.        
  79.  
  80. }
  81.  
  82. class Square extends JPanel {
  83.         /**
  84.          *
  85.          */
  86.         private static final long serialVersionUID = 1L;
  87.         final int x;
  88.         final int y;
  89.         final int size;
  90.         /**
  91.          * Create the panel.
  92.          */
  93.         public Square(int x, int y, int size) {
  94.                 this.x = x;
  95.                 this.y = y;
  96.                 this.size = size;
  97.         }
  98.        
  99.         protected void paintComponent(Graphics g) {
  100.         super.paintComponent(g);
  101.         g.drawRect(x, y, size, size);
  102.     }
  103. }