akl
geek
Код:
import java.awt.*;
import javax.swing.*;
import java.awt.Dimension;
import javax.swing.BorderFactory;
class GamePanel extends JComponent{
Graphics g;
int[][] FMATRIX = null;
int COUNTW = 0; // number of squares along the width
int COUNTH = 0; // number of squares along the height
int SQSIZE = 0;
int WIDTH = 0, HEIGHT = 0;
private DebuggerTools dt;
public GamePanel(int count_w, int count_h){
this.setSize(400, 400);
this.setMinimumSize(new Dimension(400, 300));
this.setBorder(BorderFactory.createLineBorder(Color.GREEN));
// Init debugger
dt = new DebuggerTools();
// Init variables
COUNTW = count_w;
COUNTH = count_h;
// Init game matrix
FMATRIX = new int[COUNTW][COUNTH];
// Init size of the square
Dimension sz = getSize();
WIDTH = sz.width; // panel size along X coord
HEIGHT = sz.height; // panel size along Y coord
}
~~~~~~~~~~~~ code ~~~~~~~~~~~~~~~~~
public void fillSquare (int w, int h){
FMATRIX[w][h] = 1;
Graphics gf = getGraphics();
System.out.println (gf);
int coord_x = getSquareXY(w,h).width;
int coord_y = getSquareXY(w,h).height;
if (gf != null) {
gf.setColor(Color.BLACK);
gf.fillRect(coord_x, coord_y, SQSIZE, SQSIZE);
} else {
repaint();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
/* drawing the grid */
// setting drawing color
g.setColor(Color.GRAY);
// drawing vertical lines
for (int i = 0; i <= COUNTW*SQSIZE; i += SQSIZE){
g.drawLine (i, 0 , i, COUNTH*SQSIZE);
}
// drawing horizontal lines
for (int j = 0; j <= COUNTH*SQSIZE; j += SQSIZE){
g.drawLine (0, j, COUNTW*SQSIZE, j);
}
}
}