Witam
Wziąłem się za napisanie Tetrisa. Program nie jest jeszcze skończony. Gdy go uruchamiam, to pokazuje się puste okno i nic się nie rysuje.
Wiem, że nie zrobiłem jeszcze sterowania, nie dokończona jest metoda aktualizacji klocka itp. itd., ale sama plansza i klocek powinien się pojawić, a tu nic :(. Proszę o znalezienie błędu, przez który nic się nie rysuje.
GUI:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class GUI extends JFrame
{
int stan = 0;
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setTitle("Tetris");
frame.setSize(500, 600);
frame.setLocation(400, 100);
frame.setLayout(null);
frame.setVisible(true);
Klocki.rysujPlansze();
Klocki watek = new Klocki();
watek.start();
}
public void paint(Graphics g)
{
switch(stan)
{
case 0:
rysujPlansze(g);
case 1:
rysujKlocki(g);
}
}
public void rysujPlansze(Graphics g)
{
g.setColor(Color.BLUE);
for(int i=0; i<Klocki.plansza.length; i++)
{
for(int j=0; j<Klocki.plansza[0].length; j++)
{
switch(Klocki.plansza[i][j])
{
case 0:
{
g.setColor(Color.BLACK);
g.drawRect((20*j)+50, (20*i)+50, 20, 20);
break;
}
case 1:
{
g.setColor(Color.GRAY);
g.fillRect((20*j)+50, (20*i)+50, 20, 20);
}
}
}
}
stan = 1;
}
public void rysujKlocki(Graphics g)
{
for(int i=0; i<Klocki.planszaKlockow.length; i++)
{
for(int j=0; j<Klocki.planszaKlockow[0].length; j++)
{
switch(Klocki.planszaKlockow[i][j])
{
case 2:
{
g.setColor(Color.GRAY);
g.fillRect((20*j)+50, (20*i)+50, 20, 20);
}
case 3:
{
g.setColor(Color.ORANGE);
g.drawRect((20*j)+50, (20*i)+50, 20, 20);
}
case 4:
{
g.setColor(Color.BLUE);
g.drawRect((20*j)+50, (20*i)+50, 20, 20);
}
}
}
}
stan = 2;
}
public void keyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case 83:
Klocki.sterowanie('s');
break;
case 65:
Klocki.sterowanie('a');
break;
case 68:
Klocki.sterowanie('d');
break;
case 32:
Klocki.sterowanie('c');
break;
default:
System.out.println(e.getKeyCode());
break;
}
}
}
Klocki:
import java.util.Random;
public class Klocki extends Thread
{
double szybkoscSpadania = 1;
static GUI gui = new GUI();
static Integer aktualnyKlocek = new Integer(0);
final static int plansza[][] = new int[21][12];
static int planszaKlockow[][] = new int[21][12];
final int x1a[][] = new int[4][1];
static Character obrotKlocka2 = new Character('a');
static Integer numerX2 = new Integer(1);
static Integer numerY2 = new Integer(6);
static Character obrotKlocka3 = new Character('a');
static Integer numerX3 = new Integer(0);
static Integer numerY3 = new Integer(7);
static Integer numerX4 = new Integer(0);
static Integer numerY4 = new Integer(6);
static Character obrotKlocka5 = new Character('a');
static Integer numerX5 = new Integer(1);
static Integer numerY5 = new Integer(6);
static Character obrotKlocka6 = new Character('a');
static Integer numerX6 = new Integer(1);
static Integer numerY6 = new Integer(6);
static Character obrotKlocka7 = new Character('a');
static Integer numerX7 = new Integer(0);
static Integer numerY7 = new Integer(7);
static Character obrotKlocka8 = new Character('a');
static Integer numerX8 = new Integer(0);
static Integer numerY8 = new Integer(7);
public void run()
{
if(aktualnyKlocek == 0)
{
losujKlocek();
tworzenieKlocka(aktualnyKlocek);
}
else if(aktualnyKlocek !=0)
{
aktualizacjaKlocka();
}
try {
sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
static void rysujPlansze()
{
int plansza[][] =
{
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1}
};
}
static void losujKlocek()
{
Random r = new Random();
aktualnyKlocek = r.nextInt((6)+2);
}
static void tworzenieKlocka(Integer aktualnyKlocek)
{
switch(aktualnyKlocek)
{
case 2:
{
if(planszaKlockow[0][6]== 0 && planszaKlockow[1][6]== 0 && planszaKlockow[2][6]== 0 && planszaKlockow[3][6]== 0)
{
planszaKlockow[0][6] = 2;
planszaKlockow[1][6] = 2;
planszaKlockow[2][6] = 2;
planszaKlockow[3][6] = 2;
}
}
case 3:
{
if(planszaKlockow[0][6]== 0 && planszaKlockow[0][7]== 0 && planszaKlockow[0][8]== 0 && planszaKlockow[1][7]== 0)
{
planszaKlockow[0][6] = 3;
planszaKlockow[0][7] = 3;
planszaKlockow[0][8] = 3;
planszaKlockow[1][7] = 3;
}
}
case 4:
{
if(planszaKlockow[0][6]== 0 && planszaKlockow[0][7]== 0 && planszaKlockow[1][6]== 0 && planszaKlockow[1][7]== 0)
{
planszaKlockow[0][6] = 4;
planszaKlockow[0][7] = 4;
planszaKlockow[1][6] = 4;
planszaKlockow[1][7] = 4;
}
}
case 5:
{
if(planszaKlockow[0][6]== 0 && planszaKlockow[1][6]== 0 && planszaKlockow[2][6]== 0 && planszaKlockow[2][7]== 0)
{
planszaKlockow[0][6] = 5;
planszaKlockow[1][6] = 5;
planszaKlockow[2][6] = 5;
planszaKlockow[2][7] = 5;
}
}
case 6:
{
if(planszaKlockow[0][7]== 0 && planszaKlockow[1][7]== 0 && planszaKlockow[2][7]== 0 && planszaKlockow[2][6]== 0)
{
planszaKlockow[0][7] = 6;
planszaKlockow[1][7] = 6;
planszaKlockow[2][7] = 6;
planszaKlockow[2][6] = 6;
}
}
case 7:
{
if(planszaKlockow[0][7]== 0 && planszaKlockow[0][8]== 0 && planszaKlockow[1][6]== 0 && planszaKlockow[1][7]== 0)
{
planszaKlockow[0][7] = 7;
planszaKlockow[0][8] = 7;
planszaKlockow[1][6] = 7;
planszaKlockow[1][7] = 7;
}
}
case 8:
{
if(planszaKlockow[0][6]== 0 && planszaKlockow[0][7]== 0 && planszaKlockow[1][7]== 0 && planszaKlockow[1][8]== 0)
{
planszaKlockow[0][6] = 8;
planszaKlockow[0][7] = 8;
planszaKlockow[1][7] = 8;
planszaKlockow[1][8] = 8;
}
}
}
}
void aktualizacjaKlocka()
{
switch(aktualnyKlocek)
{
case 2:
{
if(obrotKlocka2 == 'a' && planszaKlockow[numerX2+3][numerY2] == 0)
{
planszaKlockow[numerX2-1][numerY2] = 0;
planszaKlockow[numerX2+3][numerY2] = 2;
numerX2-=1;
numerX2+=3;
}
else if(obrotKlocka2 == 'b' && planszaKlockow[numerX2+1][numerY2] == 0 &&
planszaKlockow[numerX2+1][numerY2+1] == 0 &&
planszaKlockow[numerX2+1][numerY2+2] == 0 &&
planszaKlockow[numerX2+1][numerY2+3] == 0)
{
planszaKlockow[numerX2][numerY2] = 0;
planszaKlockow[numerX2][numerY2+1] = 0;
planszaKlockow[numerX2][numerY2+2] = 0;
planszaKlockow[numerX2][numerY2+3] = 0;
planszaKlockow[numerX2+1][numerY2] = 2;
planszaKlockow[numerX2+1][numerY2+1] = 2;
planszaKlockow[numerX2+1][numerY2+2] = 2;
planszaKlockow[numerX2+1][numerY2+3] = 2;
}
}
case 3:
{
if(obrotKlocka3 == 'a' && planszaKlockow[numerX2+1][numerY2-1] == 0 &&
planszaKlockow[numerX2+1][numerY2+1] == 0)
{
planszaKlockow[numerX3][numerY3-1] = 0;
planszaKlockow[numerX3][numerY3+1] = 0;
planszaKlockow[numerX3+1][numerY3-1] = 3;
planszaKlockow[numerX3+1][numerY3+1] = 3;
}
else if(obrotKlocka3 == 'b' && planszaKlockow[numerX3+1][numerY3-1] == 0 &&
planszaKlockow[numerX3+2][numerY3] == 0)
{
planszaKlockow[numerX3-1][numerY3] = 0;
planszaKlockow[numerX3][numerY3-1] = 0;
planszaKlockow[numerX3+1][numerY3-1] = 3;
planszaKlockow[numerX3+2][numerY3] = 3;
}
else if(obrotKlocka3 == 'c' && planszaKlockow[numerX3-1][numerY3] == 0 &&
planszaKlockow[numerX3][numerY3-1] == 0 &&
planszaKlockow[numerX3][numerY3+1] == 0)
{
planszaKlockow[numerX3-1][numerY3] = 0;
planszaKlockow[numerX3][numerY3-1] = 0;
planszaKlockow[numerX3][numerY3+1] = 0;
planszaKlockow[numerX3+1][numerY3] = 3;
planszaKlockow[numerX3+1][numerY3-1] = 3;
planszaKlockow[numerX3+1][numerY3+1] = 3;
}
else if(obrotKlocka3 == 'd' && planszaKlockow[numerX3+1][numerY3+1] == 0 &&
planszaKlockow[numerX3+2][numerY3] == 0)
{
planszaKlockow[numerX3-1][numerY3] = 0;
planszaKlockow[numerX3][numerY3+1] = 0;
planszaKlockow[numerX3+1][numerY3+1] = 3;
planszaKlockow[numerX3+2][numerY3] = 3;
}
}
case 4:
{
if(planszaKlockow[numerX4+1][numerY4] == 0 && planszaKlockow[numerX4+1][numerY4+1] == 0)
{
planszaKlockow[numerX4-1][numerY4] = 0;
planszaKlockow[numerX4-1][numerY4+1] = 0;
planszaKlockow[numerX4+1][numerY4] = 3;
planszaKlockow[numerX4+1][numerY4+1] = 3;
}
}
}
}
static void sterowanie(char c)
{
switch(c)
{
case 'c':
{
obracanieKlocka();
}
case 's':
{
}
case 'a':
{
}
case 'd':
{
}
}
}
private static void obracanieKlocka() {}
}
Pozdrawiam
//edti kod zaktualizowany