W klasie screen pojawia sie pusty error(?), a w klasie okienko trzy błędy
import java.awt.GridLayout;
import javax.swing.JFrame;
import grafika.Screen;
@SuppressWarnings("serial")
public class okienko extends JFrame{
public okienko() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("snake");
setResizable(false);
init();
}
public void init() {
setLayout(new GridLayout(1, 1, 0, 0));
Screen s = new Screen();
add(s);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new okienko();
}
}
klasa screen:
package grafika;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import java.awt.Component;
import postaci.BodyPart;
@SuppressWarnings("serial")
public class Screen extends JFrame implements Runnable{
public static final int WIDTH = 800, HEIGHT = 800;
private Thread thread;
private boolean running = false;
private BodyPart b;
private ArrayList<BodyPart> snake;
private int xCoor= 10, yCoor = 10;
private int size = 5;
private boolean right = true, left = false, down = false, up = false;
private int ticks = 0;
private Key key;
public Screen() {
setFocusable(true);
key = new Key();
addKeyListener(key);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
snake = new ArrayList<BodyPart>();
start();
}
public void tick() {
if(snake.size() == 0) {
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
}
ticks++;
if(ticks > 500000) {
if(right)xCoor++;
if(left)xCoor--;
if(down)yCoor++;
if(up)yCoor--;
ticks = 0;
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
if(snake.size() > size) {
snake.remove(0);
}
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
for(int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}
}
public void start() {
running = true;
thread = new Thread(this, "Game Loop");
thread.start();
}
public void stop() {
}
public void run() {
while(running) {
tick();
repaint();
}
}
private class Key implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_DOWN && !up) {
down = true;
left = false;
right = false;
}
if(code == KeyEvent.VK_UP && !down) {
up = true;
left = false;
right = false;
}
if(code == KeyEvent.VK_LEFT && !right) {
down = true;
left = false;
up = false;
}
if(code == KeyEvent.VK_RIGHT && !left) {
down = true;
up = false;
right = false;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
public static void main (String [] args) {
Screen screen = new Screen();
}
}
klasa BodyPart:
package postaci;
import java.awt.Color;
import java.awt.Graphics;
public class BodyPart {
private int xCoor, yCoor, width, height;
public BodyPart(int xCoor, int yCoor, int tileSize) {
this.xCoor = xCoor;
this.yCoor = yCoor;
width = tileSize;
height = tileSize;
}
public void tick() {
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(xCoor * width, yCoor * height, width, height );
g.setColor(Color.GREEN);
g.fillRect(xCoor * width + 2 , yCoor * height + 2, width, height );
}
}