Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //VENTANA CIRCULOS Y PANEL DE DIBUJO
- package Tanda1;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- public class VentanaCirculos extends JFrame implements ActionListener{
- private JTextField txtX , txtY , txtRadio;
- private JButton botDib,botBorrar,botGuardar;
- private panDibujo panCentro;
- VentanaCirculos() {
- //PRIMER PANEL
- //generamos el JPanel con GridLaout ,2 filas y 3 columnas , hgap y vgap
- JPanel panTextos=new JPanel(new GridLayout(2,3,15,3));
- panTextos.add(new JLabel("X :"));
- panTextos.add(new JLabel("Y :"));
- panTextos.add(new JLabel("RADIO :"));
- txtX=new JTextField(10);
- txtY=new JTextField(10);
- txtRadio=new JTextField(10);
- panTextos.add(txtX);
- panTextos.add(txtY);
- panTextos.add(txtRadio);
- //SEGUNDO PANEL
- JPanel panBotones=new JPanel(new FlowLayout(FlowLayout.CENTER));
- panBotones.add(botDib=new JButton ("Dibujar"));
- panBotones.add(botBorrar=new JButton ("Borrar"));
- panBotones.add(botGuardar=new JButton ("Guardar"));
- // 3º PANEL 0 filas y 1 columna
- JPanel panNorte=new JPanel(new GridLayout(0,1));
- panNorte.add(panTextos);
- panNorte.add(panBotones);
- panCentro=new panDibujo();
- getContentPane().setLayout(new BorderLayout ());
- //añadirlo a la zona norte ,y a la zona centro
- getContentPane().add(panNorte,BorderLayout.NORTH);
- getContentPane().add(panCentro,BorderLayout.CENTER);
- pack();
- setVisible(true);
- botDib.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- int x=Integer.parseInt(txtX.getText());
- int y=Integer.parseInt(txtY.getText());
- int r=Integer.parseInt(txtRadio.getText());
- panCentro.dibujaCirculo(x, y, r);
- }
- });
- botBorrar.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- panCentro.borrar();
- }
- });
- botGuardar.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- try {
- panCentro.guardar();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- VentanaCirculos v=new VentanaCirculos();
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- package Tanda1;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.*;
- public class panDibujo extends JPanel {
- //hashmap que asocia a una clase de tipo point una clase de tipo integr
- private HashMap <Point,Integer> circulos;
- public void borrar()
- {
- circulos.clear();
- this.removeAll();
- this.repaint();
- }
- public void guardar() throws IOException{
- FileOutputStream archivo=new FileOutputStream("fich.txt");
- ObjectOutputStream oos=new ObjectOutputStream(archivo);
- Iterator it=circulos.keySet().iterator();
- while (it.hasNext()){
- Integer clave=(Integer) it.next();
- Integer c=circulos.get(clave);
- oos.writeObject(c);
- }
- oos.close();
- }
- panDibujo(){
- //añadimos tamaño y color de fondo
- this.setPreferredSize(new Dimension(500,500));
- this.setBackground(Color.BLACK);
- //al estar dentro de un BORDER Layout setSize no lo coge , por eso hemos de
- //utilizar setPreferedSize
- //siempre sera un metrodo
- circulos=new HashMap<Point,Integer>();
- this.setVisible(true);
- }
- void dibujaCirculo(int x , int y , int radio) {
- //crea el punto y crea el entero
- Point p=new Point (x,y);
- Integer r=new Integer(radio);
- //meter clave y valor
- //si ese punto contiene la clave p
- if (circulos.containsKey(p)){ //if circulos.getP !=null SHOWMESSAGEDIALOG("YA EXISTIA");
- JOptionPane.showMessageDialog(null, "Ya hay circulo en esa posicion");
- }
- else {
- circulos.put(p, r);
- JOptionPane.showMessageDialog(null,circulos.size()+"Ahi circulo");
- //vulve a pintar en el JPanel
- repaint();
- }
- }
- //mettodo que se va a sobreescribir cada vez que eejecutamos un componente
- protected void paintComponent(Graphics g){
- super.paintComponent(g);
- //recuperamos el conjunto de claves y hlo recorremos por un iterator
- Iterator it= circulos.keySet().iterator();
- Point p;
- Integer r;
- g.setColor(Color.WHITE);
- while (it.hasNext()){
- //cada iter devolvera una clave de tipo point
- p=(Point) it.next();
- //recuperamos el valor a partir de clave,sacando el radio
- r= circulos.get(p);
- //metodo para escribir circulos ,los 2 ultimos argumentos son el ancho y el alto
- g.drawOval(p.x, p.y, r.intValue()*2, r.intValue()*2);
- //guarar que gner el objeto hasmapr y lo redibuje
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment