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:
Code :
/************* 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:
Code :
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
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.
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:
Code :
/* */ 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
*/
}
Re: I have a problem with my code of a simple game
Quote:
Originally Posted by
byrne
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:
Code :
/* */ 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
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.