Acerca de cargar Java con Canvas. . ¿Por qué no puedo cargar ayuda para principiantes?
¿Importado? java.awt.*;
¿Importar? Java.applet.*;
¿Importar? Java.awt.event.*;
¿Público? ¿clase? ¿Rompecabezas? ¿extender? Miniprograma{
¿Imagen? imgPuzzle, ¿punto de mejora? Quince = ¿nuevo? Punto(3,3);
int[][]? mapa={{0, 4, 8, 12}, {1, 5, 9, 3}, {2, 6, 10, 14}, {3, 7, 11, 15 } };
int? sx, sy;
¿Lienzo? Pantalla;
¿Gráficos? gs,GB;
¿Booleano? corriendo=false;
¿Botón? bInicio=nuevo? Botón("Nuevo juego");
¿Botón? bVer=¿nuevo? button("Mostrar imagen correcta");
¿Público? ¿Vacío? init(){
preparar imagen(); //Preparar imagen
sx = img rompecabezas . .get height(this)/4;
establecer fondo(color . azul);
initScreen();? //Pantalla de inicialización
botones de inicio(); //Botón de inicialización
Agregar (pantalla); //Agregar componentes al Applet
Agregar (bInicio);
Agregar(bVer);
}
¿No es válido? PrepareImage(){//Método para preparar imágenes
imgPuzzle=getImage(getCodeBase(), "temp/XXX.jpg));
MediaTracker?mt=new?MediaTracker(this );
mt.addImage(imgPuzzle,? 0); //Espera a que se carguen todas las imágenes
Prueba {
mt.waitforall(). ;
}catch(Exception?e){}
//Crea un búfer y obtiene su objeto Graphics.
buff = crear imagen(imgpuzle. obtener ancho(este), imgpuzle.obtener altura(esto));
GB = obtener gráficos(); p>}
¿No es válido? mapa init(){? //Inicializar el mapa
java.util.Random? rnd=¿nuevo? Java .util .aleatorio();
int? temp, x1, y1, x2, y2;
for(int?I = 0;ilt100;i){
x 1 = rnd . > p>
x2 = rnd .siguienteint(4);
y 1 = rndint(4);
y2 = rndint(4); p >
temp = mapa[x 1][y 1];
mapa[x 1][y 1]= mapa[x2][y2];
mapa [ x2][y2]= temp;
}
exterior: for(int?j = 0;j lt4;j)
for(int? I = 0;ilt4;i)
if(map[i][j]==15){
quinto establecer ubicación(I, j);
¿Romper? Externo;
}
}
¿No válido? PantallaInit(){? //Pantalla de inicialización
Pantalla=nueva? Lienzo(){? //Crear un objeto de pantalla
¿Público? ¿Vacío? Paint(Graphics?G){//Método Overlay paint()
if(GS == null)GS = get gráficos(); //Obtiene el objeto gráfico de la pantalla.
if (en ejecución)
draw screen();
Otros
//Si el juego no ha comenzado, la imagen completa .
g.drawImage(imgPuzzle, 0, 0, this);
}
}//Establece el tamaño de la pantalla.
screen . setsize(img puzzle . get width(this), img puzzle . get height(this)
screen.addMouseListener(new?MouseAdapter(){//procesamiento Eventos del ratón.
¿Público? ¿Vacío? mousePressed(MouseEvent?i){
if(!running)? Regresar;
int? x=me.getX()/sx, y = me.gety()/sy;
int? FX =(int)quinto. getx(), fy =(int)quinto. gety();
if(math. ABS(FX-x) math. ABS(fy-y) gt;= 2) Devolución;
¿Si? (mapa[x][y]==15) devuelve;
mapa[FX][fy]= mapa[x][y];
mapa[x][y] ]= 15;
quinto .establecer ubicación(x, y
dibujar pantalla(); ;
}
¿Anulado? botonesinit(){? //Inicializar el botón
//Manejar el evento del botón "Nuevo juego"
bStart.addActionListener(new? ActionListener(){
¿Público? ¿Invalidado? actionPerformed( ActionEvent? ae){
init map(); //Inicializar mpa
dibujar pantalla(); //Dibujar una imagen
Ejecutando= True ; //Indica que el juego está en progreso
BSee.setLabel("Mostrar la imagen correcta"); //Cambiar el título del botón bSee. /p>
});
// Manejo de eventos del botón "Mostrar imagen correcta"
bSee.addActionListener(new? ActionListener(){
¿Público? ¿No es válido? actionPerformed(ActionEvent?ae){
//bVer el botón titulado "Continuar juego"
If(bSee.getLabel().equals("Continuar juego")) {
dibujar pantalla(); //Dibujar una imagen
//?BSee.setLabel("Mostrar la imagen correcta"); imagen correcta" "
}
De lo contrario{
//El título de bSee es "Mostrar la imagen correcta"
gs. drawImage(imgPuzzle, 0, 0, screen);? //Mostrar la imagen completa
BSee.setLabel("Continuar juego");
}
}
});
}
¿Anulado? DrawScreen(){//Método para dibujar imágenes
gb.clearRect(0,?0,?sx*4,?sy*4);? //Borrar el búfer
//Dibuja el bloque de imagen en la posición especificada en el búfer.
int? i;
for(int?j = 0;j lt4;j)
for(I = 0;i lt4;i)
if(mapa [i][j]! = 15)draw segment(map[I][j],I,j);
//Dibuja la imagen del buffer en la pantalla.
gs.drawImage(buff,?0,?0,?screen);
}
¿No es válido? drawSegment(int?segmento,?int?x,?int?y){
int? dx=seg4*sx, dy = seg/4 * sy;
gb.drawImage(imgPuzzle, x*sx, y*sy, x*sx sx-1, y*sy sy-1, dx ,dy,dx sx-1,dy sy-1,pantalla);
}
}