I am new learning repaint g programing methos, i am trying to make a simple Rock paper sccissors with grahpics an animations. i am using netbeans
Context: 2 java clases: PPT.java wich extend jpanel and a netbeans JFrame Form. i added the PPT as a bean on the JFRame and it render nice the start animation (slide down and resize the images of rockpaper...) After the animation i try diferents events like click on the bean and recall the PPT.motor method who is the one who Reposition the things and call reprint and sleep for make the animation.
After clicking and triggering the event, the execution runs smooth just before call method motor (almost last lane). But when the programs have to go to motor by 2nd time it stay there for a while with no errors, not printing nothing in the windows, if you click more times it print images small then enormous with no animation.
I am almost sure that my problems come from not handling nice the Threads but who knows.
this is PPT.java
package ppt;
import com.sun.org.apache.bcel.internal.classfile.Constant;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class PPT extends JPanel{
//Variables de Imagen buffered para prevenir FLIKR
private BufferedImage piedra;
private BufferedImage tijera;
private BufferedImage papel;
//Game Variables
private int cpu;//CPU choice
private int j1;//J1 choice
private int totalCPU;//score
private int totalJ1;//score
private int ganaRonda;//0 ties, 1 j1 win -1 cpu win
//
private static GUI ventana;//Declaramos asi la variable ventana para poder acceder a sus Variables fuera del main.
//piedra= rock papel=paper XY=cordinate var TXTY= SizeVar
private int piedraX=240, piedraXCPU;//Cordenada X para renderizar la imagen
private int piedraY=280, piedraYCPU;//Coordenada Y para renderizar la imagen
private int piedraTX=242;//Tamaño de salida de la imagen en X 900
private int piedraTY=242;//Tanaño de salida de la imagen en Y 645
private int papelX=444, papelXCPU=-100;//Cordenada X para renderizar la imagen
private int papelY=248, papelYCPU=-100;//Coordenada Y para renderizar la imagen
private int papelTX=300;//Tamaño de salida de la imagen en X 600
private int papelTY=300;//Tanaño de salida de la imagen en Y 376
private int tijeraX=280, tijeraXCPU=-100;//Cordenada X para renderizar la imagen
private int tijeraY=240, tijeraYCPU=-100;//Coordenada Y para renderizar la imagen
private int tijeraTX=400;//Tamaño de salida de la imagen en X 210
private int tijeraTY=400;//Tanaño de salida de la imagen en Y 400
private Color color1=Color.RED;//Color de la caja izq 1
private Color color2=Color.BLUE;//Color de la caja izq 2
public static void main(String[] args) {
// TODO code application logic here
//INSTANCE OF JFRAMEFORM
ventana =new GUI("Piedra Papel Tijera");
ventana.setLocationRelativeTo(null);//Centra la ventana en el media
ventana.setVisible(true);//Hace visible la ventana
ventana.pPT1.motor(4);//pPT1 IS THIS CLASS INSTANCED FROM JFRAME AS BEAN
}
public PPT (){
//CONSTRUCTOR getting images
try {
this.piedra = ImageIO.read(getClass().getResource("piedra.png"));
this.papel = ImageIO.read(getClass().getResource("papel.png"));
this.tijera = ImageIO.read(getClass().getResource("tijeras.png"));
} catch (IOException ex) {
Logger.getLogger(PPT.class.getName()).log(Level.SEVERE, null, ex);
}
this.setSize(800, 700);//Establece el tamaño del jpanel a 800x700
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//THIS draw the Scores at the left of the screen
g.setFont(new Font("Arial",Font.BOLD,42));//establece la fuente
g.setColor(color1);//ColorRojo
g.fillRect(1, 350, 42, 84);//Dibuja el rectangulo Del Jugador
g.setColor(color2);//ColorAzul
g.fillRect(1, 264, 42, 84);//dibuja el rectangulo azul
g.drawString(Integer.toString(totalJ1) , 4, 420);//dibuja la puntuacion del J1 en azul
g.setColor(color1);//ColorRojo
g.drawString(Integer.toString(totalCPU) , 4, 313);//dibuja la puntuacion de la CPU en rojo
/THIS draw the images
g.drawImage(piedra, piedraX, piedraY,piedraTX,piedraTY,null);//pintamos la Image de la variable en la Posicion XY y con el tamaño TX TY
g.drawImage(papel, papelX, papelY,papelTX,papelTY,null);
g.drawImage(tijera, tijeraX, tijeraY,tijeraTX,tijeraTY,null);
}
public void cpuTira (){
//THIS generate RANDOM choose to cpu
cpu=(int) (Math.random() * (4 - 1)) + 1;
}
//This is the GameLoopHandler
public void motor (int num){
if (num==4){
//THIS RESET THE ORIGINAL VALUES OF vars FOR START AGAIN THE ANIMATION OF THE FIRST STAGE
piedraX=240;//Cordenada X para renderizar la imagen
piedraY=280;//Coordenada Y para renderizar la imagen
piedraTX=242;//Tamaño de salida de la imagen en X 900
piedraTY=242;//Tanaño de salida de la imagen en Y 645
papelX=444;//Cordenada X para renderizar la imagen
papelY=248;//Coordenada Y para renderizar la imagen
papelTX=300;//Tamaño de salida de la imagen en X 600
papelTY=300;//Tanaño de salida de la imagen en Y 376
tijeraX=280;//Cordenada X para renderizar la imagen
tijeraY=240;//Coordenada Y para renderizar la imagen
tijeraTX=400;//Tamaño de salida de la imagen en X 210
tijeraTY=400;//Tanaño de salida de la imagen en Y 400
//THIS MOVE THE IMAGES down until near the bottom and resize them
while (piedraY<500){
if (piedraY>400){
piedraTX--;
piedraTY--;
papelTX--;
papelTY--;
tijeraTX-=2;
tijeraTY-=2;
}
if(tijeraY>359)
tijeraX++;
if(papelY>385)
papelX++;
piedraY++;
tijeraY++;
papelY++;
repaint();
try {//CUANTO MAS TIEMPO DE SLEEP MAS LENTA VA LA ANIMACION (FPS)
Thread.sleep(18);
} catch (InterruptedException ex) {
Logger.getLogger(PPT.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
//THIS METHOD IS called by the event in JFRAME FORM
public void controlJuego(int jugador1){
j1=jugador1;//set the choice of j1
cpuTira();//set the choice of CPU
//Calculate win 1 rock, 2paper,3 scissors
switch(j1){
case 1:
if(cpu==1)
ganaRonda=0;
if(cpu==2)
ganaRonda=-1;
if(cpu==3)
ganaRonda=1;
break;
case 2:
if(cpu==1)
ganaRonda=1;
if(cpu==2)
ganaRonda=0;
if(cpu==3)
ganaRonda=-1;
break;
case 3:
if(cpu==1)
ganaRonda=-1;
if(cpu==2)
ganaRonda=1;
if(cpu==3)
ganaRonda=0;
}
//Animation of "Fight" Currently i just want to repeat the start animation
System.out.println("hellow");//it printed hellow quicly
motor(4);//4 = intro animation
System.out.println(Integer.toString(ganaRonda));//it printed the variable after half minut and dindt redender nothing.
}
}
The JFrame Form have the default netbeans code, and the bean deployed. So the thread mess can come cause i call the eventClick in JFrame and then explode.
Thanks for helping, sorry about the wallcode i just want to understand the thing.