ich habe ein kleines Fenster mit einem Gridlayout, darin ist ein Feld rot und die anderen grau, das rote Feld kann man mit wasd bewegen wenn man über den rand hinausgeht kommt Gameover in der Konsole(später soll dann auch die Eingabe blockiert werden)
wie kann ich es machen, dass sich das rote Feld von alleine in die Richtung(direction) bewegt? und dass in bestimmten Zeitabständen.
hier mein bisheriger Code:
kann mir jemand hier im Bezug auf mein Problem Timer oder Thread.sleep erklären hab bisher für mich nur unverständliches im I-Net gefunden
wie kann ich es machen, dass sich das rote Feld von alleine in die Richtung(direction) bewegt? und dass in bestimmten Zeitabständen.
hier mein bisheriger Code:
Java:
public class testGrid extends JFrame implements KeyListener
{
private JPanel contentPane;
private int hight = 10;
private int width = 10;
private JLabel[][] map = new JLabel[hight][width];
private int aktH = 0;
private int aktW = 0;
private String direction = "down";
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testGrid frame = new testGrid();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public testGrid()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(hight,width));
createGrid();
addKeyListener(this);
}
public void createGrid()
{
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map[i].length; j++)
{
Border border = BorderFactory.createLineBorder(Color.BLUE, 1);
map[i][j] = new JLabel();
map[i][j].setBorder(border);
map[i][j].setBackground(Color.GRAY);
map[i][j].setOpaque(true);
contentPane.add(map[i][j]);
}
}
map[0][0].setBackground(Color.RED);
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
char input = e.getKeyChar();
switch(input)
{
case 'w' : direction="up"; Move();
break;
case 's' : direction="down"; Move();
break;
case 'a' : direction="left"; Move();
break;
case 'd' : direction="right"; Move();
break;
default : System.out.println("False Eingabe"); ;
}
}
@Override
public void keyReleased(KeyEvent e)
{
}
public void goDown()
{
if(aktH != 9)
{
map[aktH][aktW].setBackground(Color.GRAY);
aktH += 1;
map[aktH][aktW].setBackground(Color.RED);
}else if(aktH == 9)
{
System.out.println("GAME OVER");
}
}
public void goUp()
{
if(aktH != 0)
{
map[aktH][aktW].setBackground(Color.GRAY);
aktH -= 1;
map[aktH][aktW].setBackground(Color.RED);
}else if(aktH == 0)
{
System.out.println("GAME OVER");;
}
}
public void goLeft()
{
if(aktW != 0)
{
map[aktH][aktW].setBackground(Color.GRAY);
aktW -= 1;
map[aktH][aktW].setBackground(Color.RED);
}else if(aktW == 0)
{
System.out.println("GAME OVER");
}
}
public void goRight()
{
if(aktW != 9)
{
map[aktH][aktW].setBackground(Color.GRAY);
aktW += 1;
map[aktH][aktW].setBackground(Color.RED);
}else if(aktW == 9)
{
System.out.println("GAME OVER");
}
}
public void Move()
{
if(direction.equalsIgnoreCase("down"))
{
goDown();
}else
if(direction.equalsIgnoreCase("up"))
{
goUp();
}else
if(direction.equalsIgnoreCase("right"))
{
goRight();
}else
if(direction.equalsIgnoreCase("left"))
{
goLeft();
}
}
}
kann mir jemand hier im Bezug auf mein Problem Timer oder Thread.sleep erklären hab bisher für mich nur unverständliches im I-Net gefunden