Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I have a problem with my code of a simple game

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have a problem with my code of a simple game

    Hello!

    I try to do a image in a frame to advance a few pixels to click the image

    The problem comes to add the image in a label and I try to call the label in a method addMouseListener() and after Mouseclicked()

    The code in which I do it itīs it:

     
    /************* This is the part af code where I have the request    *////////////////////////////////////
     
                 Ejemplo.getContentPane().add(etiqueta, BorderLayout.CENTER); 
    /* 109 */    
                  etiqueta.addMouseListener(new MouseAdapter()
    /*     */     {
    /*     */      public void mouseClicked(MouseEvent paramMouseEvent)
    /*     */      {
    /* 113 */        System.out.println("pepe");
    /* 114 */        Ejemplo.this.sw2 = 1;
    /*     */       }
    /*     */     });
     
    /************* This is the part af code where I have the request    *////////////////////////////////////

    etiqueta is a label and adds the label is the image.

    This code is in the constructor, in Ejemplo(){}.

    in the main class of the java program, in "public class Ejemplo extends JPanel
    /* */ implements KeyListener, Runnable
    /* */ {"
    I put the code of the label, it is:

    static ImageIcon imagen1 = new ImageIcon("rectangulovacio.jpg");
    static JLabel etiqueta = new JLabel(imagen1);

    and the program work, I created the file "Ejemplo.class" but not work the method "MouseClicked" because I click in the image and not happen anything... I can't do the program so? Thanks


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: I have a problem with my code of a simple game

    So, from what you're saying, you're not getting any error messages. Could you try to re-explain yourself, I can see that english isn't your first language. I also wouldn't mind see a little bit more code.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with my code of a simple game

    I don't receive error messages but the program doesn`t do that I want.

    I want when the image is clicked it must be advance a pixels inside the frame but the image don't do anything when I click in. It only shows inside the frame, in the center

    the complete code of the program is it:

     
     
    /*     */ import java.awt.Container;
    /*     */ import java.awt.Graphics;
    /*     */ import java.awt.event.KeyEvent;
    /*     */ import java.awt.event.KeyListener;
    /*     */ import java.awt.event.MouseAdapter;
    /*     */ import java.awt.event.MouseEvent;
    /*     */ import java.awt.event.WindowAdapter;
    /*     */ import java.awt.event.WindowEvent;
    /*     */ import java.io.PrintStream;
    /*     */ import java.util.Date;
    /*     */ import java.util.Random;
    /*     */ import javax.swing.ImageIcon;
    /*     */ import javax.swing.JFrame;
    /*     */ import javax.swing.JLabel;
    /*     */ import javax.swing.JPanel;
    /*     */ public class Jojo extends Applet{
    /*     */ public class Ejemplo extends JPanel
    /*     */   implements KeyListener, Runnable
    /*     */ {
    /*     */   Thread anima;
    /*     */   private static final long serialVersionUID = 1L;
    /*     */   private int x;
    /*     */   private int y;
    /*     */   public int x1;
    /*     */   public int y1;
    /*     */   public int x2;
    /*     */   public int y2;
    /*     */   public int y3;
    /*     */   public int x3;
    /*     */   public int sw;
    /*  35 */   public int sw2 = 0;
    /*     */   public int azar;
    /*  37 */   public int u = 0;
    /*     */  Container localEjemplo;
    //Container contenedor = getContentPane();
     
    /*  40 */   static ImageIcon imagen1 = new ImageIcon("rectangulovacio.jpg");
    /*  41 */   static ImageIcon imagen2 = new ImageIcon("rectangulolleno.jpg");
    /*     */ 
    /*  43 */   //static JPanel panel1 = new JPanel();
    /*  44 */    static JLabel etiqueta = new JLabel(imagen1);
    /*     */  
     
     
    /*     */   public void start()
    /*     */   {
    /*  51 */     if (this.anima == null) {
    /*  52 */       this.anima = new Thread(this);
    /*  53 */       this.anima.start();
    /*     */     }
     
    /*     */   }
    /*     */ 
    /*     */   public void run()
    /*     */   {
    /*     */     while (true)
    /*  60 */       mover();
    /*     */   }
    /*     */ 
    /*     */   private static void log(String paramString) {
    /*  64 */     System.out.println("Deja un hueco en el medio y ve que pasa");
    /*  65 */     System.out.println(paramString);
    /*     */   }
    /*     */ 
    /*     */   public static void main(String[] paramArrayOfString)
    /*     */   {
                   //panel1.setLayout(new BorderLayout());
                  //panel1.add(etiqueta);
                   // /* esto vale */ panel1.add(etiqueta, "Center");
                   //panel1.setVisible(true);
    /*  76 */      //Ejemplo localEjemplo = new Ejemplo();
                   //localEjemplo.getContentPane();
     
     
    //localEjemplo.show(); 
    //localEjemplo.setVisible(true);
    //localEjemplo.pack();
    /*     */ 
    /*  78 */     
     
     
     
     
    /*  79 */     
    /*     */ 
    /*  81 */     //
    /*     */ 
    /*  86 */     
    /*     */ 
       //
    /*     */ 
    /*  93 */     System.out.println("Directorio ejecucion = " + System.getProperty("user.dir"));
    /*     */   }
    /*     */ 
    /*     */   Ejemplo()
    /*     */   {
    /*  98 */     //super("Ejemplo");
    /*  99 */     setSize(1000, 500);
    /*     */     //this.addActionListener(this);
     
     
    /************* This is the part af code where I have the request    *////////////////////////////////////
     
                 Ejemplo.getContentPane().add(etiqueta, BorderLayout.CENTER); 
    /* 109 */    
                  etiqueta.addMouseListener(new MouseAdapter()
    /*     */     {
    /*     */      public void mouseClicked(MouseEvent paramMouseEvent)
    /*     */      {
    /* 113 */        System.out.println("pepe");
    /* 114 */        Ejemplo.this.sw2 = 1;
    /*     */       }
    /*     */     });
     
    /************* This is the part af code where I have the request    *////////////////////////////////////              
     
      //
     
    /* 118 */     this.sw = 0;
    /* 119 */     Random localRandom = new Random();
    /* 120 */     localRandom.setSeed(new Date().getTime());
    /* 121 */     this.azar = localRandom.nextInt(3);
    /*     */ 
    /* 123 */     if (this.azar == 0) {
    /* 124 */       this.x = 50;
    /* 125 */       this.y = 100;
    /* 126 */       this.x1 = (this.x + 260);
    /* 127 */       this.y1 = this.y;
    /* 128 */       this.x2 = 150;
    /* 129 */       this.y2 = 250;
    /* 130 */       this.x3 = (this.x2 + 150);
    /* 131 */       this.y3 = (this.y + 150);
    /* 132 */       log("0 ");
    /*     */     }
    /* 134 */     if (this.azar == 1) {
    /* 135 */       this.x = 155;
    /* 136 */       this.y = 200;
    /* 137 */       this.x1 = (this.x + 155);
    /* 138 */       this.y1 = this.y;
    /* 139 */       this.x2 = 50;
    /* 140 */       this.y2 = 355;
    /* 141 */       this.x3 = (this.x + 155);
    /* 142 */       this.y3 = (this.y + 155);
    /* 143 */       log("1 ");
    /*     */     }
    /* 145 */     if (this.azar == 2) {
    /* 146 */       this.x = 50;
    /* 147 */       this.y = 200;
    /* 148 */       this.x1 = (this.x + 155);
    /* 149 */       this.y1 = this.y;
    /* 150 */       this.x2 = 50;
    /* 151 */       this.y2 = 355;
    /* 152 */       this.x3 = (this.x + 155);
    /* 153 */       this.y3 = (this.y + 155);
    /* 154 */       log("2");
    /*     */     }
    /*     */ 
    /* 157 */     addKeyListener(this);
    /* 158 */   // addWindowListener(new WindowAdapter()
    /*     */     //{
    /*     */       //public void windowClosing(WindowEvent paramWindowEvent)
    /*     */       //{
    /* 162 */       //  System.exit(0);
    /*     */       //}
    /*     */     //});*/
    /*     */   }
    /*     */ 
    /*     */   public void keyPressed(KeyEvent paramKeyEvent)
    /*     */   {
    /* 169 */     int i = 1;
    /* 170 */     int j = 1;
    /* 171 */     if (this.sw == 0)
    /*     */     {
    /* 173 */       switch (paramKeyEvent.getKeyCode())
    /*     */       {
    /*     */       case 38:
    /* 176 */         this.y -= 1;
    /* 177 */         break;
    /*     */       case 40:
    /* 179 */         this.y += 1;
    /* 180 */         break;
    /*     */       case 37:
    /* 182 */         this.x -= 1;
    /* 183 */         break;
    /*     */       case 39:
    /* 185 */         this.x += 1;
    /* 186 */         break;
    /*     */       default:
    /* 188 */         i = 0;
    /*     */       }
    /* 190 */       if (i != 0) {
    /* 191 */         repaint();
    /*     */       }
    /* 193 */       if (this.x + 100 == this.x1) {
    /* 194 */         this.sw = 1;
    /*     */       }
    /*     */     }
    /*     */ 
    /* 198 */     if (this.sw == 1) {
    /* 199 */       switch (paramKeyEvent.getKeyCode())
    /*     */       {
    /*     */       case 38:
    /* 202 */         this.y1 -= 1;
    /* 203 */         break;
    /*     */       case 40:
    /* 205 */         this.y1 += 1;
    /* 206 */         break;
    /*     */       case 37:
    /* 208 */         this.x1 -= 1;
    /* 209 */         break;
    /*     */       case 39:
    /* 211 */         this.x1 += 1;
    /* 212 */         break;
    /*     */       default:
    /* 214 */         j = 0;
    /*     */       }
    /* 216 */       if (j != 0)
    /* 217 */         repaint();
    /*     */     }
    /*     */   }
    /*     */ 
    /*     */   public void mover()
    /*     */   {
    /*     */   }
    /*     */ 
    /*     */   public void paint(Graphics paramGraphics)
    /*     */   {
    /* 229 */     if (imagen1 == null) {
    /* 230 */       return;
    /*     */     }
    /*     */ 
    /* 233 */     paramGraphics.drawImage(imagen1.getImage(), this.x, this.y, imagen1.getImageObserver());
    /*     */ 
    /* 238 */     repaint();
    /* 239 */     if (this.sw2 != 1) {
    /* 240 */       return;
    /*     */     }
    /*     */     if (this.sw2==1){
    /* 243 */     this.u = 0;
    /* 244 */     while (this.u != 40)
    /*     */     {
    /* 247 */       this.x -= 1;
    /* 248 */       this.u += 1;
    /*     */     }
    /* 250 */     this.sw2 = 0;
    /* 251 */     paramGraphics.drawImage(imagen2.getImage(), this.x, this.y, imagen2.getImageObserver());
    /* 252 */     //repaint();
                  }
    /*     */   }
    /*     */ 
    /*     */   public void keyReleased(KeyEvent paramKeyEvent)
    /*     */   {
    /*     */   }
    /*     */ 
    /*     */   public void keyTyped(KeyEvent paramKeyEvent)
    /*     */   {
    /*     */   }
    /*     */ }
     
    /* Location:           C:\Cosas Internet\ejemplos java-javascript\
     * Qualified Name:     Ejemplo
     * JD-Core Version:    0.5.3
     */
    }

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with my code of a simple game

    Quote Originally Posted by byrne View Post
    I don't receive error messages but the program doesn`t do that I want.

    I want when the image is clicked it must be advance a pixels inside the frame but the image don't do anything when I click in. It only shows inside the frame, in the center

    the complete code of the program is it:

     
     
    /*     */ import java.awt.Container;
    /*     */ import java.awt.Graphics;
    /*     */ import java.awt.event.KeyEvent;
    /*     */ import java.awt.event.KeyListener;
    /*     */ import java.awt.event.MouseAdapter;
    /*     */ import java.awt.event.MouseEvent;
    /*     */ import java.awt.event.WindowAdapter;
    /*     */ import java.awt.event.WindowEvent;
    /*     */ import java.io.PrintStream;
    /*     */ import java.util.Date;
    /*     */ import java.util.Random;
    /*     */ import javax.swing.ImageIcon;
    /*     */ import javax.swing.JFrame;
    /*     */ import javax.swing.JLabel;
    /*     */ import javax.swing.JPanel;
    /*     */ public class Jojo extends Applet{
    /*     */ public class Ejemplo extends JPanel
    /*     */   implements KeyListener, Runnable
    /*     */ {
    /*     */   Thread anima;
    /*     */   private static final long serialVersionUID = 1L;
    /*     */   private int x;
    /*     */   private int y;
    /*     */   public int x1;
    /*     */   public int y1;
    /*     */   public int x2;
    /*     */   public int y2;
    /*     */   public int y3;
    /*     */   public int x3;
    /*     */   public int sw;
    /*  35 */   public int sw2 = 0;
    /*     */   public int azar;
    /*  37 */   public int u = 0;
    /*     */  Container localEjemplo;
    //Container contenedor = getContentPane();
     
    /*  40 */   static ImageIcon imagen1 = new ImageIcon("rectangulovacio.jpg");
    /*  41 */   static ImageIcon imagen2 = new ImageIcon("rectangulolleno.jpg");
    /*     */ 
    /*  43 */   //static JPanel panel1 = new JPanel();
    /*  44 */    static JLabel etiqueta = new JLabel(imagen1);
    /*     */  
     
     
    /*     */   public void start()
    /*     */   {
    /*  51 */     if (this.anima == null) {
    /*  52 */       this.anima = new Thread(this);
    /*  53 */       this.anima.start();
    /*     */     }
     
    /*     */   }
    /*     */ 
    /*     */   public void run()
    /*     */   {
    /*     */     while (true)
    /*  60 */       mover();
    /*     */   }
    /*     */ 
    /*     */   private static void log(String paramString) {
    /*  64 */     System.out.println("Deja un hueco en el medio y ve que pasa");
    /*  65 */     System.out.println(paramString);
    /*     */   }
    /*     */ 
    /*     */   public static void main(String[] paramArrayOfString)
    /*     */   {
                   //panel1.setLayout(new BorderLayout());
                  //panel1.add(etiqueta);
                   // /* esto vale */ panel1.add(etiqueta, "Center");
                   //panel1.setVisible(true);
    /*  76 */      //Ejemplo localEjemplo = new Ejemplo();
                   //localEjemplo.getContentPane();
     
     
    //localEjemplo.show(); 
    //localEjemplo.setVisible(true);
    //localEjemplo.pack();
    /*     */ 
    /*  78 */     
     
     
     
     
    /*  79 */     
    /*     */ 
    /*  81 */     //
    /*     */ 
    /*  86 */     
    /*     */ 
       //
    /*     */ 
    /*  93 */     System.out.println("Directorio ejecucion = " + System.getProperty("user.dir"));
    /*     */   }
    /*     */ 
    /*     */   Ejemplo()
    /*     */   {
    /*  98 */     //super("Ejemplo");
    /*  99 */     setSize(1000, 500);
    /*     */     //this.addActionListener(this);
     
     
    /************* This is the part af code where I have the request    *////////////////////////////////////
     
                 Ejemplo.getContentPane().add(etiqueta, BorderLayout.CENTER); 
    /* 109 */    
                  etiqueta.addMouseListener(new MouseAdapter()
    /*     */     {
    /*     */      public void mouseClicked(MouseEvent paramMouseEvent)
    /*     */      {
    /* 113 */        System.out.println("pepe");
    /* 114 */        Ejemplo.this.sw2 = 1;
    /*     */       }
    /*     */     });
     
    /************* This is the part af code where I have the request    *////////////////////////////////////              
     
      //
     
    /* 118 */     this.sw = 0;
    /* 119 */     Random localRandom = new Random();
    /* 120 */     localRandom.setSeed(new Date().getTime());
    /* 121 */     this.azar = localRandom.nextInt(3);
    /*     */ 
    /* 123 */     if (this.azar == 0) {
    /* 124 */       this.x = 50;
    /* 125 */       this.y = 100;
    /* 126 */       this.x1 = (this.x + 260);
    /* 127 */       this.y1 = this.y;
    /* 128 */       this.x2 = 150;
    /* 129 */       this.y2 = 250;
    /* 130 */       this.x3 = (this.x2 + 150);
    /* 131 */       this.y3 = (this.y + 150);
    /* 132 */       log("0 ");
    /*     */     }
    /* 134 */     if (this.azar == 1) {
    /* 135 */       this.x = 155;
    /* 136 */       this.y = 200;
    /* 137 */       this.x1 = (this.x + 155);
    /* 138 */       this.y1 = this.y;
    /* 139 */       this.x2 = 50;
    /* 140 */       this.y2 = 355;
    /* 141 */       this.x3 = (this.x + 155);
    /* 142 */       this.y3 = (this.y + 155);
    /* 143 */       log("1 ");
    /*     */     }
    /* 145 */     if (this.azar == 2) {
    /* 146 */       this.x = 50;
    /* 147 */       this.y = 200;
    /* 148 */       this.x1 = (this.x + 155);
    /* 149 */       this.y1 = this.y;
    /* 150 */       this.x2 = 50;
    /* 151 */       this.y2 = 355;
    /* 152 */       this.x3 = (this.x + 155);
    /* 153 */       this.y3 = (this.y + 155);
    /* 154 */       log("2");
    /*     */     }
    /*     */ 
    /* 157 */     addKeyListener(this);
    /* 158 */   // addWindowListener(new WindowAdapter()
    /*     */     //{
    /*     */       //public void windowClosing(WindowEvent paramWindowEvent)
    /*     */       //{
    /* 162 */       //  System.exit(0);
    /*     */       //}
    /*     */     //});*/
    /*     */   }
    /*     */ 
    /*     */   public void keyPressed(KeyEvent paramKeyEvent)
    /*     */   {
    /* 169 */     int i = 1;
    /* 170 */     int j = 1;
    /* 171 */     if (this.sw == 0)
    /*     */     {
    /* 173 */       switch (paramKeyEvent.getKeyCode())
    /*     */       {
    /*     */       case 38:
    /* 176 */         this.y -= 1;
    /* 177 */         break;
    /*     */       case 40:
    /* 179 */         this.y += 1;
    /* 180 */         break;
    /*     */       case 37:
    /* 182 */         this.x -= 1;
    /* 183 */         break;
    /*     */       case 39:
    /* 185 */         this.x += 1;
    /* 186 */         break;
    /*     */       default:
    /* 188 */         i = 0;
    /*     */       }
    /* 190 */       if (i != 0) {
    /* 191 */         repaint();
    /*     */       }
    /* 193 */       if (this.x + 100 == this.x1) {
    /* 194 */         this.sw = 1;
    /*     */       }
    /*     */     }
    /*     */ 
    /* 198 */     if (this.sw == 1) {
    /* 199 */       switch (paramKeyEvent.getKeyCode())
    /*     */       {
    /*     */       case 38:
    /* 202 */         this.y1 -= 1;
    /* 203 */         break;
    /*     */       case 40:
    /* 205 */         this.y1 += 1;
    /* 206 */         break;
    /*     */       case 37:
    /* 208 */         this.x1 -= 1;
    /* 209 */         break;
    /*     */       case 39:
    /* 211 */         this.x1 += 1;
    /* 212 */         break;
    /*     */       default:
    /* 214 */         j = 0;
    /*     */       }
    /* 216 */       if (j != 0)
    /* 217 */         repaint();
    /*     */     }
    /*     */   }
    /*     */ 
    /*     */   public void mover()
    /*     */   {
    /*     */   }
    /*     */ 
    /*     */   public void paint(Graphics paramGraphics)
    /*     */   {
    /* 229 */     if (imagen1 == null) {
    /* 230 */       return;
    /*     */     }
    /*     */ 
    /* 233 */     paramGraphics.drawImage(imagen1.getImage(), this.x, this.y, imagen1.getImageObserver());
    /*     */ 
    /* 238 */     repaint();
    /* 239 */     if (this.sw2 != 1) {
    /* 240 */       return;
    /*     */     }
    /*     */     if (this.sw2==1){
    /* 243 */     this.u = 0;
    /* 244 */     while (this.u != 40)
    /*     */     {
    /* 247 */       this.x -= 1;
    /* 248 */       this.u += 1;
    /*     */     }
    /* 250 */     this.sw2 = 0;
    /* 251 */     paramGraphics.drawImage(imagen2.getImage(), this.x, this.y, imagen2.getImageObserver());
    /* 252 */     //repaint();
                  }
    /*     */   }
    /*     */ 
    /*     */   public void keyReleased(KeyEvent paramKeyEvent)
    /*     */   {
    /*     */   }
    /*     */ 
    /*     */   public void keyTyped(KeyEvent paramKeyEvent)
    /*     */   {
    /*     */   }
    /*     */ }
     
    /* Location:           C:\Cosas Internet\ejemplos java-javascript\
     * Qualified Name:     Ejemplo
     * JD-Core Version:    0.5.3
     */
    }
    Can Anyone help me to resolve my problem with my code? Thanks

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I have a problem with my code of a simple game

    You're more likely to get help if you boil your problem down to an SSCCE that demonstrates exactly what you're talking about.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  2. Simple game problem
    By frozen java in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 30th, 2011, 09:14 PM
  3. Programming AI for simple game?
    By YouGoLast in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2011, 08:53 AM
  4. simple game with swing and awt problem
    By Pulse_Irl in forum AWT / Java Swing
    Replies: 2
    Last Post: October 12th, 2010, 02:04 PM
  5. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM