Foros del Web » Programación para mayores de 30 ;) » Java »

Activar Objeto al hacer click

Estas en el tema de Activar Objeto al hacer click en el foro de Java en Foros del Web. muy buenas tardes Foreros, estoy empezando a usar JAVA para un materia y nos pidieron desarrollar un juego sencillo. he estado intentando hacer el clásico ...
  #1 (permalink)  
Antiguo 01/11/2011, 12:59
 
Fecha de Ingreso: mayo-2008
Mensajes: 489
Antigüedad: 16 años
Puntos: 8
Pregunta Activar Objeto al hacer click

muy buenas tardes Foreros, estoy empezando a usar JAVA para un materia y nos pidieron desarrollar un juego sencillo. he estado intentando hacer el clásico "cuadritos" o "puntos y cuadrados", el juego donde a travès de lineas se armas cuadrados y gana quien màs halla formado.

el programa està compuesto actualmente por los mètodos "crearTablero", "clicLinea", "paint" el problema està en que yo trabajo las lìneas como un todo al intentar tomar las coordenadas de donde se hace clic, solo puedo calcular las lìneas verticales u horizontales, pero no ambas a la vez.

me dijeron que tomara cada lìnea como objeto y que el objeto se activara al darle click sobre èl, pero no sè como hacerlo.

alguien me podrìa orientar con ello?

acà el còdigo hasta ahora:

Código java:
Ver original
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package paqGrilla;
  6.  
  7. import java.awt.Color;
  8. import java.awt.Font;
  9. import java.awt.Graphics;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import java.awt.event.WindowEvent;
  13. import java.awt.event.WindowListener;
  14. import javax.swing.JFrame;
  15.  
  16. public class Grilla extends JFrame {
  17.     public int celdas = 10;
  18.     private int TAM = celdas + 1; // tamaño de la tabla
  19.     private int LARGO = 40; // Largo de la línea
  20.     private int lineaVisible[][][][] = new int[TAM][TAM][TAM][TAM]; // 0 oculto; 1 visible
  21.     private int celda[][] = new int[TAM][TAM];
  22.     private int estado = 0; // 0 inicio; 1 final
  23.     private int lineasVistas = 0;
  24.    
  25.     public Grilla() {
  26.        
  27.         setTitle("Mi Grilla");
  28.         setSize(440,460);
  29.         setVisible(true);
  30.         setResizable(true);
  31.  
  32.         crearTablero();
  33.        
  34.         addMouseListener(new MouseListener(){          
  35.             public void mouseEntered(MouseEvent me) {}            
  36.             public void mouseExited(MouseEvent me) {}
  37.             public void mouseClicked(MouseEvent me) {}
  38.             public void mousePressed(MouseEvent me) {}
  39.             public void mouseReleased(MouseEvent me) {
  40.                 if (estado==0) {
  41.                     int x = me.getXOnScreen();
  42.                     int y = me.getYOnScreen();
  43.                     int x1 = x/40;
  44.                     int y1 = (y-40)/40;
  45.                     int x2 = x/40;
  46.                     int y2 = y/40;
  47.                     clicLinea(x1,y1,x2,y2);
  48.                    
  49.                 }
  50.             }
  51.         });
  52.        
  53.         addWindowListener(new WindowListener() {
  54.             public void windowClosing(WindowEvent we) {
  55.                 System.exit(0);
  56.             }
  57.             public void windowOpened(WindowEvent we) {}
  58.             public void windowClosed(WindowEvent we) {}
  59.             public void windowIconified(WindowEvent we) {}
  60.             public void windowDeiconified(WindowEvent we) {}
  61.             public void windowActivated(WindowEvent we) {}
  62.             public void windowDeactivated(WindowEvent we) {}
  63.         });
  64.     }
  65.    
  66.     public void clicLinea (int x1, int y1, int x2, int y2) {
  67.         if (lineaVisible[x1][y1][x2][y2]==0) {
  68.             lineaVisible[x1][y1][x2][y2]=1;
  69.             lineasVistas++;
  70.             repaint();
  71.         }
  72.     }
  73.    
  74.     public void crearTablero(){
  75.         for (int p1=0; p1<TAM; p1++) {
  76.             for (int p2=0; p2<TAM; p2++) {
  77.                 int x1 = p1;
  78.                 int y1 = p2;
  79.                 int x2 = p1;
  80.                 int y2 = p2+1;
  81.                
  82.                 if (y2<TAM) {
  83.                     for (int p3=0; p3<2; p3++) {
  84.                         if (p3==0) { // Vertical
  85.                             lineaVisible[x1][y1][x2][y2] = 0;
  86.                         } else { // Horizontal
  87.                             lineaVisible[y1][x1][y2][x2] = 0;
  88.                         }
  89.                     }
  90.                    
  91.                     celda[x1][y1]=0;
  92.                 }
  93.             }
  94.         }
  95.        
  96.         estado = 0;
  97.         lineasVistas = 0;
  98.     }
  99.  
  100.     public void repaint(Graphics g) {
  101.         paint(g);
  102.     }
  103.    
  104.     public void paint(Graphics g) {
  105.         g.setFont(new Font("Arial",Font.BOLD,10));
  106.         g.clearRect(0, 0, getWidth(), getHeight());
  107.         for (int p1=0; p1<TAM; p1++) {
  108.             for (int p2=0; p2<TAM; p2++) {
  109.                 int x1 = p1*40;
  110.                 int y1 = p2*40;
  111.                 int x2 = p1*40;
  112.                 int y2 = (p2+1)*40;
  113.                
  114.                 if (y2<TAM*40) {
  115.                     for (int p3=0; p3<2; p3++) {
  116.                         if (p3==0) { // Vertical
  117.                             if (lineaVisible[x1/40][y1/40][x2/40][y2/40]==1) {
  118.                                 g.setColor(Color.blue);
  119.                             } else {
  120.                                 g.setColor(Color.LIGHT_GRAY);
  121.                             }
  122.                             g.drawLine(x1+20, y1+40, x2+20, y2+40);
  123.                         } else { // Horizontal
  124.                             if (lineaVisible[y1/40][x1/40][y2/40][x2/40]==1) {
  125.                                 g.setColor(Color.blue);
  126.                             } else {
  127.                                 g.setColor(Color.LIGHT_GRAY);
  128.                             }
  129.                             g.drawLine(y1+20, x1+40, y2+20, x2+40);
  130.                         }
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.     }
  136.     /**
  137.      * @param args the command line arguments
  138.      */
  139.     public static void main(String[] args) {
  140.         new Grilla();
  141.     }
  142. }
__________________
AppLab - Laboratorio de Ideas

Etiquetas: objeto, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:00.