witam, kiedyś kilka wątków wcześniej zacząłem pisać snake w javie. mam problem z rysowaniem węża. stworzyłem okno i tło i na tym etapie się wszystko ładnie wyświetla. mam problem z rysowaniem węża, może ktoś wskazać błąd?? kod wrzucę na githuba jak tylko program ściągnę do obsługi. pozdrawiam i z góry dziękuję za pomoc.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsnake;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author marcin
*/
public class JavaSwingSnake {
/**
* @param args the command line arguments
*/
static int timeSpeed = 3000;// 3000ms
public static void main(String[] args) {
SnakeWindow snakeWindow = new SnakeWindow();
while (true) {
try {
snakeWindow.repaint();
Thread.sleep(timeSpeed);
} catch (InterruptedException ex) {
Logger.getLogger(JavaSwingSnake.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsnake;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
/**
*
* @author marcin
*/
public class SnakeWindow extends JFrame {
static int widthWindow = 800;
static int heightWindow = 600;
static int xWindowPosition = 300;
static int yWindowPosition = 50;
static int transX;
static int transY;
private BufferStrategy bufferStrategy;
private SnakeWindowBackground snakeWindowBackground;
private Snake snake = new Snake();
SnakeWindow() {
makeScreen();
}
private void makeScreen() {
setTitle("snake");
setVisible(true);
setResizable(false);
setLocation(xWindowPosition, yWindowPosition);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createBufferStrategy(2);
bufferStrategy = getBufferStrategy();
Insets i = getInsets();
transX = i.left;
transY = i.top;
setSize(new Dimension(widthWindow + transX + i.right, heightWindow));
snakeWindowBackground = new SnakeWindowBackground();
}
@Override
public void repaint() {
Graphics graphics = bufferStrategy.getDrawGraphics();
draw(graphics);
graphics.dispose();
bufferStrategy.show();
}
private void draw(Graphics graphics) {
snakeWindowBackground.draw(graphics);
snake.draw(graphics);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsnake;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
/**
*
* @author marcin
*/
public class SnakeWindowBackground {
private BufferedImage background;
static int tileWidth = 10;
static int tileHeight = 10;
SnakeWindowBackground() {
background = new BufferedImage(SnakeWindow.widthWindow, SnakeWindow.heightWindow,
BufferedImage.TYPE_INT_RGB);
Graphics graphics = background.getGraphics();
for (int x = 0; x < SnakeWindow.widthWindow / tileWidth; ++x) {
for (int y = 0; y < SnakeWindow.heightWindow / tileHeight; ++y) {
graphics.setColor(Color.gray);
graphics.drawRect(x * tileWidth, y * tileHeight, tileWidth,
tileHeight);
}
}
}
public void draw(Graphics graphics) {
graphics.drawImage(background, SnakeWindow.transX, SnakeWindow.transY, null);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsnake;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Stack;
/**
*
* @author marcin
*/
public class Snake {
Stack snakeParts = new Stack();
public Snake() {
snakeParts.add(new SnakePart(10, 10));
}
public void draw(Graphics graphics){
for(int i = 0; i < snakeParts.size(); ++i){
snakeParts.get(i).render(graphics);
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsnake;
import java.awt.Color;
import java.awt.Graphics;
/**
*
* @author marcin
*/
public class SnakePart {
private int xPositionHead;
private int yPositionHead;
private int widthTail = 10;
private int heightTail = 10;
public SnakePart(int xPositionHead, int yPositionHead) {
this.xPositionHead = xPositionHead;
this.yPositionHead = yPositionHead;
}
public void render(Graphics graphics){
graphics.setColor(Color.red);
graphics.fillRoundRect(xPositionHead, yPositionHead, widthTail,
heightTail, 360, 360);
}
}