witam, piszę gę snake, w tej chwili mam dwie klasy: pierwsza która tworzy okno i druga która rysuje plansze wraz z dwoma przyciskami(przycisk który rozpoczyna grę oraz przycisk który kończy grę), oraz labelem który wyświetla wynik. proszę o opinie o kodzie.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsnake;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @author marcin
*/
public class JavaSwingSnake extends JFrame {
/**
* @param args the command line arguments
*/
private int widthWindow = 640;
private int heightWindow = 480;
public JavaSwingSnake() {
setSize(new Dimension(widthWindow, heightWindow));
setTitle("snake !!!!!!!");
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JavaSwingSnake javaSwingSnake = new JavaSwingSnake();
ClassDrawBoard classDrawBoard = new ClassDrawBoard();
javaSwingSnake.add(classDrawBoard);
}
});
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingsnake;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author marcin
*/
public class ClassDrawBoard extends JPanel {
JLabel scoreLabel;
JButton startGameButton;
JButton endGameButton;
BorderLayout borderLayout;
BoxLayout boxLayout;
BoxLayout boxLayoutButtons;
JPanel controlPanel;
JPanel drawPanel;
private int sizeSquare = 20;//wielkosc kwadratu
private int startDrawPositionX = 20;//poczatek rysowania
private int startDrawPositionY = 20;//poczatek rysowania
//oznaczenia cyfr w tablicy
//0 - mur
//1 - plansza po ktorej porusza sie waz
static int[][] board = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1
{0, 1, 1,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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//15
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//16
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//17
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//18
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//19
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//20
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}//21
};
public ClassDrawBoard() {
scoreLabel = new JLabel("score: 0");
startGameButton = new JButton("start");
endGameButton = new JButton("end");
controlPanel = new JPanel();
drawPanel = new JPanel();
borderLayout = new BorderLayout();
boxLayoutButtons = new BoxLayout(controlPanel, BoxLayout.Y_AXIS);
setLayout(borderLayout);
add(drawPanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.EAST);
controlPanel.setLayout(boxLayoutButtons);
controlPanel.setPreferredSize(new Dimension(100, 480));
controlPanel.add(scoreLabel);
controlPanel.add(startGameButton);
controlPanel.add(endGameButton);
drawPanel.setBackground(Color.BLACK);
drawPanel.setPreferredSize(new Dimension(540, 480));
}
@Override
public void paint(Graphics graphics) {
super.paint(graphics);
graphics.setColor(Color.red);
graphics.drawRect(18, 18, 504, 424);
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[0].length; ++j) {
if (board[i][j] == 0) {
graphics.setColor(Color.blue);
graphics.fillRect((sizeSquare * j) + startDrawPositionX,
(sizeSquare * i) + startDrawPositionY, sizeSquare,
sizeSquare);
}
}
}
for (int i = 2; i < board.length - 1; ++i) {
for (int j = 1; j < board[0].length - 1; ++j) {
graphics.setColor(Color.green);
graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
+ sizeSquare, (j * sizeSquare) + sizeSquare
+ startDrawPositionX, (i * sizeSquare) + startDrawPositionY);
}
}
for (int i = 1; i < board.length - 1; ++i) {
for (int j = 2; j < board[0].length - 1; ++j) {
graphics.setColor(Color.green);
graphics.drawLine((j * sizeSquare) + sizeSquare, (i * sizeSquare)
+ sizeSquare, (j * sizeSquare) + sizeSquare,
(i * sizeSquare) + startDrawPositionY + sizeSquare);
}
}
}
}