Advertisement
Garro

Java Swing - Dibuja N Circulos

Jul 3rd, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8.  
  9. public class PS3{
  10.     static JFrame frame;
  11.     static JButton button;
  12.     static JTextField label;
  13.     static CenterPanel panel;
  14.     static int N;
  15.     static boolean buttonPressed = false;
  16.     public static void main (String args[]) {
  17.         PS3 gui = new PS3();
  18.         gui.go();
  19.     }
  20.     public void go(){
  21.         frame = new JFrame();
  22.         button = new JButton("Ingresar numero");
  23.         label = new JTextField();
  24.         panel = new CenterPanel();
  25.        
  26.         frame.setSize(600,600);
  27.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         frame.setLayout(new BorderLayout());
  29.         frame.setVisible(true);
  30.        
  31.         frame.add(label, BorderLayout.PAGE_START);
  32.         frame.add(panel, BorderLayout.CENTER);
  33.         frame.add(button, BorderLayout.PAGE_END);
  34.        
  35.         button.addActionListener(new buttonAction());
  36.         panel.addMouseListener(new panelAction());
  37.     }
  38.     class buttonAction implements ActionListener{
  39.         public void actionPerformed(ActionEvent a){
  40.             buttonPressed = false;
  41.             String answer = label.getText();
  42.             try{
  43.                 N = Integer.parseInt(answer);
  44.                 if(N<1){
  45.                     int E = Integer.parseInt("error.");
  46.                 }else{
  47.                     frame.setTitle("Dibujo "+N+" circulos");
  48.                     buttonPressed = true;
  49.                 }
  50.             }catch(NumberFormatException ex){
  51.                 JOptionPane.showMessageDialog(null,"El numero ingresado no es valido","ERROR!",JOptionPane.INFORMATION_MESSAGE);
  52.             }
  53.         }
  54.     }class panelAction implements MouseListener{
  55.         public void mouseClicked(MouseEvent m){
  56.             if(!buttonPressed){
  57.                 JOptionPane.showMessageDialog(null,"No se ha ingresado un numero.","ERROR!",JOptionPane.INFORMATION_MESSAGE);
  58.             }for(int i = 1; i<=N ; i++){
  59.                 panel.draw(m,i);
  60.             }
  61.         }public void mouseEntered(MouseEvent m){
  62.            
  63.         }public void mouseExited(MouseEvent m){
  64.            
  65.         }public void mousePressed(MouseEvent m){
  66.            
  67.         }public void mouseReleased(MouseEvent m){
  68.            
  69.         }
  70.     }public class CenterPanel extends JPanel{  
  71.         public void paintComponent(Graphics g){
  72.             super.paintComponent(g);
  73.             this.setBackground(Color.WHITE);
  74.         }public void draw(MouseEvent m,int i){
  75.             super.getGraphics().setColor(Color.BLACK);
  76.             super.getGraphics().drawOval(m.getX(),m.getY(),i*50,i*50);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement