i have the following grid with cells of size 10x10 and i am trying to write a certain number in the absolute middle of every cell. is there any way to do it?
the grid is being created inside a public void paintComponent(Graphics g)
, is zoomable and has the following code:
g.setColor(Color.darkGray);
for (int i = 0; i < getWIDTH(); i++) {
g.drawLine(i * size, 0, i * size, HEIGHT);
}
for (int i = 0; i < getHEIGHT(); i++) {
g.drawLine(0, i * size, WIDTH, i * size);
}
update: i've managed to do this by using g.drawString
method, but the text is not center and remains the same size when zooming in our out
****** SOLUTION *******
update: after trying my hand with multiple techniques, mostly weird and quite stupid actually, i found out a way to do it. the idea is that i am declaring a set of coordinates to be used in the drawString()
method. the position is not perfect, but it does its job, and as someone once said, if it looks stupid but it works, then it ain't stupid. even the font is zoomable, increasing or decreasing in size when zooming in or out, and will only display if the size of the grid is bigger than 20px. the grid now looks like this (i gotta change the font tho):
the code is:
if(size > 20) {
g.setColor(Color.darkGray);
Font smallNumbers = new Font("SansSerif", Font.PLAIN, size/2);
int x = j * size + size / 3;
int y = i * size + (size * 100) / 145;
g.setFont(smallNumbers);
g.drawString("0", x, y);
}