package javaapplication2;
import com.sun.imageio.plugins.jpeg.JPEGImageReader;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.image.BufferedImage.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Graphics.*;
public class JavaApplication2 extends Applet implements ActionListener, KeyListener{
int y = 1;
int x = 5;
Thread Puls;
private Image Roodblokje;
int[][] anArray;
Font f = new Font("Arial", Font.BOLD, 20);
Graphics buffergraphics;
Image offscreen;
Dimension dim;
public void init(){
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
Roodblokje = getImage(getDocumentBase(),"4 Blokjes in vierkant.png");
setSize(1010,907);
dim=getSize();
Thread Puls = new Thread(PulsUitvoer);
offscreen=createImage(dim.width,dim.height);
buffergraphics=offscreen.getGraphics();
anArray = new int[10][20];
for (int i=0; i<10; i++ ){
for (int j=1; j<20; j++ ){
anArray[i][j]=0;
}
}
anArray[x][y]=1;
Puls.start();
}
Runnable PulsUitvoer = new Runnable(){
public void run(){
int x2,y2;
while (true){
try {
x2 = x;
y2 = y+1;
if ( y2<19) {
if ( anArray[x2][y2]==0){
System.out.println(x2 + " array " + y2 );
anArray[x][y]=0;
anArray[x2][y2]=1;
y++;
}
}
else {
System.out.println("debug1");
anArray[x2][y2]=2;
anArray[5][1]=1;
x=5;
y=1;
}
repaint();
Puls.sleep(500);
} catch (InterruptedException ex) {
}
}
}
};
public void paint (Graphics g){
dim=getSize();
buffergraphics.clearRect(0, 0, dim.width, dim.height);
//Tekstopmaak
buffergraphics.setFont(f);
//Lijn om vierkant
buffergraphics.setColor(Color.black);
buffergraphics.fillRect (0, 0, 457, 907);
//Achtergrond binnen vierkant
buffergraphics.setColor(Color.gray);
buffergraphics.fillRect (0, 5, 452, 897);
//Scoreboard rand
buffergraphics.setColor(Color.black);
buffergraphics.fillRect (510, 400, 500, 507);
//Scoreboard zijkant
buffergraphics.setColor(Color.gray);
buffergraphics.fillRect (515, 405, 490, 497);
//Next blokje rand
buffergraphics.setColor(Color.black);
buffergraphics.fillRect(510, 0, 500, 360);
//Next blokje vierkant
buffergraphics.setColor(Color.gray);
buffergraphics.fillRect (515, 5, 490, 350);
//Tekst in next blokje vak
buffergraphics.setColor(Color.black);
buffergraphics.drawString("Next Block:", 720, 30);
//Tekst in score vak
buffergraphics.setColor(Color.black);
buffergraphics.drawString("Score:", 735, 430);
for (int i=0; i<10; i++ ){
for (int j=1; j<20; j++ ){
if(anArray[i][j]==1){
buffergraphics.drawImage(Roodblokje, i*45, j*45, this);
}
if(anArray[i][j]==2){
buffergraphics.drawImage(Roodblokje, i*45, j*45, this);
}
}
}
g.drawImage(offscreen, 0,0, this);
}
public void update (Graphics g){
paint(g);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_DOWN){
anArray[x][50]=2;
anArray[x][y]=0;
x=5;
y=1;
anArray[x][y]=1;
}
if (code == KeyEvent.VK_RIGHT && x<9){
anArray[x][y]=0;
x++;
anArray[x][y]=1;
}
if (code == KeyEvent.VK_LEFT && x>0){
anArray[x][y]=0;
x--;
anArray[x][y]=1;
}
repaint();
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
private void maakTraceMogelijk() {
throw new UnsupportedOperationException("Not yet implemented");
}
}