Txerrinko

VentanaCirculos y Panel Dibujo

May 23rd, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. //VENTANA CIRCULOS Y PANEL DE DIBUJO
  2.  
  3. package Tanda1;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.io.*;
  8.  
  9.  
  10. public class VentanaCirculos extends JFrame implements ActionListener{
  11.  
  12.  private JTextField txtX , txtY , txtRadio;
  13.  private JButton botDib,botBorrar,botGuardar;  
  14.  
  15.  private panDibujo panCentro;
  16.  
  17.     VentanaCirculos() {
  18.           //PRIMER PANEL
  19.               //generamos el  JPanel  con GridLaout  ,2 filas y 3 columnas , hgap y vgap
  20.         JPanel panTextos=new JPanel(new GridLayout(2,3,15,3));
  21.         panTextos.add(new JLabel("X :"));
  22.         panTextos.add(new JLabel("Y :"));
  23.         panTextos.add(new JLabel("RADIO :"));
  24.        
  25.        
  26.         txtX=new JTextField(10);
  27.         txtY=new JTextField(10);
  28.         txtRadio=new JTextField(10);
  29.  
  30.         panTextos.add(txtX);
  31.         panTextos.add(txtY);
  32.         panTextos.add(txtRadio);
  33.        
  34.        //SEGUNDO PANEL
  35.      JPanel panBotones=new JPanel(new FlowLayout(FlowLayout.CENTER));
  36.      panBotones.add(botDib=new JButton ("Dibujar"));
  37.      panBotones.add(botBorrar=new JButton ("Borrar"));
  38.      panBotones.add(botGuardar=new JButton ("Guardar"));
  39.            
  40.                            //       3º PANEL      0 filas y 1 columna
  41.      JPanel panNorte=new JPanel(new GridLayout(0,1));
  42.      panNorte.add(panTextos);
  43.      panNorte.add(panBotones);
  44.        
  45.        
  46.      
  47.       panCentro=new panDibujo();
  48.      
  49.        
  50.        getContentPane().setLayout(new BorderLayout ());
  51.        //añadirlo a la zona norte ,y a la zona centro
  52.        getContentPane().add(panNorte,BorderLayout.NORTH);
  53.        getContentPane().add(panCentro,BorderLayout.CENTER);
  54.        
  55.        
  56.         pack();
  57.         setVisible(true);
  58.        
  59.        
  60.         botDib.addActionListener(new ActionListener(){
  61.            
  62.            
  63.             public void actionPerformed(ActionEvent arg0) {
  64.                 // TODO Auto-generated method stub
  65.                
  66.                 int x=Integer.parseInt(txtX.getText());
  67.                 int y=Integer.parseInt(txtY.getText());
  68.                 int r=Integer.parseInt(txtRadio.getText());
  69.                
  70.                 panCentro.dibujaCirculo(x, y, r);
  71.                
  72.             }
  73.         });
  74.    
  75.         botBorrar.addActionListener(new ActionListener(){
  76.  
  77.             @Override
  78.             public void actionPerformed(ActionEvent arg0) {
  79.                 // TODO Auto-generated method stub
  80.                
  81.                
  82.                 panCentro.borrar();
  83.                
  84.                
  85.                
  86.             }
  87.            
  88.            
  89.         });
  90.            
  91.         botGuardar.addActionListener(new ActionListener(){
  92.  
  93.             @Override
  94.             public void actionPerformed(ActionEvent arg0) {
  95.                 // TODO Auto-generated method stub
  96.                
  97.            
  98.             try {
  99.                 panCentro.guardar();
  100.             } catch (IOException e) {
  101.                 // TODO Auto-generated catch block
  102.                 e.printStackTrace();
  103.             }
  104.                
  105.                
  106.             }
  107.        
  108.     });
  109.        
  110.     }
  111.    
  112.    
  113.    
  114.    
  115.    
  116.    
  117.    
  118.     public static void main(String[] args) {
  119.         // TODO Auto-generated method stub
  120.  
  121.         VentanaCirculos v=new VentanaCirculos();
  122.        
  123.     }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.     @Override
  132.     public void actionPerformed(ActionEvent arg0) {
  133.         // TODO Auto-generated method stub
  134.        
  135.     }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.    
  144. }
  145. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  146. package Tanda1;
  147.  
  148. import javax.swing.*;
  149. import java.awt.*;
  150. import java.awt.event.*;
  151. import java.io.*;
  152. import java.util.*;
  153.  
  154. public class panDibujo extends JPanel {
  155.  
  156.     //hashmap que asocia a una clase de tipo point una clase de tipo integr
  157.    
  158.     private HashMap <Point,Integer> circulos;
  159.    
  160.     public void borrar()
  161.     {
  162.         circulos.clear();
  163.         this.removeAll();
  164.         this.repaint();
  165.     }
  166.    
  167.     public void guardar() throws IOException{
  168.        
  169.        
  170.         FileOutputStream archivo=new FileOutputStream("fich.txt"); 
  171.         ObjectOutputStream oos=new ObjectOutputStream(archivo);
  172.    
  173.        
  174.         Iterator it=circulos.keySet().iterator();
  175.        
  176.  
  177.         while (it.hasNext()){
  178.            
  179.             Integer clave=(Integer) it.next();
  180.             Integer c=circulos.get(clave);
  181.             oos.writeObject(c);
  182.            
  183.            
  184.            
  185.            
  186.         }
  187.     oos.close();
  188.        
  189.        
  190.        
  191.  
  192.     }
  193.    
  194.    
  195.    
  196.     panDibujo(){
  197.         //añadimos tamaño y color de fondo
  198.         this.setPreferredSize(new Dimension(500,500));
  199.         this.setBackground(Color.BLACK);
  200.         //al estar dentro de un BORDER Layout setSize no lo coge , por eso hemos de
  201.         //utilizar setPreferedSize
  202.        
  203.        
  204.         //siempre sera un metrodo
  205.         circulos=new HashMap<Point,Integer>();
  206.        
  207.        
  208.        
  209.        
  210.        
  211.         this.setVisible(true);
  212.        
  213.        
  214.     }
  215.    
  216.     void dibujaCirculo(int x , int y , int radio) {
  217.        
  218.       //crea el punto y crea el entero
  219.       Point p=new Point (x,y);
  220.       Integer r=new Integer(radio);
  221.                 //meter clave y valor
  222.      
  223.       //si ese punto contiene la clave p
  224.       if (circulos.containsKey(p)){   //if circulos.getP !=null  SHOWMESSAGEDIALOG("YA EXISTIA");
  225.          
  226.           JOptionPane.showMessageDialog(null, "Ya hay circulo en esa posicion");
  227.       }
  228.           else {
  229.        
  230.               circulos.put(p, r);
  231.                JOptionPane.showMessageDialog(null,circulos.size()+"Ahi circulo");
  232.              
  233.                //vulve a pintar en el JPanel
  234.                repaint();
  235.           }
  236.        
  237.     }
  238.    
  239.    
  240.     //mettodo que se va a sobreescribir cada vez que eejecutamos un componente
  241.  
  242. protected void paintComponent(Graphics g){
  243.    super.paintComponent(g);
  244.    
  245.    //recuperamos el conjunto de claves y hlo recorremos por un iterator
  246.    Iterator it= circulos.keySet().iterator();
  247.    Point p;
  248.    Integer r;
  249.    
  250.     g.setColor(Color.WHITE);
  251.    while (it.hasNext()){
  252.        //cada iter devolvera una clave de tipo point
  253.        p=(Point) it.next();
  254.        
  255.        //recuperamos el valor a partir de clave,sacando el radio
  256.        r= circulos.get(p);
  257.        
  258.         //metodo para escribir circulos   ,los 2 ultimos argumentos son el ancho y el alto
  259.         g.drawOval(p.x, p.y, r.intValue()*2, r.intValue()*2);
  260.        
  261.        
  262.         //guarar que gner el objeto hasmapr y lo redibuje
  263.        
  264.        
  265.        
  266.        
  267.        
  268.    }
  269.    
  270. }
  271.    
  272.     public static void main(String[] args) {
  273.         // TODO Auto-generated method stub
  274.  
  275.     }
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment